Skip to content

Commit 590ef9e

Browse files
committed
feat: add "Wired magnet contact" fixes #2
1 parent 7defb17 commit 590ef9e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

custom_components/hikvision_axpro/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class DetectorType(Enum):
6464
WIRELESS_TEMPERATURE_HUMIDITY_DETECTOR = "wirelessTemperatureHumidityDetector"
6565
WIRELESS_GLASS_BREAK_DETECTOR = "wirelessGlassBreakDetector"
6666
WIRELESS_PIR_AM_CURTAIN_DETECTOR = "wirelessDTAMCurtainDetector"
67+
WIRED_MAGNETIC_CONTACT = "magneticContact"
6768

6869

6970
def detector_model_to_name(model_id: Optional[str]) -> str:

custom_components/hikvision_axpro/sensor.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ async def async_setup_entry(
4747
)
4848
if zone.zone.detector_type == DetectorType.WIRELESS_EXTERNAL_MAGNET_DETECTOR:
4949
devices.append(HikWirelessExtMagnetDetector(coordinator, zone.zone, entry.entry_id))
50+
if zone.zone.detector_type == DetectorType.WIRED_MAGNETIC_CONTACT:
51+
devices.append(HikMagneticContactDetector(coordinator, zone.zone, entry.entry_id))
5052
if zone.zone.temperature is not None:
5153
devices.append(HikTemperature(coordinator, zone.zone, entry.entry_id))
5254
if zone.zone.detector_type == DetectorType.WIRELESS_TEMPERATURE_HUMIDITY_DETECTOR:
@@ -113,6 +115,46 @@ def is_on(self) -> bool | None:
113115
return False
114116

115117

118+
class HikMagneticContactDetector(CoordinatorEntity, HikDevice, BinarySensorEntity):
119+
"""Representation of Hikvision external magnet detector."""
120+
coordinator: HikAxProDataUpdateCoordinator
121+
122+
def __init__(self, coordinator: HikAxProDataUpdateCoordinator, zone: Zone, entry_id: str) -> None:
123+
"""Create the entity with a DataUpdateCoordinator."""
124+
super().__init__(coordinator)
125+
self.zone = zone
126+
self._ref_id = entry_id
127+
self._attr_unique_id = f"{self.coordinator.device_name}-magnet-{zone.id}"
128+
self._attr_icon = "mdi:magnet"
129+
#self._attr_name = f"Magnet presence"
130+
self._device_class = BinarySensorDeviceClass.PRESENCE
131+
self._attr_has_entity_name = True
132+
self.entity_id = f"{SENSOR_DOMAIN}.{coordinator.device_name}-magnet-{zone.id}"
133+
134+
@property
135+
def name(self) -> str | None:
136+
return "Magnet presence"
137+
138+
@callback
139+
def _handle_coordinator_update(self) -> None:
140+
"""Handle updated data from the coordinator."""
141+
self.async_write_ha_state()
142+
if self.coordinator.zones and self.coordinator.zones[self.zone.id]:
143+
value = self.coordinator.zones[self.zone.id].magnet_open_status
144+
self._attr_state = STATE_ON if value is True else STATE_OFF
145+
else:
146+
self._attr_state = None
147+
148+
@property
149+
def is_on(self) -> bool | None:
150+
"""Return true if the binary sensor is on."""
151+
if self.coordinator.zones and self.coordinator.zones[self.zone.id]:
152+
value = self.coordinator.zones[self.zone.id].status
153+
return value == Status.ONLINE
154+
else:
155+
return False
156+
157+
116158
class HikTemperature(CoordinatorEntity, HikDevice, SensorEntity):
117159
"""Representation of Hikvision external magnet detector."""
118160
coordinator: HikAxProDataUpdateCoordinator

0 commit comments

Comments
 (0)