Code cleanup in litterrobot (#86037)

This commit is contained in:
Nathan Spencer
2023-01-16 12:58:30 -07:00
committed by GitHub
parent 5fbc005224
commit c6f60bf45d
8 changed files with 23 additions and 67 deletions

View File

@@ -1,15 +1,12 @@
"""Litter-Robot entities for common data and methods."""
from __future__ import annotations
from collections.abc import Iterable
from typing import Generic, TypeVar
from pylitterbot import Robot
from pylitterbot.robot import EVENT_UPDATE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
import homeassistant.helpers.entity_registry as er
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
@@ -37,9 +34,6 @@ class LitterRobotEntity(
self.hub = hub
self.entity_description = description
self._attr_unique_id = f"{self.robot.serial}-{description.key}"
# The following can be removed in 2022.12 after adjusting names in entities appropriately
if description.name is not None:
self._attr_name = description.name.capitalize()
@property
def device_info(self) -> DeviceInfo:
@@ -57,18 +51,3 @@ class LitterRobotEntity(
"""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))
def async_update_unique_id(
hass: HomeAssistant, domain: str, entities: Iterable[LitterRobotEntity[_RobotT]]
) -> None:
"""Update unique ID to be based on entity description key instead of name.
Introduced with release 2022.9.
"""
ent_reg = er.async_get(hass)
for entity in entities:
old_unique_id = f"{entity.robot.serial}-{entity.entity_description.name}"
if entity_id := ent_reg.async_get_entity_id(domain, DOMAIN, old_unique_id):
new_unique_id = f"{entity.robot.serial}-{entity.entity_description.key}"
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)