Files
homeassistant-core/homeassistant/components/point/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
4.6 KiB
Python
Raw Normal View History

2019-02-14 05:35:12 +01:00
"""Support for Minut Point binary sensors."""
2022-01-24 13:57:29 +01:00
from __future__ import annotations
2018-11-19 12:52:21 +01:00
import logging
2021-03-23 20:03:54 +01:00
from pypoint import EVENTS
from homeassistant.components.binary_sensor import (
DOMAIN,
2021-12-16 16:29:54 -05:00
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
2018-11-19 12:52:21 +01:00
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2018-11-19 12:52:21 +01:00
from . import MinutPointEntity
from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW, SIGNAL_WEBHOOK
2018-11-19 12:52:21 +01:00
_LOGGER = logging.getLogger(__name__)
2021-03-23 20:03:54 +01:00
DEVICES = {
"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
}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> 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
2018-12-03 16:50:05 +01:00
async def async_discover_sensor(device_id):
"""Discover and add a discovered sensor."""
client = hass.data[POINT_DOMAIN][config_entry.entry_id]
async_add_entities(
(
2021-03-23 20:03:54 +01:00
MinutPointBinarySensor(client, device_id, device_name)
for device_name in DEVICES
if device_name in EVENTS
2018-12-03 16:50:05 +01:00
),
True,
)
async_dispatcher_connect(
2018-12-13 16:40:56 +01:00
hass, POINT_DISCOVERY_NEW.format(DOMAIN, POINT_DOMAIN), async_discover_sensor
2018-12-03 16:50:05 +01:00
)
2018-11-19 12:52:21 +01:00
class MinutPointBinarySensor(MinutPointEntity, BinarySensorEntity):
2018-11-19 12:52:21 +01:00
"""The platform class required by Home Assistant."""
2021-03-23 20:03:54 +01:00
def __init__(self, point_client, device_id, device_name):
2019-02-14 05:35:12 +01:00
"""Initialize the binary sensor."""
2021-03-23 20:03:54 +01:00
super().__init__(
point_client,
device_id,
DEVICES[device_name].get("device_class", device_name),
2021-03-23 20:03:54 +01:00
)
self._device_name = device_name
2018-11-19 12:52:21 +01:00
self._async_unsub_hook_dispatcher_connect = None
2021-03-23 20:03:54 +01:00
self._events = EVENTS[device_name]
2023-09-12 17:15:36 +02:00
self._attr_unique_id = f"point.{device_id}-{device_name}"
self._attr_icon = DEVICES[self._device_name].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()
self._async_unsub_hook_dispatcher_connect = async_dispatcher_connect(
self.hass, SIGNAL_WEBHOOK, self._webhook_event
)
2022-09-06 09:51:33 +02:00
async def async_will_remove_from_hass(self) -> None:
2018-11-19 12:52:21 +01:00
"""Disconnect dispatcher listener when removed."""
await super().async_will_remove_from_hass()
if self._async_unsub_hook_dispatcher_connect:
self._async_unsub_hook_dispatcher_connect()
2019-02-12 17:15:02 +01:00
async def _update_callback(self):
2018-11-19 12:52:21 +01:00
"""Update the value of the sensor."""
if not self.is_updated:
return
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
2020-04-01 14:19:51 -07:00
self.async_write_ha_state()
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()