Add door state sensors to tedee (#147386)

This commit is contained in:
Josef Zweck 2025-06-24 13:30:13 +02:00 committed by GitHub
parent 63ac14a19b
commit 23b90f5984
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 4 deletions

View File

@ -4,7 +4,7 @@ from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from aiotedee import TedeeLock from aiotedee import TedeeLock
from aiotedee.lock import TedeeLockState from aiotedee.lock import TedeeDoorState, TedeeLockState
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
@ -29,6 +29,8 @@ class TedeeBinarySensorEntityDescription(
"""Describes Tedee binary sensor entity.""" """Describes Tedee binary sensor entity."""
is_on_fn: Callable[[TedeeLock], bool | None] is_on_fn: Callable[[TedeeLock], bool | None]
supported_fn: Callable[[TedeeLock], bool] = lambda _: True
available_fn: Callable[[TedeeLock], bool] = lambda _: True
ENTITIES: tuple[TedeeBinarySensorEntityDescription, ...] = ( ENTITIES: tuple[TedeeBinarySensorEntityDescription, ...] = (
@ -61,6 +63,14 @@ ENTITIES: tuple[TedeeBinarySensorEntityDescription, ...] = (
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
TedeeBinarySensorEntityDescription(
key="door_state",
is_on_fn=lambda lock: lock.door_state is TedeeDoorState.OPENED,
device_class=BinarySensorDeviceClass.DOOR,
supported_fn=lambda lock: lock.door_state is not TedeeDoorState.NOT_PAIRED,
available_fn=lambda lock: lock.door_state
not in [TedeeDoorState.UNCALIBRATED, TedeeDoorState.DISCONNECTED],
),
) )
@ -77,6 +87,7 @@ async def async_setup_entry(
TedeeBinarySensorEntity(lock, coordinator, entity_description) TedeeBinarySensorEntity(lock, coordinator, entity_description)
for entity_description in ENTITIES for entity_description in ENTITIES
for lock in locks for lock in locks
if entity_description.supported_fn(lock)
) )
coordinator.new_lock_callbacks.append(_async_add_new_lock) coordinator.new_lock_callbacks.append(_async_add_new_lock)
@ -92,3 +103,8 @@ class TedeeBinarySensorEntity(TedeeDescriptionEntity, BinarySensorEntity):
def is_on(self) -> bool | None: def is_on(self) -> bool | None:
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return self.entity_description.is_on_fn(self._lock) return self.entity_description.is_on_fn(self._lock)
@property
def available(self) -> bool:
"""Return true if the binary sensor is available."""
return self.entity_description.available_fn(self._lock) and super().available

View File

@ -9,7 +9,8 @@
"is_charging": false, "is_charging": false,
"state_change_result": 0, "state_change_result": 0,
"is_enabled_pullspring": 1, "is_enabled_pullspring": 1,
"duration_pullspring": 2 "duration_pullspring": 2,
"door_state": 0
}, },
{ {
"lock_name": "Lock-2C3D", "lock_name": "Lock-2C3D",
@ -21,6 +22,7 @@
"is_charging": false, "is_charging": false,
"state_change_result": 0, "state_change_result": 0,
"is_enabled_pullspring": 0, "is_enabled_pullspring": 0,
"duration_pullspring": 0 "duration_pullspring": 0,
"door_state": 2
} }
] ]

View File

@ -242,6 +242,55 @@
'state': 'off', 'state': 'off',
}) })
# --- # ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_door-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.lock_2c3d_door',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.DOOR: 'door'>,
'original_icon': None,
'original_name': 'Door',
'platform': 'tedee',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '98765-door_state',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_door-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'door',
'friendly_name': 'Lock-2C3D Door',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_2c3d_door',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_lock_uncalibrated-entry] # name: test_binary_sensors[binary_sensor.lock_2c3d_lock_uncalibrated-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({

View File

@ -17,7 +17,7 @@
}), }),
'1': dict({ '1': dict({
'battery_level': 70, 'battery_level': 70,
'door_state': 0, 'door_state': 2,
'duration_pullspring': 0, 'duration_pullspring': 0,
'is_charging': False, 'is_charging': False,
'is_connected': True, 'is_connected': True,