diff --git a/custom_components/sector/alarm_control_panel.py b/custom_components/sector/alarm_control_panel.py index 6d2e0f6..074da70 100644 --- a/custom_components/sector/alarm_control_panel.py +++ b/custom_components/sector/alarm_control_panel.py @@ -10,8 +10,7 @@ SUPPORT_ALARM_ARM_HOME, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import STATE_ALARM_PENDING -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import ( @@ -49,8 +48,6 @@ def __init__( self._hub = hub super().__init__(coordinator) self._code: str = code if code != "" else None - self._state: str = STATE_ALARM_PENDING - self._changed_by: str = "" self._displayname: str = self._hub.alarm_displayname self._isonline: str = self._hub.alarm_isonline self._attr_name = f"Sector Alarmpanel {self._hub.alarm_id}" @@ -104,3 +101,11 @@ async def async_alarm_arm_away(self, code=None) -> None: if code: await self._hub.triggeralarm(command, code=code) await self.coordinator.async_refresh() + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._isonline: str = self._hub.alarm_isonline + self._attr_changed_by = self._hub.alarm_changed_by + self._attr_state = self._hub.alarm_state + self.async_write_ha_state() diff --git a/custom_components/sector/lock.py b/custom_components/sector/lock.py index f8d6c96..6a8608b 100644 --- a/custom_components/sector/lock.py +++ b/custom_components/sector/lock.py @@ -3,8 +3,8 @@ from homeassistant.components.lock import LockEntity, LockEntityDescription from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_CODE, STATE_LOCKED, STATE_UNKNOWN -from homeassistant.core import HomeAssistant +from homeassistant.const import ATTR_CODE, STATE_LOCKED +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import ( @@ -71,15 +71,16 @@ def __init__( """Initialize lock.""" self._hub = hub super().__init__(coordinator) - self._serial = serial self._attr_name = description.name self._attr_unique_id: str = "sa_lock_" + str(description.key) self._attr_code_format: str = f"^\\d{code_format}$" + self._attr_is_locked = bool( + self._hub.lock_state[description.key] == STATE_LOCKED + ) self._autolock = autolock self._code = code self.entity_description = description self._code_format = code_format - self._state: str = STATE_UNKNOWN @property def device_info(self) -> DeviceInfo: @@ -98,20 +99,15 @@ def extra_state_attributes(self) -> dict: """Additional states of lock.""" return { "Autolock": self._autolock, - "Serial No": self._serial, + "Serial No": self.entity_description.key, } - @property - def is_locked(self) -> bool: - """Return if locked.""" - return self._state == STATE_LOCKED - async def async_unlock(self, **kwargs) -> None: """Unlock lock.""" command = "unlock" code = kwargs.get(ATTR_CODE, self._code) if code: - await self._hub.triggerlock(self._serial, code, command) + await self._hub.triggerlock(self.entity_description.key, code, command) await self.coordinator.async_refresh() async def async_lock(self, **kwargs) -> None: @@ -119,5 +115,13 @@ async def async_lock(self, **kwargs) -> None: command = "lock" code = kwargs.get(ATTR_CODE, self._code) if code: - await self._hub.triggerlock(self._serial, code, command) + await self._hub.triggerlock(self.entity_description.key, code, command) await self.coordinator.async_refresh() + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._attr_is_locked = bool( + self._hub.lock_state[self.entity_description.key] == STATE_LOCKED + ) + self.async_write_ha_state() diff --git a/custom_components/sector/sensor.py b/custom_components/sector/sensor.py index 6c1891b..805f88b 100644 --- a/custom_components/sector/sensor.py +++ b/custom_components/sector/sensor.py @@ -9,7 +9,7 @@ ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import TEMP_CELSIUS -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import ( @@ -88,3 +88,9 @@ def device_info(self) -> DeviceInfo: def extra_state_attributes(self) -> dict: """Extra states for sensor.""" return {"Serial No": self.entity_description.key} + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._attr_native_value = self._hub.temp_state[self.entity_description.key] + self.async_write_ha_state() diff --git a/custom_components/sector/switch.py b/custom_components/sector/switch.py index ac799da..0a9a514 100644 --- a/custom_components/sector/switch.py +++ b/custom_components/sector/switch.py @@ -7,7 +7,7 @@ SwitchEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import ( @@ -56,12 +56,11 @@ def __init__( """Initialize Switch.""" self._hub = hub super().__init__(coordinator) - self._serial = description.key self._attr_name = description.name self._attr_unique_id: str = "sa_switch_" + str(description.key) self.entity_description = description - self._attr_is_on = bool(self._hub.switch_state[self._serial] == "On") - self._id: str = self._hub.switch_id[self._serial] + self._attr_is_on = bool(self._hub.switch_state[description.key] == "On") + self._id: str = self._hub.switch_id[description.key] @property def device_info(self) -> DeviceInfo: @@ -79,7 +78,7 @@ def device_info(self) -> DeviceInfo: def extra_state_attributes(self) -> dict: """Additional states for switch.""" return { - "Serial No": self._serial, + "Serial No": self.entity_description.key, "Id": self._id, } @@ -95,3 +94,11 @@ async def async_triggerswitch(self, command) -> None: """Trigger switch.""" await self._hub.triggerswitch(self._id, command) await self.coordinator.async_refresh() + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._attr_is_on = bool( + self._hub.switch_state[self.entity_description.key] == "On" + ) + self.async_write_ha_state()