core/homeassistant/components/hive/binary_sensor.py

96 lines
3.0 KiB
Python
Raw Normal View History

"""Support for the Hive binary sensors."""
from datetime import timedelta
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HiveEntity
from .const import ATTR_MODE, DOMAIN
2018-01-15 22:24:12 +00:00
DEVICETYPE = {
"contactsensor": BinarySensorDeviceClass.OPENING,
"motionsensor": BinarySensorDeviceClass.MOTION,
"Connectivity": BinarySensorDeviceClass.CONNECTIVITY,
"SMOKE_CO": BinarySensorDeviceClass.SMOKE,
"DOG_BARK": BinarySensorDeviceClass.SOUND,
"GLASS_BREAK": BinarySensorDeviceClass.SOUND,
}
PARALLEL_UPDATES = 0
SCAN_INTERVAL = timedelta(seconds=15)
2018-01-15 22:24:12 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Hive thermostat based on a config entry."""
2018-01-15 22:24:12 +00:00
hive = hass.data[DOMAIN][entry.entry_id]
devices = hive.session.deviceList.get("binary_sensor")
entities = []
if devices:
for dev in devices:
entities.append(HiveBinarySensorEntity(hive, dev))
async_add_entities(entities, True)
2018-01-15 22:24:12 +00:00
class HiveBinarySensorEntity(HiveEntity, BinarySensorEntity):
2018-01-15 22:24:12 +00:00
"""Representation of a Hive binary sensor."""
2019-01-09 04:15:12 +00:00
@property
def unique_id(self):
"""Return unique ID of entity."""
return self._unique_id
@property
def device_info(self) -> DeviceInfo:
2019-01-09 04:15:12 +00:00
"""Return device information."""
return DeviceInfo(
identifiers={(DOMAIN, self.device["device_id"])},
manufacturer=self.device["deviceData"]["manufacturer"],
2021-10-23 13:35:33 +00:00
model=self.device["deviceData"]["model"],
name=self.device["device_name"],
sw_version=self.device["deviceData"]["version"],
via_device=(DOMAIN, self.device["parentDevice"]),
)
2019-01-09 04:15:12 +00:00
2018-01-15 22:24:12 +00:00
@property
def device_class(self):
"""Return the class of this sensor."""
return DEVICETYPE.get(self.device["hiveType"])
2018-01-15 22:24:12 +00:00
@property
def name(self):
"""Return the name of the binary sensor."""
return self.device["haName"]
@property
def available(self):
"""Return if the device is available."""
if self.device["hiveType"] != "Connectivity":
return self.device["deviceData"]["online"]
return True
2018-01-15 22:24:12 +00:00
@property
def extra_state_attributes(self):
"""Show Device Attributes."""
return {
ATTR_MODE: self.attributes.get(ATTR_MODE),
}
2018-01-15 22:24:12 +00:00
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self.device["status"]["state"]
2018-01-15 22:24:12 +00:00
async def async_update(self):
2018-01-27 19:58:27 +00:00
"""Update all Node data from Hive."""
await self.hive.session.updateData(self.device)
self.device = await self.hive.sensor.getSensor(self.device)
self.attributes = self.device.get("attributes", {})