mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Add entity descriptions to binary sensors of Rituals Perfume Genie (#92485)
This commit is contained in:
parent
41515249a0
commit
a73a66bb0c
@ -1,9 +1,15 @@
|
||||
"""Support for Rituals Perfume Genie binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from pyrituals import Diffuser
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
@ -15,6 +21,33 @@ from .coordinator import RitualsDataUpdateCoordinator
|
||||
from .entity import DiffuserEntity
|
||||
|
||||
|
||||
@dataclass
|
||||
class RitualsentityDescriptionMixin:
|
||||
"""Mixin values for Rituals entities."""
|
||||
|
||||
is_on_fn: Callable[[Diffuser], bool]
|
||||
has_fn: Callable[[Diffuser], bool]
|
||||
|
||||
|
||||
@dataclass
|
||||
class RitualsBinarySensorEntityDescription(
|
||||
BinarySensorEntityDescription, RitualsentityDescriptionMixin
|
||||
):
|
||||
"""Class describing Rituals binary sensor entities."""
|
||||
|
||||
|
||||
ENTITY_DESCRIPTIONS = (
|
||||
RitualsBinarySensorEntityDescription(
|
||||
key="charging",
|
||||
name="Battery Charging",
|
||||
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
is_on_fn=lambda diffuser: diffuser.charging,
|
||||
has_fn=lambda diffuser: diffuser.has_battery,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
@ -26,25 +59,30 @@ async def async_setup_entry(
|
||||
]
|
||||
|
||||
async_add_entities(
|
||||
DiffuserBatteryChargingBinarySensor(coordinator)
|
||||
RitualsBinarySensorEntity(coordinator, description)
|
||||
for coordinator in coordinators.values()
|
||||
if coordinator.diffuser.has_battery
|
||||
for description in ENTITY_DESCRIPTIONS
|
||||
if description.has_fn(coordinator.diffuser)
|
||||
)
|
||||
|
||||
|
||||
class DiffuserBatteryChargingBinarySensor(DiffuserEntity, BinarySensorEntity):
|
||||
"""Representation of a diffuser battery charging binary sensor."""
|
||||
class RitualsBinarySensorEntity(DiffuserEntity, BinarySensorEntity):
|
||||
"""Defines a Rituals binary sensor entity."""
|
||||
|
||||
_attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
|
||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||
entity_description: RitualsBinarySensorEntityDescription
|
||||
|
||||
def __init__(self, coordinator: RitualsDataUpdateCoordinator) -> None:
|
||||
"""Initialize the battery charging binary sensor."""
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: RitualsDataUpdateCoordinator,
|
||||
description: RitualsBinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize Rituals binary sensor entity."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = f"{coordinator.diffuser.hublot}-charging"
|
||||
self._attr_name = f"{coordinator.diffuser.name} Battery Charging"
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{coordinator.diffuser.hublot}-{description.key}"
|
||||
self._attr_name = f"{coordinator.diffuser.name} {description.name}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return the state of the battery charging binary sensor."""
|
||||
return self.coordinator.diffuser.charging
|
||||
"""Return the state of the binary sensor."""
|
||||
return self.entity_description.is_on_fn(self.coordinator.diffuser)
|
||||
|
Loading…
x
Reference in New Issue
Block a user