Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1469,6 +1469,7 @@ omit =
homeassistant/components/yi/camera.py
homeassistant/components/yolink/__init__.py
homeassistant/components/yolink/api.py
homeassistant/components/yolink/binary_sensor.py
homeassistant/components/yolink/const.py
homeassistant/components/yolink/coordinator.py
homeassistant/components/yolink/entity.py
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/yolink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.SENSOR]
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
98 changes: 98 additions & 0 deletions homeassistant/components/yolink/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""YoLink BinarySensor."""
from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass

from yolink.device import YoLinkDevice

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 .const import ATTR_COORDINATOR, ATTR_DEVICE_DOOR_SENSOR, DOMAIN
from .coordinator import YoLinkCoordinator
from .entity import YoLinkEntity


@dataclass
class YoLinkBinarySensorEntityDescriptionMixin:
"""Mixin for device type."""

exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
Comment thread
MartinHjelmare marked this conversation as resolved.
Outdated


@dataclass
class YoLinkBinarySensorEntityDescription(
YoLinkBinarySensorEntityDescriptionMixin, BinarySensorEntityDescription
):
"""YoLink BinarySensorEntityDescription."""

value: Callable = lambda state: state
Comment thread
MartinHjelmare marked this conversation as resolved.
Outdated


SENSOR_DEVICE_TYPE = [ATTR_DEVICE_DOOR_SENSOR]

SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = (
YoLinkBinarySensorEntityDescription(
key="state",
icon="mdi:door",
device_class=BinarySensorDeviceClass.DOOR,
name="state",
value=lambda value: value == "open",
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_DOOR_SENSOR],
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up YoLink Sensor from a config entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATOR]
sensor_devices = [
device
for device in coordinator.yl_devices
if device.device_type in SENSOR_DEVICE_TYPE
]
entities = []
for sensor_device in sensor_devices:
for description in SENSOR_TYPES:
if description.exists_fn(sensor_device):
entities.append(
YoLinkBinarySensorEntity(coordinator, description, sensor_device)
)
async_add_entities(entities)


class YoLinkBinarySensorEntity(YoLinkEntity, BinarySensorEntity):
"""YoLink Sensor Entity."""

entity_description: YoLinkBinarySensorEntityDescription

def __init__(
self,
coordinator: YoLinkCoordinator,
description: YoLinkBinarySensorEntityDescription,
device: YoLinkDevice,
) -> None:
"""Init YoLink Sensor."""
super().__init__(coordinator, device)
self.entity_description = description
self._attr_unique_id = f"{device.device_id} {self.entity_description.key}"
self._attr_name = f"{device.device_name} ({self.entity_description.name})"

@callback
def update_entity_state(self, state: dict) -> None:
"""Update HA Entity State."""
self._attr_is_on = self.entity_description.value(
state[self.entity_description.key]
)
self.async_write_ha_state()