From 653e0917bb70b0f1fcc412bd1de666d31d8d677a Mon Sep 17 00:00:00 2001 From: Jafar Atili Date: Wed, 28 Sep 2022 14:51:52 +0300 Subject: [PATCH] Bump pyswitchbee to 1.5.3 (#78583) * Add switchbee to strict-typing * strict typing * Bumped pyswitchbee * bumped library to 1.5.1 * strict-typed the package * fixed issue * addressed epenet comments * fixed requirements_all * once more --- .strict-typing | 1 + .../components/switchbee/config_flow.py | 7 ++- .../components/switchbee/coordinator.py | 13 ++++- homeassistant/components/switchbee/entity.py | 12 ++-- .../components/switchbee/manifest.json | 2 +- homeassistant/components/switchbee/switch.py | 56 +++++++++++++------ mypy.ini | 10 ++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 9 files changed, 76 insertions(+), 29 deletions(-) diff --git a/.strict-typing b/.strict-typing index 9df3d16dc43..cc90af1b98b 100644 --- a/.strict-typing +++ b/.strict-typing @@ -241,6 +241,7 @@ homeassistant.components.stream.* homeassistant.components.sun.* homeassistant.components.surepetcare.* homeassistant.components.switch.* +homeassistant.components.switchbee.* homeassistant.components.switcher_kis.* homeassistant.components.synology_dsm.* homeassistant.components.systemmonitor.* diff --git a/homeassistant/components/switchbee/config_flow.py b/homeassistant/components/switchbee/config_flow.py index c20878cc2c2..d49155f9cb5 100644 --- a/homeassistant/components/switchbee/config_flow.py +++ b/homeassistant/components/switchbee/config_flow.py @@ -29,7 +29,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema( ) -async def validate_input(hass: HomeAssistant, data: dict[str, Any]): +async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> str: """Validate the user input allows us to connect.""" websession = async_get_clientsession(hass, verify_ssl=False) @@ -45,6 +45,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]): raise CannotConnect from exp + assert api.mac is not None return format_mac(api.mac) @@ -53,7 +54,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None) -> FlowResult: + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Show the setup form to the user.""" errors: dict[str, str] = {} diff --git a/homeassistant/components/switchbee/coordinator.py b/homeassistant/components/switchbee/coordinator.py index 72a487d812b..5d6cf6140af 100644 --- a/homeassistant/components/switchbee/coordinator.py +++ b/homeassistant/components/switchbee/coordinator.py @@ -1,5 +1,8 @@ """SwitchBee integration Coordinator.""" +from __future__ import annotations + +from collections.abc import Mapping from datetime import timedelta import logging @@ -15,7 +18,7 @@ from .const import DOMAIN, SCAN_INTERVAL_SEC _LOGGER = logging.getLogger(__name__) -class SwitchBeeCoordinator(DataUpdateCoordinator[dict[int, SwitchBeeBaseDevice]]): +class SwitchBeeCoordinator(DataUpdateCoordinator[Mapping[int, SwitchBeeBaseDevice]]): """Class to manage fetching Freedompro data API.""" def __init__( @@ -26,7 +29,11 @@ class SwitchBeeCoordinator(DataUpdateCoordinator[dict[int, SwitchBeeBaseDevice]] """Initialize.""" self.api: CentralUnitAPI = swb_api self._reconnect_counts: int = 0 - self.mac_formated: str = format_mac(swb_api.mac) + + self.mac_formated: str | None = ( + None if self.api.mac is None else format_mac(self.api.mac) + ) + super().__init__( hass, _LOGGER, @@ -34,7 +41,7 @@ class SwitchBeeCoordinator(DataUpdateCoordinator[dict[int, SwitchBeeBaseDevice]] update_interval=timedelta(seconds=SCAN_INTERVAL_SEC), ) - async def _async_update_data(self) -> dict[int, SwitchBeeBaseDevice]: + async def _async_update_data(self) -> Mapping[int, SwitchBeeBaseDevice]: """Update data via library.""" if self._reconnect_counts != self.api.reconnect_count: diff --git a/homeassistant/components/switchbee/entity.py b/homeassistant/components/switchbee/entity.py index fa2fb93c4f2..56191cbb1eb 100644 --- a/homeassistant/components/switchbee/entity.py +++ b/homeassistant/components/switchbee/entity.py @@ -1,4 +1,6 @@ """Support for SwitchBee entity.""" +from typing import Generic, TypeVar + from switchbee import SWITCHBEE_BRAND from switchbee.device import SwitchBeeBaseDevice @@ -8,15 +10,17 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN from .coordinator import SwitchBeeCoordinator +_DeviceTypeT = TypeVar("_DeviceTypeT", bound=SwitchBeeBaseDevice) -class SwitchBeeEntity(CoordinatorEntity[SwitchBeeCoordinator]): + +class SwitchBeeEntity(CoordinatorEntity[SwitchBeeCoordinator], Generic[_DeviceTypeT]): """Representation of a Switchbee entity.""" _attr_has_entity_name = True def __init__( self, - device: SwitchBeeBaseDevice, + device: _DeviceTypeT, coordinator: SwitchBeeCoordinator, ) -> None: """Initialize the Switchbee entity.""" @@ -26,12 +30,12 @@ class SwitchBeeEntity(CoordinatorEntity[SwitchBeeCoordinator]): self._attr_unique_id = f"{coordinator.mac_formated}-{device.id}" -class SwitchBeeDeviceEntity(SwitchBeeEntity): +class SwitchBeeDeviceEntity(SwitchBeeEntity[_DeviceTypeT]): """Representation of a Switchbee device entity.""" def __init__( self, - device: SwitchBeeBaseDevice, + device: _DeviceTypeT, coordinator: SwitchBeeCoordinator, ) -> None: """Initialize the Switchbee device.""" diff --git a/homeassistant/components/switchbee/manifest.json b/homeassistant/components/switchbee/manifest.json index f368fa1e3fa..4751af129b7 100644 --- a/homeassistant/components/switchbee/manifest.json +++ b/homeassistant/components/switchbee/manifest.json @@ -3,7 +3,7 @@ "name": "SwitchBee", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/switchbee", - "requirements": ["pyswitchbee==1.4.8"], + "requirements": ["pyswitchbee==1.5.3"], "codeowners": ["@jafar-atili"], "iot_class": "local_polling" } diff --git a/homeassistant/components/switchbee/switch.py b/homeassistant/components/switchbee/switch.py index e05339ee68e..a8fccd632cb 100644 --- a/homeassistant/components/switchbee/switch.py +++ b/homeassistant/components/switchbee/switch.py @@ -1,9 +1,18 @@ """Support for SwitchBee switch.""" + +from __future__ import annotations + import logging -from typing import Any +from typing import Any, TypeVar, Union, cast from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError -from switchbee.device import ApiStateCommand, DeviceType, SwitchBeeBaseDevice +from switchbee.device import ( + ApiStateCommand, + SwitchBeeGroupSwitch, + SwitchBeeSwitch, + SwitchBeeTimedSwitch, + SwitchBeeTimerSwitch, +) from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry @@ -17,6 +26,16 @@ from .entity import SwitchBeeDeviceEntity _LOGGER = logging.getLogger(__name__) +_DeviceTypeT = TypeVar( + "_DeviceTypeT", + bound=Union[ + SwitchBeeTimedSwitch, + SwitchBeeGroupSwitch, + SwitchBeeSwitch, + SwitchBeeTimerSwitch, + ], +) + async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback @@ -27,22 +46,24 @@ async def async_setup_entry( async_add_entities( SwitchBeeSwitchEntity(device, coordinator) for device in coordinator.data.values() - if device.type - in [ - DeviceType.TimedPowerSwitch, - DeviceType.GroupSwitch, - DeviceType.Switch, - DeviceType.TimedSwitch, - ] + if isinstance( + device, + ( + SwitchBeeTimedSwitch, + SwitchBeeGroupSwitch, + SwitchBeeSwitch, + SwitchBeeTimerSwitch, + ), + ) ) -class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity, SwitchEntity): +class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity[_DeviceTypeT], SwitchEntity): """Representation of a Switchbee switch.""" def __init__( self, - device: SwitchBeeBaseDevice, + device: _DeviceTypeT, coordinator: SwitchBeeCoordinator, ) -> None: """Initialize the Switchbee switch.""" @@ -64,7 +85,7 @@ class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity, SwitchEntity): def _update_from_coordinator(self) -> None: """Update the entity attributes from the coordinator data.""" - async def async_refresh_state(): + async def async_refresh_state() -> None: """Refresh the device state in the Central Unit. This function addresses issue of a device that came online back but still report @@ -84,7 +105,10 @@ class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity, SwitchEntity): except SwitchBeeError: return - if self.coordinator.data[self._device.id].state == -1: + coordinator_device = cast(_DeviceTypeT, self.coordinator.data[self._device.id]) + + if coordinator_device.state == -1: + # This specific call will refresh the state of the device in the CU self.hass.async_create_task(async_refresh_state()) @@ -108,9 +132,7 @@ class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity, SwitchEntity): # timed power switch state is an integer representing the number of minutes left until it goes off # regulare switches state is ON/OFF (1/0 respectively) - self._attr_is_on = ( - self.coordinator.data[self._device.id].state != ApiStateCommand.OFF - ) + self._attr_is_on = coordinator_device.state != ApiStateCommand.OFF async def async_turn_on(self, **kwargs: Any) -> None: """Async function to set on to switch.""" @@ -120,7 +142,7 @@ class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity, SwitchEntity): """Async function to set off to switch.""" return await self._async_set_state(ApiStateCommand.OFF) - async def _async_set_state(self, state: ApiStateCommand) -> None: + async def _async_set_state(self, state: str) -> None: try: await self.coordinator.api.set_state(self._device.id, state) except (SwitchBeeError, SwitchBeeDeviceOfflineError) as exp: diff --git a/mypy.ini b/mypy.ini index c77934d958a..04986db451c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2163,6 +2163,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.switchbee.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.switcher_kis.*] check_untyped_defs = true disallow_incomplete_defs = true diff --git a/requirements_all.txt b/requirements_all.txt index 54872f62e6f..ec747177b0d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1920,7 +1920,7 @@ pystiebeleltron==0.0.1.dev2 pysuez==0.1.19 # homeassistant.components.switchbee -pyswitchbee==1.4.8 +pyswitchbee==1.5.3 # homeassistant.components.syncthru pysyncthru==0.7.10 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 9eff5d82f81..d7fd8a19edf 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1349,7 +1349,7 @@ pyspcwebgw==0.4.0 pysqueezebox==0.6.0 # homeassistant.components.switchbee -pyswitchbee==1.4.8 +pyswitchbee==1.5.3 # homeassistant.components.syncthru pysyncthru==0.7.10