Add low battery binary sensor to deCONZ integration (#64168)

* Make tamper sensor a property sensor

* Add low battery binary sensor as a binary property sensor

* Change according to review comment

* Use value_fn lambda

* Fix comparison

* Specific entity_description typing

* Minimize the code block affected by catching AttributeError

* Update homeassistant/components/deconz/binary_sensor.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update homeassistant/components/deconz/binary_sensor.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Avoid try statement

* Reflect review changes in sensor platform as well

* Store known sensor entities once per device

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Robert Svensson 2022-01-17 22:35:20 +01:00 committed by GitHub
parent 259befa65f
commit deed5f327c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 38 deletions

View file

@ -78,6 +78,7 @@ class DeconzSensorDescriptionMixin:
suffix: str
update_key: str
required_attr: str
value_fn: Callable[[PydeconzSensor], float | int | None]
@ -142,6 +143,7 @@ ENTITY_DESCRIPTIONS = {
SENSOR_DESCRIPTIONS = [
DeconzSensorDescription(
key="temperature",
required_attr="secondary_temperature",
value_fn=lambda device: device.secondary_temperature,
suffix="Temperature",
update_key="temperature",
@ -151,6 +153,7 @@ SENSOR_DESCRIPTIONS = [
),
DeconzSensorDescription(
key="air_quality_ppb",
required_attr="air_quality_ppb",
value_fn=lambda device: device.air_quality_ppb,
suffix="PPB",
update_key="airqualityppb",
@ -207,19 +210,18 @@ async def async_setup_entry(
):
entities.append(DeconzSensor(sensor, gateway))
known_sensor_entities = set(gateway.entities[DOMAIN])
for sensor_description in SENSOR_DESCRIPTIONS:
try:
if sensor_description.value_fn(sensor):
known_sensors = set(gateway.entities[DOMAIN])
new_sensor = DeconzPropertySensor(
sensor, gateway, sensor_description
)
if new_sensor.unique_id not in known_sensors:
entities.append(new_sensor)
except AttributeError:
if not hasattr(
sensor, sensor_description.required_attr
) or not sensor_description.value_fn(sensor):
continue
new_sensor = DeconzPropertySensor(sensor, gateway, sensor_description)
if new_sensor.unique_id not in known_sensor_entities:
entities.append(new_sensor)
if entities:
async_add_entities(entities)