Skip to content

Commit

Permalink
Fix instant updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed Dec 20, 2021
1 parent 7dfa0db commit d3685c8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
4 changes: 1 addition & 3 deletions custom_components/sector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
websession=websession,
)

async def async_update_data():
async def async_update_data() -> None:
"""Fetch data from api."""

now = datetime.utcnow()
Expand All @@ -111,8 +111,6 @@ async def async_update_data():
)
await api.fetch_info()

return True

coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
Expand Down
20 changes: 13 additions & 7 deletions custom_components/sector/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
SUPPORT_ALARM_ARM_HOME,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import (
STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME,
STATE_ALARM_DISARMED,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -82,7 +87,8 @@ async def async_alarm_arm_home(self, code=None) -> None:
code = self._code
if code:
await self._hub.triggeralarm(command, code=code)
await self.coordinator.async_refresh()
self._attr_state = STATE_ALARM_ARMED_HOME
await self.async_write_ha_state()

async def async_alarm_disarm(self, code=None) -> None:
"""Arm alarm off."""
Expand All @@ -91,7 +97,8 @@ async def async_alarm_disarm(self, code=None) -> None:
code = self._code
if code:
await self._hub.triggeralarm(command, code=code)
await self.coordinator.async_refresh()
self._attr_state = STATE_ALARM_DISARMED
await self.async_write_ha_state()

async def async_alarm_arm_away(self, code=None) -> None:
"""Arm alarm away."""
Expand All @@ -100,12 +107,11 @@ async def async_alarm_arm_away(self, code=None) -> None:
code = self._code
if code:
await self._hub.triggeralarm(command, code=code)
await self.coordinator.async_refresh()
self._attr_state = STATE_ALARM_ARMED_AWAY
await self.async_write_ha_state()

@callback
def _handle_coordinator_update(self) -> None:
def 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()
12 changes: 6 additions & 6 deletions custom_components/sector/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from homeassistant.components.lock import LockEntity, LockEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CODE, STATE_LOCKED
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -110,20 +110,20 @@ async def async_unlock(self, **kwargs) -> None:
code = kwargs.get(ATTR_CODE, self._code)
if code:
await self._hub.triggerlock(self.entity_description.key, code, command)
await self.coordinator.async_refresh()
self._attr_is_locked = False
await self.async_write_ha_state()

async def async_lock(self, **kwargs) -> None:
"""Lock lock."""
command = "lock"
code = kwargs.get(ATTR_CODE, self._code)
if code:
await self._hub.triggerlock(self.entity_description.key, code, command)
await self.coordinator.async_refresh()
self._attr_is_locked = True
await self.async_write_ha_state()

@callback
def _handle_coordinator_update(self) -> None:
def update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_is_locked = bool(
self._hub.lock_state[self.entity_description.key] == "lock"
)
self.async_write_ha_state()
8 changes: 3 additions & 5 deletions custom_components/sector/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorStateClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -89,8 +89,6 @@ def extra_state_attributes(self) -> dict:
"""Extra states for sensor."""
return {"Serial No": self.entity_description.key}

@callback
def _handle_coordinator_update(self) -> None:
def 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()
19 changes: 8 additions & 11 deletions custom_components/sector/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -86,21 +86,18 @@ def extra_state_attributes(self) -> dict:

async def async_turn_on(self, **kwargs) -> None:
"""Turn the switch on."""
await self.async_triggerswitch("On")
await self._hub.triggerswitch(self._id, "On")
self._attr_is_on = True
await self.async_write_ha_state

async def async_turn_off(self, **kwargs) -> None:
"""Turn the switch off."""
await self.async_triggerswitch("Off")
await self._hub.triggerswitch(self._id, "Off")
self._attr_is_on = False
await self.async_write_ha_state

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:
def 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()
1 change: 1 addition & 0 deletions hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "Sector Alarm",
"render_readme": true,
"domains": ["sensor", "lock", "alarm_control_panel", "switch"],
"homeassistant": "2021.12.0",
"iot_class": "Cloud Polling"
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

**Date created:** 2020-04-29

**Last update:** 2021-12-14
**Last update:** 2021-12-20

---

Expand Down

0 comments on commit d3685c8

Please sign in to comment.