From 9a529b44986769dff265aaf44d96f83493c6bb5a Mon Sep 17 00:00:00 2001 From: Thomas5555 Date: Thu, 17 Feb 2022 16:39:19 +0000 Subject: [PATCH 1/3] add lifecyle hooks --- custom_components/husqvarna_automower/entity.py | 16 ++++++++++++---- .../husqvarna_automower/manifest.json | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/custom_components/husqvarna_automower/entity.py b/custom_components/husqvarna_automower/entity.py index f966eeaa..bd564fdd 100644 --- a/custom_components/husqvarna_automower/entity.py +++ b/custom_components/husqvarna_automower/entity.py @@ -23,10 +23,6 @@ def __init__(self, session, idx) -> None: self.mower_name = mower_attributes["system"]["name"] self.model = mower_attributes["system"]["model"] - self.session.register_cb( - lambda _: self.async_write_ha_state(), schedule_immediately=True - ) - self._available = self.get_mower_attributes()["metadata"]["connected"] self._event = None @@ -36,6 +32,18 @@ def get_mower_attributes(self) -> dict: """Get the mower attributes of the current mower.""" return self.session.data["data"][self.idx]["attributes"] + async def async_added_to_hass(self): + """Call when entity about to be added to Home Assistant.""" + await super().async_added_to_hass() + self.session.register_data_callback( + lambda _: self.async_write_ha_state(), schedule_immediately=True + ) + + async def async_will_remove_from_hass(self): + """Call when entity is being removed from Home Assistant.""" + await super().async_will_remove_from_hass() + self.session.unregister_data_callback(lambda _: self.async_write_ha_state()) + @property def device_info(self) -> DeviceInfo: """Defines the DeviceInfo for the mower.""" diff --git a/custom_components/husqvarna_automower/manifest.json b/custom_components/husqvarna_automower/manifest.json index ea7a1aae..e7b5f9c3 100644 --- a/custom_components/husqvarna_automower/manifest.json +++ b/custom_components/husqvarna_automower/manifest.json @@ -11,7 +11,7 @@ "@Thomas55555" ], "requirements": [ - "aioautomower==2022.2.0" + "aioautomower==2022.2.1" ], "iot_class": "cloud_push", "version": "0.0.0" From 432be1fbfaca33fa1714e8af2bdd618e05dea4a3 Mon Sep 17 00:00:00 2001 From: Thomas5555 Date: Thu, 17 Feb 2022 16:39:29 +0000 Subject: [PATCH 2/3] isort --- custom_components/husqvarna_automower/__init__.py | 1 - custom_components/husqvarna_automower/config_flow.py | 2 +- custom_components/husqvarna_automower/system_health.py | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/custom_components/husqvarna_automower/__init__.py b/custom_components/husqvarna_automower/__init__.py index 1d18671a..a5e32df5 100644 --- a/custom_components/husqvarna_automower/__init__.py +++ b/custom_components/husqvarna_automower/__init__.py @@ -2,7 +2,6 @@ import logging import aioautomower - from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME from homeassistant.core import HomeAssistant diff --git a/custom_components/husqvarna_automower/config_flow.py b/custom_components/husqvarna_automower/config_flow.py index 82f386aa..51e6ec57 100644 --- a/custom_components/husqvarna_automower/config_flow.py +++ b/custom_components/husqvarna_automower/config_flow.py @@ -1,10 +1,10 @@ """Config flow to add the integration via the UI.""" import logging -from aioautomower import GetAccessToken, GetMowerData, TokenError from aiohttp.client_exceptions import ClientConnectorError, ClientResponseError import voluptuous as vol +from aioautomower import GetAccessToken, GetMowerData, TokenError from homeassistant import config_entries from homeassistant.const import ( CONF_ACCESS_TOKEN, diff --git a/custom_components/husqvarna_automower/system_health.py b/custom_components/husqvarna_automower/system_health.py index 62f05bba..ff862ae9 100644 --- a/custom_components/husqvarna_automower/system_health.py +++ b/custom_components/husqvarna_automower/system_health.py @@ -1,6 +1,5 @@ """Provide info to system health.""" from aioautomower import TOKEN_URL - from homeassistant.components.system_health import ( SystemHealthRegistration, async_check_can_reach_url, From 4077e610712af3d4b0ec8469c370da69072afe05 Mon Sep 17 00:00:00 2001 From: Thomas5555 Date: Thu, 17 Feb 2022 16:43:27 +0000 Subject: [PATCH 3/3] add missing TypeHints --- custom_components/husqvarna_automower/entity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/husqvarna_automower/entity.py b/custom_components/husqvarna_automower/entity.py index bd564fdd..bf510f92 100644 --- a/custom_components/husqvarna_automower/entity.py +++ b/custom_components/husqvarna_automower/entity.py @@ -32,14 +32,14 @@ def get_mower_attributes(self) -> dict: """Get the mower attributes of the current mower.""" return self.session.data["data"][self.idx]["attributes"] - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity about to be added to Home Assistant.""" await super().async_added_to_hass() self.session.register_data_callback( lambda _: self.async_write_ha_state(), schedule_immediately=True ) - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Call when entity is being removed from Home Assistant.""" await super().async_will_remove_from_hass() self.session.unregister_data_callback(lambda _: self.async_write_ha_state())