Skip to content

Commit

Permalink
Use built in polling
Browse files Browse the repository at this point in the history
  • Loading branch information
natekspencer committed Dec 28, 2022
1 parent ba24cb6 commit 1c3f620
Showing 1 changed file with 7 additions and 34 deletions.
41 changes: 7 additions & 34 deletions homeassistant/components/litterrobot/update.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Support for Litter-Robot updates."""
from __future__ import annotations

from collections.abc import Callable
from datetime import datetime, timedelta
from typing import Any

from pylitterbot import LitterRobot4
Expand All @@ -17,8 +15,6 @@
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
Expand All @@ -43,7 +39,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):
Expand All @@ -53,16 +49,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."""
Expand All @@ -73,27 +59,20 @@ def in_progress(self) -> bool:
"""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:
latest_version = self.installed_version

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
Expand All @@ -103,9 +82,3 @@ async def async_install(
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

0 comments on commit 1c3f620

Please sign in to comment.