Use built in polling for litterrobot update entity (#84678)

* Use built in polling

* Define scan interval
This commit is contained in:
Nathan Spencer 2022-12-28 12:30:48 -07:00 committed by GitHub
parent ba24cb6e72
commit 3f3ffe3060
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,7 @@
"""Support for Litter-Robot updates."""
from __future__ import annotations
from collections.abc import Callable
from datetime import datetime, timedelta
from datetime import timedelta
from typing import Any
from pylitterbot import LitterRobot4
@ -17,12 +16,12 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.start import async_at_start
from .const import DOMAIN
from .entity import LitterRobotEntity, LitterRobotHub
SCAN_INTERVAL = timedelta(days=1)
FIRMWARE_UPDATE_ENTITY = UpdateEntityDescription(
key="firmware",
name="Firmware",
@ -43,7 +42,7 @@ async def async_setup_entry(
for robot in robots
if isinstance(robot, LitterRobot4)
]
async_add_entities(entities)
async_add_entities(entities, True)
class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity):
@ -53,16 +52,6 @@ class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity):
UpdateEntityFeature.INSTALL | UpdateEntityFeature.PROGRESS
)
def __init__(
self,
robot: LitterRobot4,
hub: LitterRobotHub,
description: UpdateEntityDescription,
) -> None:
"""Initialize a Litter-Robot update entity."""
super().__init__(robot, hub, description)
self._poll_unsub: Callable[[], None] | None = None
@property
def installed_version(self) -> str:
"""Version installed and in use."""
@ -73,10 +62,13 @@ class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity):
"""Update installation progress."""
return self.robot.firmware_update_triggered
async def _async_update(self, _: HomeAssistant | datetime | None = None) -> None:
"""Update the entity."""
self._poll_unsub = None
@property
def should_poll(self) -> bool:
"""Set polling to True."""
return True
async def async_update(self) -> None:
"""Update the entity."""
if await self.robot.has_firmware_update():
latest_version = await self.robot.get_latest_firmware()
else:
@ -84,16 +76,6 @@ class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity):
if self._attr_latest_version != self.installed_version:
self._attr_latest_version = latest_version
self.async_write_ha_state()
self._poll_unsub = async_call_later(
self.hass, timedelta(days=1), self._async_update
)
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(async_at_start(self.hass, self._async_update))
async def async_install(
self, version: str | None, backup: bool, **kwargs: Any
@ -103,9 +85,3 @@ class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity):
if not await self.robot.update_firmware():
message = f"Unable to start firmware update on {self.robot.name}"
raise HomeAssistantError(message)
async def async_will_remove_from_hass(self) -> None:
"""Call when entity will be removed."""
if self._poll_unsub:
self._poll_unsub()
self._poll_unsub = None