Refactor of Hue integration with full V2 support (#58996)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Marcel van der Veldt 2021-11-16 20:59:17 +01:00 committed by GitHub
parent 4642a70651
commit e1e6925097
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 7146 additions and 2255 deletions

View file

@ -1,56 +1,24 @@
"""Hue binary sensor entities."""
from aiohue.sensors import TYPE_ZLL_PRESENCE
"""Support for Hue binary sensors."""
from __future__ import annotations
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN as HUE_DOMAIN
from .sensor_base import SENSOR_CONFIG_MAP, GenericZLLSensor
PRESENCE_NAME_FORMAT = "{} motion"
from .bridge import HueBridge
from .const import DOMAIN
from .v1.binary_sensor import async_setup_entry as setup_entry_v1
from .v2.binary_sensor import async_setup_entry as setup_entry_v2
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Defer binary sensor setup to the shared sensor module."""
bridge = hass.data[HUE_DOMAIN][config_entry.entry_id]
if not bridge.sensor_manager:
return
await bridge.sensor_manager.async_register_component(
"binary_sensor", async_add_entities
)
class HuePresence(GenericZLLSensor, BinarySensorEntity):
"""The presence sensor entity for a Hue motion sensor device."""
_attr_device_class = DEVICE_CLASS_MOTION
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self.sensor.presence
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = super().extra_state_attributes
if "sensitivity" in self.sensor.config:
attributes["sensitivity"] = self.sensor.config["sensitivity"]
if "sensitivitymax" in self.sensor.config:
attributes["sensitivity_max"] = self.sensor.config["sensitivitymax"]
return attributes
SENSOR_CONFIG_MAP.update(
{
TYPE_ZLL_PRESENCE: {
"platform": "binary_sensor",
"name_format": PRESENCE_NAME_FORMAT,
"class": HuePresence,
}
}
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up binary sensor entities."""
bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
if bridge.api_version == 1:
await setup_entry_v1(hass, config_entry, async_add_entities)
else:
await setup_entry_v2(hass, config_entry, async_add_entities)