Skip to content

Commit

Permalink
Fix state issue (#62)
Browse files Browse the repository at this point in the history
* Use new alarm state property

https://developers.home-assistant.io/blog/2024/10/22/new-alarm-state-property/

* Trim spaces from server url input + refine error handling

* Bump version to 1.2.2 + Require HACS 2.0.0 & HA Core 2024.11.0

---------

Co-authored-by: Ludovic ENGRAND <[email protected]>
  • Loading branch information
the8tre and Ludovic ENGRAND authored Nov 9, 2024
1 parent 33a181d commit 5a5e610
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
24 changes: 12 additions & 12 deletions custom_components/somfy_protexial/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
from homeassistant.components.alarm_control_panel import (
AlarmControlPanelEntity,
AlarmControlPanelEntityFeature,
AlarmControlPanelState,
CodeFormat,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_NIGHT,
STATE_ALARM_DISARMED,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -73,7 +68,6 @@ def __init__(
self.modes.append(AlarmControlPanelEntityFeature.ARM_HOME)
self.arm_code = arm_code
self._changed_by = None
self._attr_state = self.__getCurrentState()

@property
def name(self):
Expand Down Expand Up @@ -107,9 +101,13 @@ def changed_by(self):
"""Return the last change triggered by."""
return self._changed_by

@property
def alarm_state(self) -> AlarmControlPanelState | None:
"""Return the state of the alarm."""
return self.__getCurrentState()

@callback
def _handle_coordinator_update(self) -> None:
self._attr_state = self.__getCurrentState()
self.async_write_ha_state()

def __getCurrentState(self):
Expand All @@ -122,16 +120,18 @@ def __getCurrentState(self):
active_zones += Zone.C.value

if active_zones == Zone.NONE.value:
return STATE_ALARM_DISARMED
return AlarmControlPanelState.DISARMED

if active_zones == Zone.ABC.value:
return STATE_ALARM_ARMED_AWAY
return AlarmControlPanelState.ARMED_AWAY

if active_zones == self.night_zones:
return STATE_ALARM_ARMED_NIGHT
return AlarmControlPanelState.ARMED_NIGHT

if active_zones == self.home_zones:
return STATE_ALARM_ARMED_HOME
return AlarmControlPanelState.ARMED_HOME

return AlarmControlPanelState.UNKNOWN

async def async_alarm_disarm(self, code=None):
self.check_arm_code(code)
Expand Down
13 changes: 11 additions & 2 deletions custom_components/somfy_protexial/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,22 @@ class ProtexialConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
MINOR_VERSION = 3

def __init__(self) -> None:
super().__init__()
self.url = None
self.api_type = None
self.protexial = None
self.code = None
self.username = None
self.password = None

async def async_step_user(self, user_input):
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

errors = {}
if user_input is not None:
parts = urlparse(user_input[CONF_URL])
parts = urlparse(user_input[CONF_URL].strip())
self.url = f"{parts.scheme}://{parts.netloc}"
session = aiohttp_client.async_create_clientsession(self.hass)
self.protexial = SomfyProtexial(session, self.url)
Expand All @@ -62,7 +71,7 @@ async def async_step_user(self, user_input):
challenge = await self.protexial.get_challenge()
return await self.async_step_login(None, challenge)
except Exception as e:
_LOGGER.error(e)
_LOGGER.exception(e)
errors["base"] = "connection"

return self.async_show_form(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/somfy_protexial/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"issue_tracker": "https://github.com/the8tre/somfy-protexial/issues",
"loggers": ["custom_components.somfy_protexial"],
"requirements": ["pyquery==2.0.0"],
"version": "1.2.1"
"version": "1.2.2"
}
30 changes: 15 additions & 15 deletions custom_components/somfy_protexial/protexial.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async def guess_and_set_api_type(self):
async def do_guess_get(self, page) -> str:
try:
async with asyncio.timeout(HTTP_TIMEOUT):
_LOGGER.debug(f"Guess {self.url + page}")
_LOGGER.debug(f"Guess '{self.url + page}'")
response = await self.session.get(
self.url + page, headers={}, allow_redirects=False
)
Expand All @@ -261,25 +261,25 @@ async def do_guess_get(self, page) -> str:
raise SomfyException("Unavailable, please retry later")
# Looks like another model
except asyncio.TimeoutError as exception:
_LOGGER.error(
"Timeout error fetching information from %s - %s",
page,
exception,
)
raise SomfyException(
f"Timeout error fetching information from {page} - {exception}"
)
f"Timeout error fetching from '{self.url + page}'"
) from exception
except ClientError as exception:
_LOGGER.error(
"Error fetching information from %s - %s",
page,
exception,
)
raise SomfyException(
f"Error fetching information from {page} - {exception}"
f"Error fetching from '{self.url + page}'"
) from exception
except UnicodeDecodeError as exception:
_LOGGER.error(
"Incompatible encoding found in '%s' - %s", self.url + page, exception
)
except SomfyException:
raise
except Exception as exception:
_LOGGER.error("Something really wrong happened! - %s", exception)
_LOGGER.error(
"Something really wrong happened when fetching from '%s' ! - %s",
self.url + page,
exception,
)
return None

async def get_challenge(self):
Expand Down
4 changes: 2 additions & 2 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Somfy Protexial",
"hacs": "1.6.0",
"homeassistant": "2022.8.0"
"hacs": "2.0.0",
"homeassistant": "2024.11.0"
}

0 comments on commit 5a5e610

Please sign in to comment.