From cf6442fe0b5e2802d0a01e3217179635d7668b74 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 13 Feb 2023 11:13:20 +0100 Subject: [PATCH 1/3] Add dormakaba_dkey door and dead bolt binary sensors --- .coveragerc | 1 + .../dormakaba_dkey/binary_sensor.py | 88 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 homeassistant/components/dormakaba_dkey/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 5f820665db90a6..957ec8620cb807 100644 --- a/.coveragerc +++ b/.coveragerc @@ -213,6 +213,7 @@ omit = homeassistant/components/doorbird/entity.py homeassistant/components/doorbird/util.py homeassistant/components/dormakaba_dkey/__init__.py + homeassistant/components/dormakaba_dkey/binary_sensor.py homeassistant/components/dormakaba_dkey/entity.py homeassistant/components/dormakaba_dkey/lock.py homeassistant/components/dormakaba_dkey/sensor.py diff --git a/homeassistant/components/dormakaba_dkey/binary_sensor.py b/homeassistant/components/dormakaba_dkey/binary_sensor.py new file mode 100644 index 00000000000000..d4200260b67590 --- /dev/null +++ b/homeassistant/components/dormakaba_dkey/binary_sensor.py @@ -0,0 +1,88 @@ +"""Dormakaba dKey integration binary sensor platform.""" +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from py_dormakaba_dkey import DKEYLock +from py_dormakaba_dkey.commands import DoorPosition, Notifications, UnlockStatus + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + +from .const import DOMAIN +from .entity import DormakabaDkeyEntity +from .models import DormakabaDkeyData + + +@dataclass +class DormakabaDkeyBinarySensorDescriptionMixin: + """Class for keys required by Dormakaba dKey binary sensor entity.""" + + is_on: Callable[[Notifications], bool] + + +@dataclass +class DormakabaDkeyBinarySensorDescription( + BinarySensorEntityDescription, DormakabaDkeyBinarySensorDescriptionMixin +): + """Describes Dormakaba dKey binary sensor entity.""" + + +BINARY_SENSOR_DESCRIPTIONS = ( + DormakabaDkeyBinarySensorDescription( + key="door_position", + name="Door", + device_class=BinarySensorDeviceClass.DOOR, + is_on=lambda state: state.door_position == DoorPosition.OPEN, + ), + DormakabaDkeyBinarySensorDescription( + key="security_locked", + name="Security locked", + device_class=BinarySensorDeviceClass.LOCK, + is_on=lambda state: state.unlock_status != UnlockStatus.SECURITY_LOCKED, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the lock platform for Dormakaba dKey.""" + data: DormakabaDkeyData = hass.data[DOMAIN][entry.entry_id] + async_add_entities( + DormakabaDkeyBinarySensor(data.coordinator, data.lock, description) + for description in BINARY_SENSOR_DESCRIPTIONS + ) + + +class DormakabaDkeyBinarySensor(DormakabaDkeyEntity, BinarySensorEntity): + """Dormakaba dKey binary sensor.""" + + _attr_has_entity_name = True + entity_description: DormakabaDkeyBinarySensorDescription + + def __init__( + self, + coordinator: DataUpdateCoordinator[None], + lock: DKEYLock, + description: DormakabaDkeyBinarySensorDescription, + ) -> None: + """Initialize a Dormakaba dKey binary sensor.""" + self.entity_description = description + self._attr_unique_id = f"{lock.address}_{description.key}" + super().__init__(coordinator, lock) + + @callback + def _async_update_attrs(self) -> None: + """Handle updating _attr values.""" + self._attr_is_on = self.entity_description.is_on(self._lock.state) From fe302f3c85a277a8018bb9b4111962ea2c2969de Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 13 Feb 2023 11:16:50 +0100 Subject: [PATCH 2/3] Rename dead bolt sensor --- homeassistant/components/dormakaba_dkey/binary_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/dormakaba_dkey/binary_sensor.py b/homeassistant/components/dormakaba_dkey/binary_sensor.py index d4200260b67590..84db962335e170 100644 --- a/homeassistant/components/dormakaba_dkey/binary_sensor.py +++ b/homeassistant/components/dormakaba_dkey/binary_sensor.py @@ -45,7 +45,7 @@ class DormakabaDkeyBinarySensorDescription( ), DormakabaDkeyBinarySensorDescription( key="security_locked", - name="Security locked", + name="Dead bolt", device_class=BinarySensorDeviceClass.LOCK, is_on=lambda state: state.unlock_status != UnlockStatus.SECURITY_LOCKED, ), From 0c191c2b309086e980432995f51dcfdf6af16964 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Tue, 14 Feb 2023 11:42:01 +0100 Subject: [PATCH 3/3] Fix docstring --- homeassistant/components/dormakaba_dkey/binary_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/dormakaba_dkey/binary_sensor.py b/homeassistant/components/dormakaba_dkey/binary_sensor.py index 84db962335e170..95e26a3eeb3515 100644 --- a/homeassistant/components/dormakaba_dkey/binary_sensor.py +++ b/homeassistant/components/dormakaba_dkey/binary_sensor.py @@ -57,7 +57,7 @@ async def async_setup_entry( entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: - """Set up the lock platform for Dormakaba dKey.""" + """Set up the binary sensor platform for Dormakaba dKey.""" data: DormakabaDkeyData = hass.data[DOMAIN][entry.entry_id] async_add_entities( DormakabaDkeyBinarySensor(data.coordinator, data.lock, description)