2021-04-11 20:35:25 +00:00
|
|
|
"""Litter-Robot entities for common data and methods."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-17 09:29:56 +00:00
|
|
|
from typing import Generic, TypeVar
|
2021-04-11 20:35:25 +00:00
|
|
|
|
2022-08-30 16:14:06 +00:00
|
|
|
from pylitterbot import Robot
|
2022-09-17 09:29:56 +00:00
|
|
|
from pylitterbot.robot import EVENT_UPDATE
|
2021-04-11 20:35:25 +00:00
|
|
|
|
2022-09-17 09:29:56 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
2022-07-25 20:52:13 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2021-04-11 20:35:25 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .hub import LitterRobotHub
|
|
|
|
|
2022-08-30 16:14:06 +00:00
|
|
|
_RobotT = TypeVar("_RobotT", bound=Robot)
|
2021-04-11 20:35:25 +00:00
|
|
|
|
|
|
|
|
2022-08-30 16:14:06 +00:00
|
|
|
class LitterRobotEntity(
|
|
|
|
CoordinatorEntity[DataUpdateCoordinator[bool]], Generic[_RobotT]
|
|
|
|
):
|
2021-04-11 20:35:25 +00:00
|
|
|
"""Generic Litter-Robot entity representing common data and methods."""
|
|
|
|
|
2022-08-30 14:37:03 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2022-09-01 18:02:46 +00:00
|
|
|
def __init__(
|
|
|
|
self, robot: _RobotT, hub: LitterRobotHub, description: EntityDescription
|
|
|
|
) -> None:
|
2021-04-11 20:35:25 +00:00
|
|
|
"""Pass coordinator to CoordinatorEntity."""
|
|
|
|
super().__init__(hub.coordinator)
|
|
|
|
self.robot = robot
|
|
|
|
self.hub = hub
|
2022-09-01 18:02:46 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"{self.robot.serial}-{description.key}"
|
2021-04-11 20:35:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-01 22:37:19 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2021-04-11 20:35:25 +00:00
|
|
|
"""Return the device information for a Litter-Robot."""
|
2022-08-25 16:32:27 +00:00
|
|
|
assert self.robot.serial
|
2021-10-25 21:26:40 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self.robot.serial)},
|
|
|
|
manufacturer="Litter-Robot",
|
|
|
|
model=self.robot.model,
|
|
|
|
name=self.robot.name,
|
2022-08-29 15:48:24 +00:00
|
|
|
sw_version=getattr(self.robot, "firmware", None),
|
2021-10-25 21:26:40 +00:00
|
|
|
)
|
2021-04-11 20:35:25 +00:00
|
|
|
|
2022-09-17 09:29:56 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Set up a listener for the entity."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(self.robot.on(EVENT_UPDATE, self.async_write_ha_state))
|