Skip to content

Commit

Permalink
Add entity descriptions to binary sensors of Rituals Perfume Genie (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored May 4, 2023
1 parent 4151524 commit a73a66b
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions homeassistant/components/rituals_perfume_genie/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,6 +21,33 @@
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,
Expand All @@ -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)

0 comments on commit a73a66b

Please sign in to comment.