Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use built in polling for litterrobot update entity #84678

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 10 additions & 34 deletions homeassistant/components/litterrobot/update.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,12 +16,12 @@
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",
Expand All @@ -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)
natekspencer marked this conversation as resolved.
Show resolved Hide resolved


class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity):
Expand All @@ -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."""
Expand All @@ -73,27 +62,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
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

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 +85,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