mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +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."""
|
"""Support for Rituals Perfume Genie binary sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from pyrituals import Diffuser
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
@ -15,6 +21,33 @@ from .coordinator import RitualsDataUpdateCoordinator
|
|||||||
from .entity import DiffuserEntity
|
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(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
@ -26,25 +59,30 @@ async def async_setup_entry(
|
|||||||
]
|
]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
DiffuserBatteryChargingBinarySensor(coordinator)
|
RitualsBinarySensorEntity(coordinator, description)
|
||||||
for coordinator in coordinators.values()
|
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):
|
class RitualsBinarySensorEntity(DiffuserEntity, BinarySensorEntity):
|
||||||
"""Representation of a diffuser battery charging binary sensor."""
|
"""Defines a Rituals binary sensor entity."""
|
||||||
|
|
||||||
_attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
|
entity_description: RitualsBinarySensorEntityDescription
|
||||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
||||||
|
|
||||||
def __init__(self, coordinator: RitualsDataUpdateCoordinator) -> None:
|
def __init__(
|
||||||
"""Initialize the battery charging binary sensor."""
|
self,
|
||||||
|
coordinator: RitualsDataUpdateCoordinator,
|
||||||
|
description: RitualsBinarySensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize Rituals binary sensor entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_unique_id = f"{coordinator.diffuser.hublot}-charging"
|
self.entity_description = description
|
||||||
self._attr_name = f"{coordinator.diffuser.name} Battery Charging"
|
self._attr_unique_id = f"{coordinator.diffuser.hublot}-{description.key}"
|
||||||
|
self._attr_name = f"{coordinator.diffuser.name} {description.name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return the state of the battery charging binary sensor."""
|
"""Return the state of the binary sensor."""
|
||||||
return self.coordinator.diffuser.charging
|
return self.entity_description.is_on_fn(self.coordinator.diffuser)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user