Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 88 additions & 0 deletions homeassistant/components/dormakaba_dkey/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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="Dead bolt",
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 binary sensor 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)