2019-02-14 05:35:12 +01:00
|
|
|
"""Support for Minut Point binary sensors."""
|
2024-03-08 15:04:07 +01:00
|
|
|
|
2022-01-24 13:57:29 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2018-11-19 12:52:21 +01:00
|
|
|
import logging
|
2025-03-30 20:58:40 +02:00
|
|
|
from typing import Any
|
2018-11-19 12:52:21 +01:00
|
|
|
|
2021-03-23 20:03:54 +01:00
|
|
|
from pypoint import EVENTS
|
|
|
|
|
|
2020-09-12 20:21:57 +02:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 16:29:54 -05:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 20:21:57 +02:00
|
|
|
BinarySensorEntity,
|
|
|
|
|
)
|
2022-01-03 11:35:02 +01:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2018-11-19 12:52:21 +01:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2025-02-10 21:08:03 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
2018-11-19 12:52:21 +01:00
|
|
|
|
2025-03-30 20:58:40 +02:00
|
|
|
from .const import SIGNAL_WEBHOOK
|
2026-01-12 16:34:38 +01:00
|
|
|
from .coordinator import PointConfigEntry, PointDataUpdateCoordinator
|
2024-09-23 15:58:20 +02:00
|
|
|
from .entity import MinutPointEntity
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2018-11-19 12:52:21 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
2021-03-23 20:03:54 +01:00
|
|
|
|
2025-03-30 20:58:40 +02:00
|
|
|
DEVICES: dict[str, Any] = {
|
2021-03-23 20:03:54 +01:00
|
|
|
"alarm": {"icon": "mdi:alarm-bell"},
|
2021-12-16 16:29:54 -05:00
|
|
|
"battery": {"device_class": BinarySensorDeviceClass.BATTERY},
|
2021-03-23 20:03:54 +01:00
|
|
|
"button_press": {"icon": "mdi:gesture-tap-button"},
|
2021-12-16 16:29:54 -05:00
|
|
|
"cold": {"device_class": BinarySensorDeviceClass.COLD},
|
|
|
|
|
"connectivity": {"device_class": BinarySensorDeviceClass.CONNECTIVITY},
|
2021-03-23 20:03:54 +01:00
|
|
|
"dry": {"icon": "mdi:water"},
|
|
|
|
|
"glass": {"icon": "mdi:window-closed-variant"},
|
2021-12-16 16:29:54 -05:00
|
|
|
"heat": {"device_class": BinarySensorDeviceClass.HEAT},
|
|
|
|
|
"moisture": {"device_class": BinarySensorDeviceClass.MOISTURE},
|
|
|
|
|
"motion": {"device_class": BinarySensorDeviceClass.MOTION},
|
2021-03-23 20:03:54 +01:00
|
|
|
"noise": {"icon": "mdi:volume-high"},
|
2021-12-16 16:29:54 -05:00
|
|
|
"sound": {"device_class": BinarySensorDeviceClass.SOUND},
|
2021-03-23 20:03:54 +01:00
|
|
|
"tamper_old": {"icon": "mdi:shield-alert"},
|
|
|
|
|
"tamper": {"icon": "mdi:shield-alert"},
|
2018-11-19 12:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-03 11:35:02 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
|
hass: HomeAssistant,
|
2025-03-30 20:58:40 +02:00
|
|
|
config_entry: PointConfigEntry,
|
2025-02-10 21:08:03 +01:00
|
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
2022-01-03 11:35:02 +01:00
|
|
|
) -> None:
|
2018-11-19 12:52:21 +01:00
|
|
|
"""Set up a Point's binary sensors based on a config entry."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2025-03-30 20:58:40 +02:00
|
|
|
coordinator = config_entry.runtime_data
|
|
|
|
|
|
|
|
|
|
def async_discover_sensor(device_id: str) -> None:
|
2018-12-03 16:50:05 +01:00
|
|
|
"""Discover and add a discovered sensor."""
|
|
|
|
|
async_add_entities(
|
2025-03-30 20:58:40 +02:00
|
|
|
MinutPointBinarySensor(coordinator, device_id, device_name)
|
|
|
|
|
for device_name in DEVICES
|
|
|
|
|
if device_name in EVENTS
|
2018-12-03 16:50:05 +01:00
|
|
|
)
|
|
|
|
|
|
2025-03-30 20:58:40 +02:00
|
|
|
coordinator.new_device_callbacks.append(async_discover_sensor)
|
|
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
|
MinutPointBinarySensor(coordinator, device_id, device_name)
|
|
|
|
|
for device_name in DEVICES
|
|
|
|
|
if device_name in EVENTS
|
|
|
|
|
for device_id in coordinator.point.device_ids
|
2018-12-03 16:50:05 +01:00
|
|
|
)
|
2018-11-19 12:52:21 +01:00
|
|
|
|
|
|
|
|
|
2020-04-23 21:57:07 +02:00
|
|
|
class MinutPointBinarySensor(MinutPointEntity, BinarySensorEntity):
|
2018-11-19 12:52:21 +01:00
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
|
2025-03-30 20:58:40 +02:00
|
|
|
def __init__(
|
|
|
|
|
self, coordinator: PointDataUpdateCoordinator, device_id: str, key: str
|
|
|
|
|
) -> None:
|
2019-02-14 05:35:12 +01:00
|
|
|
"""Initialize the binary sensor."""
|
2025-03-30 20:58:40 +02:00
|
|
|
self._attr_device_class = DEVICES[key].get("device_class", key)
|
|
|
|
|
super().__init__(coordinator, device_id)
|
|
|
|
|
self._device_name = key
|
|
|
|
|
self._events = EVENTS[key]
|
|
|
|
|
self._attr_unique_id = f"point.{device_id}-{key}"
|
|
|
|
|
self._attr_icon = DEVICES[key].get("icon")
|
2018-11-19 12:52:21 +01:00
|
|
|
|
2022-09-06 09:51:33 +02:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-02-14 05:35:12 +01:00
|
|
|
"""Call when entity is added to HOme Assistant."""
|
2018-11-19 12:52:21 +01:00
|
|
|
await super().async_added_to_hass()
|
2025-03-30 20:58:40 +02:00
|
|
|
self.async_on_remove(
|
|
|
|
|
async_dispatcher_connect(self.hass, SIGNAL_WEBHOOK, self._webhook_event)
|
2018-11-19 12:52:21 +01:00
|
|
|
)
|
|
|
|
|
|
2025-03-30 20:58:40 +02:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
2018-11-19 12:52:21 +01:00
|
|
|
"""Update the value of the sensor."""
|
2022-01-24 13:57:29 +01:00
|
|
|
if self.device_class == BinarySensorDeviceClass.CONNECTIVITY:
|
|
|
|
|
# connectivity is the other way around.
|
2023-02-03 16:18:33 +01:00
|
|
|
self._attr_is_on = self._events[0] not in self.device.ongoing_events
|
2018-11-19 12:52:21 +01:00
|
|
|
else:
|
2022-01-24 13:57:29 +01:00
|
|
|
self._attr_is_on = self._events[0] in self.device.ongoing_events
|
2025-03-30 20:58:40 +02:00
|
|
|
super()._handle_coordinator_update()
|
2018-11-19 12:52:21 +01:00
|
|
|
|
|
|
|
|
@callback
|
|
|
|
|
def _webhook_event(self, data, webhook):
|
|
|
|
|
"""Process new event from the webhook."""
|
|
|
|
|
if self.device.webhook != webhook:
|
|
|
|
|
return
|
|
|
|
|
_type = data.get("event", {}).get("type")
|
2019-02-14 05:27:17 +01:00
|
|
|
_device_id = data.get("event", {}).get("device_id")
|
|
|
|
|
if _type not in self._events or _device_id != self.device.device_id:
|
2018-11-19 12:52:21 +01:00
|
|
|
return
|
2019-08-03 00:20:07 +03:00
|
|
|
_LOGGER.debug("Received webhook: %s", _type)
|
2018-11-19 12:52:21 +01:00
|
|
|
if _type == self._events[0]:
|
2022-01-24 13:57:29 +01:00
|
|
|
_is_on = True
|
|
|
|
|
elif _type == self._events[1]:
|
|
|
|
|
_is_on = False
|
|
|
|
|
else:
|
|
|
|
|
return
|
2018-11-19 12:52:21 +01:00
|
|
|
|
2021-12-16 16:29:54 -05:00
|
|
|
if self.device_class == BinarySensorDeviceClass.CONNECTIVITY:
|
2018-11-19 12:52:21 +01:00
|
|
|
# connectivity is the other way around.
|
2022-01-24 13:57:29 +01:00
|
|
|
self._attr_is_on = not _is_on
|
|
|
|
|
else:
|
|
|
|
|
self._attr_is_on = _is_on
|
|
|
|
|
self.async_write_ha_state()
|