From 6ffb7a4901a553dc5abb12d0771206f33b4daf4c Mon Sep 17 00:00:00 2001 From: Niek Schoemaker <32094562+niekschoemaker@users.noreply.github.com> Date: Sun, 29 Dec 2024 21:03:40 +0100 Subject: [PATCH] Remove deprecated typing and fix thread safety issue --- custom_components/hon/__init__.py | 6 +++--- custom_components/hon/binary_sensor.py | 7 +++---- custom_components/hon/button.py | 8 ++++---- custom_components/hon/climate.py | 14 +++++++------- custom_components/hon/entity.py | 5 ++--- custom_components/hon/fan.py | 9 ++++----- custom_components/hon/light.py | 9 ++++----- custom_components/hon/lock.py | 5 ++--- custom_components/hon/number.py | 13 ++++++------- custom_components/hon/select.py | 9 ++++----- custom_components/hon/sensor.py | 15 ++++++++++----- custom_components/hon/switch.py | 9 ++++----- 12 files changed, 53 insertions(+), 56 deletions(-) diff --git a/custom_components/hon/__init__.py b/custom_components/hon/__init__.py index 1e38a2d..52ecfd1 100644 --- a/custom_components/hon/__init__.py +++ b/custom_components/hon/__init__.py @@ -6,8 +6,8 @@ import voluptuous as vol # type: ignore[import-untyped] from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.helpers import config_validation as cv, aiohttp_client -from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator +from homeassistant.core import HomeAssistant from pyhon import Hon from .const import DOMAIN, PLATFORMS, MOBILE_ID, CONF_REFRESH_TOKEN @@ -27,7 +27,7 @@ CONFIG_SCHEMA = vol.Schema( ) -async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: session = aiohttp_client.async_get_clientsession(hass) if (config_dir := hass.config.config_dir) is None: raise ValueError("Missing Config Dir") @@ -60,7 +60,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool return True -async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: refresh_token = hass.data[DOMAIN][entry.unique_id]["hon"].api.auth.refresh_token hass.config_entries.async_update_entry( diff --git a/custom_components/hon/binary_sensor.py b/custom_components/hon/binary_sensor.py index 3907325..de12403 100644 --- a/custom_components/hon/binary_sensor.py +++ b/custom_components/hon/binary_sensor.py @@ -7,9 +7,8 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntity, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from .const import DOMAIN from .entity import HonEntity @@ -317,7 +316,7 @@ BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD" async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: @@ -346,4 +345,4 @@ class HonBinarySensorEntity(HonEntity, BinarySensorEntity): == self.entity_description.on_value ) if update: - self.async_write_ha_state() + self.schedule_update_ha_state() diff --git a/custom_components/hon/button.py b/custom_components/hon/button.py index ce0f548..4a30ef8 100644 --- a/custom_components/hon/button.py +++ b/custom_components/hon/button.py @@ -6,7 +6,7 @@ from homeassistant.components.button import ButtonEntityDescription, ButtonEntit from homeassistant.config_entries import ConfigEntry from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType +from homeassistant.core import HomeAssistant from pyhon.appliance import HonAppliance from .const import DOMAIN @@ -56,7 +56,7 @@ BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = { async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities: list[HonButtonType] = [] for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: @@ -88,7 +88,7 @@ class HonButtonEntity(HonEntity, ButtonEntity): class HonDeviceInfo(HonEntity, ButtonEntity): def __init__( - self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance + self, hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance ) -> None: super().__init__(hass, entry, device) @@ -108,7 +108,7 @@ class HonDeviceInfo(HonEntity, ButtonEntity): class HonDataArchive(HonEntity, ButtonEntity): def __init__( - self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance + self, hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance ) -> None: super().__init__(hass, entry, device) diff --git a/custom_components/hon/climate.py b/custom_components/hon/climate.py index f3ce937..1f60cfd 100644 --- a/custom_components/hon/climate.py +++ b/custom_components/hon/climate.py @@ -1,3 +1,4 @@ +import asyncio import logging from dataclasses import dataclass from typing import Any @@ -19,9 +20,8 @@ from homeassistant.const import ( ATTR_TEMPERATURE, UnitOfTemperature, ) -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from pyhon.appliance import HonAppliance from pyhon.parameter.range import HonParameterRange @@ -104,7 +104,7 @@ CLIMATES: dict[ async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] entity: HonClimateEntity | HonACClimateEntity @@ -130,7 +130,7 @@ class HonACClimateEntity(HonEntity, ClimateEntity): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: HonACClimateEntityDescription, @@ -290,7 +290,7 @@ class HonACClimateEntity(HonEntity, ClimateEntity): @callback def _handle_coordinator_update(self, update: bool = True) -> None: if update: - self.async_write_ha_state() + self.schedule_update_ha_state() class HonClimateEntity(HonEntity, ClimateEntity): @@ -299,7 +299,7 @@ class HonClimateEntity(HonEntity, ClimateEntity): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: HonClimateEntityDescription, @@ -423,4 +423,4 @@ class HonClimateEntity(HonEntity, ClimateEntity): @callback def _handle_coordinator_update(self, update: bool = True) -> None: if update: - self.async_write_ha_state() + self.schedule_update_ha_state() diff --git a/custom_components/hon/entity.py b/custom_components/hon/entity.py index a597052..1e339c5 100644 --- a/custom_components/hon/entity.py +++ b/custom_components/hon/entity.py @@ -1,9 +1,8 @@ from typing import Optional, Any from homeassistant.config_entries import ConfigEntry -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity import DeviceInfo -from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, ) @@ -20,7 +19,7 @@ class HonEntity(CoordinatorEntity[DataUpdateCoordinator[dict[str, Any]]]): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: Optional[HonEntityDescription] = None, diff --git a/custom_components/hon/fan.py b/custom_components/hon/fan.py index a07a08f..27e7b68 100644 --- a/custom_components/hon/fan.py +++ b/custom_components/hon/fan.py @@ -8,9 +8,8 @@ from homeassistant.components.fan import ( FanEntityFeature, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from homeassistant.util.percentage import ( percentage_to_ranged_value, ranged_value_to_percentage, @@ -36,7 +35,7 @@ FANS: dict[str, tuple[FanEntityDescription, ...]] = { async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: @@ -56,7 +55,7 @@ class HonFanEntity(HonEntity, FanEntity): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: FanEntityDescription, @@ -125,7 +124,7 @@ class HonFanEntity(HonEntity, FanEntity): ) self._attr_percentage = self.percentage if update: - self.async_write_ha_state() + self.schedule_update_ha_state() @property def available(self) -> bool: diff --git a/custom_components/hon/light.py b/custom_components/hon/light.py index d38a994..066c75c 100644 --- a/custom_components/hon/light.py +++ b/custom_components/hon/light.py @@ -8,9 +8,8 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from pyhon.appliance import HonAppliance from pyhon.parameter.range import HonParameterRange @@ -53,7 +52,7 @@ LIGHTS: dict[str, tuple[LightEntityDescription, ...]] = { async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: @@ -73,7 +72,7 @@ class HonLightEntity(HonEntity, LightEntity): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: LightEntityDescription, @@ -136,7 +135,7 @@ class HonLightEntity(HonEntity, LightEntity): self._attr_is_on = self.is_on self._attr_brightness = self.brightness if update: - self.async_write_ha_state() + self.schedule_update_ha_state() @property def available(self) -> bool: diff --git a/custom_components/hon/lock.py b/custom_components/hon/lock.py index 1ecb744..d85364f 100644 --- a/custom_components/hon/lock.py +++ b/custom_components/hon/lock.py @@ -3,9 +3,8 @@ from typing import Any from homeassistant.components.lock import LockEntity, LockEntityDescription from homeassistant.config_entries import ConfigEntry -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from pyhon.parameter.base import HonParameter from pyhon.parameter.range import HonParameterRange @@ -26,7 +25,7 @@ LOCKS: dict[str, tuple[LockEntityDescription, ...]] = { async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: diff --git a/custom_components/hon/number.py b/custom_components/hon/number.py index b646b75..a5f76b7 100644 --- a/custom_components/hon/number.py +++ b/custom_components/hon/number.py @@ -8,10 +8,9 @@ from homeassistant.components.number import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfTime, UnitOfTemperature -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from pyhon.appliance import HonAppliance from pyhon.parameter.range import HonParameterRange @@ -207,7 +206,7 @@ NUMBERS["WD"] = unique_entities(NUMBERS["WM"], NUMBERS["TD"]) async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] entity: HonNumberEntity | HonConfigNumberEntity @@ -230,7 +229,7 @@ class HonNumberEntity(HonEntity, NumberEntity): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: HonNumberEntityDescription, @@ -268,7 +267,7 @@ class HonNumberEntity(HonEntity, NumberEntity): self._attr_native_step = setting.step self._attr_native_value = self.native_value if update: - self.async_write_ha_state() + self.schedule_update_ha_state() @property def available(self) -> bool: @@ -285,7 +284,7 @@ class HonConfigNumberEntity(HonEntity, NumberEntity): def __init__( self, - hass: HomeAssistantType, + hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance, description: HonConfigNumberEntityDescription, @@ -324,4 +323,4 @@ class HonConfigNumberEntity(HonEntity, NumberEntity): self._attr_native_step = setting.step self._attr_native_value = self.native_value if update: - self.async_write_ha_state() + self.schedule_update_ha_state() diff --git a/custom_components/hon/select.py b/custom_components/hon/select.py index c7da326..4ddc35c 100644 --- a/custom_components/hon/select.py +++ b/custom_components/hon/select.py @@ -6,10 +6,9 @@ from dataclasses import dataclass from homeassistant.components.select import SelectEntity, SelectEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from . import const from .const import DOMAIN @@ -211,7 +210,7 @@ SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"]) async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] entity: HonSelectEntity | HonConfigSelectEntity @@ -271,7 +270,7 @@ class HonConfigSelectEntity(HonEntity, SelectEntity): self._attr_options = self.options self._attr_current_option = self.current_option if update: - self.async_write_ha_state() + self.schedule_update_ha_state() @property def available(self) -> bool: @@ -334,4 +333,4 @@ class HonSelectEntity(HonEntity, SelectEntity): self._attr_options = self.options self._attr_current_option = self.current_option if update: - self.async_write_ha_state() + self.schedule_update_ha_state() diff --git a/custom_components/hon/sensor.py b/custom_components/hon/sensor.py index 4039282..bca9f88 100644 --- a/custom_components/hon/sensor.py +++ b/custom_components/hon/sensor.py @@ -21,10 +21,9 @@ from homeassistant.const import ( UnitOfTime, UnitOfTemperature, ) -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from . import const from .const import DOMAIN @@ -513,6 +512,12 @@ SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = { translation_key="mach_modes_ac", option_list=const.AC_MACH_MODE, ), + HonSensorEntityDescription( + key="compressorFrequency", + name="Compressor Frequency", + icon="mdi:information", + device_class=SensorDeviceClass.FREQUENCY, + ), ), "REF": ( HonSensorEntityDescription( @@ -808,7 +813,7 @@ SENSORS["WD"] = unique_entities(SENSORS["WM"], SENSORS["TD"]) async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] entity: HonSensorEntity | HonConfigSensorEntity @@ -846,7 +851,7 @@ class HonSensorEntity(HonEntity, SensorEntity): self._attr_native_value = 0 self._attr_native_value = value if update: - self.async_write_ha_state() + self.schedule_update_ha_state() class HonConfigSensorEntity(HonEntity, SensorEntity): @@ -874,4 +879,4 @@ class HonConfigSensorEntity(HonEntity, SensorEntity): value = get_readable(self.entity_description, value) self._attr_native_value = value if update: - self.async_write_ha_state() + self.schedule_update_ha_state() diff --git a/custom_components/hon/switch.py b/custom_components/hon/switch.py index 359e505..4b3d4c0 100644 --- a/custom_components/hon/switch.py +++ b/custom_components/hon/switch.py @@ -5,10 +5,9 @@ from typing import Any from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import HomeAssistantType from pyhon.parameter.base import HonParameter from pyhon.parameter.range import HonParameterRange @@ -403,7 +402,7 @@ SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["TD"]) async def async_setup_entry( - hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: entities = [] entity: HonConfigSwitchEntity | HonControlSwitchEntity | HonSwitchEntity @@ -476,7 +475,7 @@ class HonSwitchEntity(HonEntity, SwitchEntity): def _handle_coordinator_update(self, update: bool = True) -> None: self._attr_is_on = self.is_on if update: - self.async_write_ha_state() + self.schedule_update_ha_state() class HonControlSwitchEntity(HonEntity, SwitchEntity): @@ -556,4 +555,4 @@ class HonConfigSwitchEntity(HonEntity, SwitchEntity): def _handle_coordinator_update(self, update: bool = True) -> None: self._attr_is_on = self.is_on if update: - self.async_write_ha_state() + self.schedule_update_ha_state()