2019-02-14 15:01:46 +00:00
|
|
|
"""Support for the Hive binary sensors."""
|
2021-02-09 21:03:49 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2020-09-12 16:07:13 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-14 18:59:17 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 16:07:13 +00:00
|
|
|
BinarySensorEntity,
|
2022-05-29 10:08:50 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-09-12 16:07:13 +00:00
|
|
|
)
|
2022-01-03 10:32:26 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from . import HiveEntity
|
2022-05-29 10:08:50 +00:00
|
|
|
from .const import DOMAIN
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2021-02-09 21:03:49 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=15)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
|
2022-05-29 10:08:50 +00:00
|
|
|
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="contactsensor", device_class=BinarySensorDeviceClass.OPENING
|
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="motionsensor",
|
|
|
|
device_class=BinarySensorDeviceClass.MOTION,
|
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="Connectivity",
|
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="SMOKE_CO",
|
|
|
|
device_class=BinarySensorDeviceClass.SMOKE,
|
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="DOG_BARK",
|
|
|
|
device_class=BinarySensorDeviceClass.SOUND,
|
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="GLASS_BREAK",
|
|
|
|
device_class=BinarySensorDeviceClass.SOUND,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 10:32:26 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-03-15 11:27:10 +00:00
|
|
|
"""Set up Hive thermostat based on a config entry."""
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
hive = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
devices = hive.session.deviceList.get("binary_sensor")
|
2021-02-09 21:03:49 +00:00
|
|
|
entities = []
|
|
|
|
if devices:
|
2022-05-29 10:08:50 +00:00
|
|
|
for description in BINARY_SENSOR_TYPES:
|
|
|
|
for dev in devices:
|
|
|
|
if dev["hiveType"] == description.key:
|
|
|
|
entities.append(HiveBinarySensorEntity(hive, dev, description))
|
2021-02-09 21:03:49 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class HiveBinarySensorEntity(HiveEntity, BinarySensorEntity):
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Representation of a Hive binary sensor."""
|
|
|
|
|
2022-05-29 10:08:50 +00:00
|
|
|
def __init__(self, hive, hive_device, entity_description):
|
|
|
|
"""Initialise hive binary sensor."""
|
|
|
|
super().__init__(hive, hive_device)
|
|
|
|
self.entity_description = entity_description
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-08-30 18:55:01 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Update all Node data from Hive."""
|
2021-02-09 21:03:49 +00:00
|
|
|
await self.hive.session.updateData(self.device)
|
2021-03-15 11:27:10 +00:00
|
|
|
self.device = await self.hive.sensor.getSensor(self.device)
|
2021-02-09 21:03:49 +00:00
|
|
|
self.attributes = self.device.get("attributes", {})
|
2022-05-29 10:08:50 +00:00
|
|
|
self._attr_is_on = self.device["status"]["state"]
|
|
|
|
if self.device["hiveType"] != "Connectivity":
|
|
|
|
self._attr_available = self.device["deviceData"].get("online")
|
|
|
|
else:
|
|
|
|
self._attr_available = True
|