Skip to content

Commit

Permalink
fix updating of binary sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
msp1974 committed Sep 7, 2024
1 parent 4579cb0 commit 81a74f0
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions custom_components/wiser/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
WiserSmokeAlarm(data, device.id, "Smoke Alarm"),
WiserHeatAlarm(data, device.id, "Heat Alarm"),
WiserTamperAlarm(data, device.id, "Tamper Alarm"),
WiserFaultWarning(data, device.id, "Fault"),
WiserFaultWarning(data, device.id, "Fault Warning"),
WiserRemoteAlarm(data, device.id, "Remote Alarm"),
]
)

async_add_entities(binary_sensors)
async_add_entities(binary_sensors, True)


class BaseBinarySensor(CoordinatorEntity, BinarySensorEntity):
Expand All @@ -51,15 +51,23 @@ def __init__(self, coordinator, device_id=0, sensor_type="") -> None:
self._device_id = device_id
self._device_name = None
self._sensor_type = sensor_type
self._room = self._data.wiserhub.rooms.get_by_device_id(self._device_id)
self._state = getattr(self._device, self._sensor_type.replace(" ", "_").lower())
_LOGGER.debug(
f"{self._data.wiserhub.system.name} {self.name} {'in room ' + self._room.name if self._room else ''} initalise" # noqa: E501
f"{self._data.wiserhub.system.name} {self.name} initalise" # noqa: E501
)

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
_LOGGER.debug(f"{self.name} device update requested")
self._device = self._data.wiserhub.devices.get_by_id(self._device_id)
self._state = getattr(self._device, self._sensor_type.replace(" ", "_").lower())
self.async_write_ha_state()

@property
def is_on(self):
"""Return the state of the sensor."""
return self._state

@property
def name(self):
Expand Down Expand Up @@ -89,12 +97,6 @@ class WiserSmokeAlarm(BaseBinarySensor):

_attr_device_class = BinarySensorDeviceClass.SMOKE

@property
def state(self):
"""Return the state of the sensor."""
_LOGGER.debug("%s device state requested", self.name)
return self._device.smoke_alarm

@property
def extra_state_attributes(self):
"""Return the state attributes of the battery."""
Expand All @@ -112,42 +114,18 @@ class WiserHeatAlarm(BaseBinarySensor):

_attr_device_class = BinarySensorDeviceClass.HEAT

@property
def state(self):
"""Return the state of the sensor."""
_LOGGER.debug("%s device state requested", self.name)
return self._device.heat_alarm


class WiserTamperAlarm(BaseBinarySensor):
"""Smoke Alarm sensor."""

_attr_device_class = BinarySensorDeviceClass.TAMPER

@property
def state(self):
"""Return the state of the sensor."""
_LOGGER.debug("%s device state requested", self.name)
return self._device.tamper_alarm


class WiserFaultWarning(BaseBinarySensor):
"""Smoke Alarm sensor."""

_attr_device_class = BinarySensorDeviceClass.PROBLEM

@property
def state(self):
"""Return the state of the sensor."""
_LOGGER.debug("%s device state requested", self.name)
return self._device.fault_warning


class WiserRemoteAlarm(BaseBinarySensor):
"""Smoke Alarm sensor."""

@property
def state(self):
"""Return the state of the sensor."""
_LOGGER.debug("%s device state requested", self.name)
return self._device.remote_alarm

0 comments on commit 81a74f0

Please sign in to comment.