Skip to content

Commit

Permalink
feat: icons and correct state of magnets
Browse files Browse the repository at this point in the history
BREAKING CHANGE: invert state of magnets to match presence
fixes #27
  • Loading branch information
petrleocompel committed Mar 10, 2023
1 parent 5582db5 commit 5469bd2
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions custom_components/hikvision_axpro/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,21 @@ def name(self) -> str | None:
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.async_write_ha_state()
if self.coordinator.zones and self.coordinator.zones[self.zone.id]:
value = self.coordinator.zones[self.zone.id].magnet_open_status
self._attr_state = STATE_ON if value is True else STATE_OFF
if value is True:
self._attr_state = STATE_OFF
self._attr_icon = "mdi:magnet-on"
elif value is False:
self._attr_state = STATE_ON
self._attr_icon = "mdi:magnet"
else:
self._attr_state = None
self._attr_icon = "mdi:help"
else:
self._attr_state = None
self._attr_icon = "mdi:help"
self.async_write_ha_state()

@property
def is_on(self) -> bool | None:
Expand All @@ -151,8 +160,6 @@ def __init__(self, coordinator: HikAxProDataUpdateCoordinator, zone: Zone, entry
self.zone = zone
self._ref_id = entry_id
self._attr_unique_id = f"{self.coordinator.device_name}-magnet-{zone.id}"
self._attr_icon = "mdi:magnet"
#self._attr_name = f"Magnet presence"
self._device_class = BinarySensorDeviceClass.PRESENCE
self._attr_has_entity_name = True
self.entity_id = f"{SENSOR_DOMAIN}.{coordinator.device_name}-magnet-{zone.id}"
Expand All @@ -164,12 +171,21 @@ def name(self) -> str | None:
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.async_write_ha_state()
if self.coordinator.zones and self.coordinator.zones[self.zone.id]:
value = self.coordinator.zones[self.zone.id].magnet_open_status
self._attr_state = STATE_ON if value is True else STATE_OFF
if value is True:
self._attr_state = STATE_OFF
self._attr_icon = "mdi:magnet-on"
elif value is False:
self._attr_state = STATE_ON
self._attr_icon = "mdi:magnet"
else:
self._attr_state = None
self._attr_icon = "mdi:help"
else:
self._attr_state = None
self._attr_icon = "mdi:help"
self.async_write_ha_state()

@property
def is_on(self) -> bool | None:
Expand Down

0 comments on commit 5469bd2

Please sign in to comment.