From 7359449636e200f459e444213d85d7e604d5c3c0 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Tue, 30 Jan 2024 09:47:52 +0100 Subject: [PATCH] Code quality for Shelly integration (#109061) --- homeassistant/components/shelly/binary_sensor.py | 2 +- homeassistant/components/shelly/config_flow.py | 12 +++++++++--- homeassistant/components/shelly/coordinator.py | 5 ++--- homeassistant/components/shelly/cover.py | 4 ++-- homeassistant/components/shelly/entity.py | 1 + homeassistant/components/shelly/light.py | 8 ++++---- homeassistant/components/shelly/number.py | 4 ++-- homeassistant/components/shelly/sensor.py | 2 +- 8 files changed, 22 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/shelly/binary_sensor.py b/homeassistant/components/shelly/binary_sensor.py index 4ad51e5cc0f..e9c8e909e87 100644 --- a/homeassistant/components/shelly/binary_sensor.py +++ b/homeassistant/components/shelly/binary_sensor.py @@ -55,7 +55,7 @@ class RestBinarySensorDescription(RestEntityDescription, BinarySensorEntityDescr """Class to describe a REST binary sensor.""" -SENSORS: Final = { +SENSORS: dict[tuple[str, str], BlockBinarySensorDescription] = { ("device", "overtemp"): BlockBinarySensorDescription( key="device|overtemp", name="Overheating", diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py index 70268fd23c4..2ae5a74bb42 100644 --- a/homeassistant/components/shelly/config_flow.py +++ b/homeassistant/components/shelly/config_flow.py @@ -201,12 +201,18 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN): if get_info_gen(self.info) in RPC_GENERATIONS: schema = { - vol.Required(CONF_PASSWORD, default=user_input.get(CONF_PASSWORD)): str, + vol.Required( + CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") + ): str, } else: schema = { - vol.Required(CONF_USERNAME, default=user_input.get(CONF_USERNAME)): str, - vol.Required(CONF_PASSWORD, default=user_input.get(CONF_PASSWORD)): str, + vol.Required( + CONF_USERNAME, default=user_input.get(CONF_USERNAME, "") + ): str, + vol.Required( + CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") + ): str, } return self.async_show_form( diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index 7f88cce1134..86fd98b527e 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -7,10 +7,9 @@ from dataclasses import dataclass from datetime import timedelta from typing import Any, Generic, TypeVar, cast -import aioshelly from aioshelly.ble import async_ensure_ble_enabled, async_stop_scanner from aioshelly.block_device import BlockDevice, BlockUpdateType -from aioshelly.const import MODEL_VALVE +from aioshelly.const import MODEL_NAMES, MODEL_VALVE from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError, RpcCallError from aioshelly.rpc_device import RpcDevice, RpcUpdateType @@ -137,7 +136,7 @@ class ShellyCoordinatorBase(DataUpdateCoordinator[None], Generic[_DeviceT]): name=self.name, connections={(CONNECTION_NETWORK_MAC, self.mac)}, manufacturer="Shelly", - model=aioshelly.const.MODEL_NAMES.get(self.model, self.model), + model=MODEL_NAMES.get(self.model, self.model), sw_version=self.sw_version, hw_version=f"gen{get_device_entry_gen(self.entry)} ({self.model})", configuration_url=f"http://{self.entry.data[CONF_HOST]}", diff --git a/homeassistant/components/shelly/cover.py b/homeassistant/components/shelly/cover.py index 4390790c794..caff64d7707 100644 --- a/homeassistant/components/shelly/cover.py +++ b/homeassistant/components/shelly/cover.py @@ -71,7 +71,7 @@ class BlockShellyCover(ShellyBlockEntity, CoverEntity): """Entity that controls a cover on block based Shelly devices.""" _attr_device_class = CoverDeviceClass.SHUTTER - _attr_supported_features = ( + _attr_supported_features: CoverEntityFeature = ( CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP ) @@ -147,7 +147,7 @@ class RpcShellyCover(ShellyRpcEntity, CoverEntity): """Entity that controls a cover on RPC based Shelly devices.""" _attr_device_class = CoverDeviceClass.SHUTTER - _attr_supported_features = ( + _attr_supported_features: CoverEntityFeature = ( CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP ) diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 3132f1f571e..3dd156e9e30 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -172,6 +172,7 @@ def async_setup_rpc_attribute_entities( coordinator = get_entry_data(hass)[config_entry.entry_id].rpc assert coordinator + polling_coordinator = None if not (sleep_period := config_entry.data[CONF_SLEEP_PERIOD]): polling_coordinator = get_entry_data(hass)[config_entry.entry_id].rpc_poll assert polling_coordinator diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index 7e49dc78e4d..234f376e85f 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -221,7 +221,7 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): red = self.block.red green = self.block.green blue = self.block.blue - return (red, green, blue) + return (cast(int, red), cast(int, green), cast(int, blue)) @property def rgbw_color(self) -> tuple[int, int, int, int]: @@ -231,7 +231,7 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): else: white = self.block.white - return (*self.rgb_color, white) + return (*self.rgb_color, cast(int, white)) @property def color_temp_kelvin(self) -> int: @@ -262,9 +262,9 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): effect_index = self.block.effect if self.coordinator.model == MODEL_BULB: - return SHBLB_1_RGB_EFFECTS[effect_index] + return SHBLB_1_RGB_EFFECTS[cast(int, effect_index)] - return STANDARD_RGB_EFFECTS[effect_index] + return STANDARD_RGB_EFFECTS[cast(int, effect_index)] async def async_turn_on(self, **kwargs: Any) -> None: """Turn on light.""" diff --git a/homeassistant/components/shelly/number.py b/homeassistant/components/shelly/number.py index 5d35e71ce5d..4cab817e67c 100644 --- a/homeassistant/components/shelly/number.py +++ b/homeassistant/components/shelly/number.py @@ -2,7 +2,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Any, Final, cast +from typing import Any, cast from aioshelly.block_device import Block from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError @@ -37,7 +37,7 @@ class BlockNumberDescription(BlockEntityDescription, NumberEntityDescription): rest_arg: str = "" -NUMBERS: Final = { +NUMBERS: dict[tuple[str, str], BlockNumberDescription] = { ("device", "valvePos"): BlockNumberDescription( key="device|valvepos", icon="mdi:pipe-valve", diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index b439a19e318..7ae709ae84f 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -69,7 +69,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription): """Class to describe a REST sensor.""" -SENSORS: Final = { +SENSORS: dict[tuple[str, str], BlockSensorDescription] = { ("device", "battery"): BlockSensorDescription( key="device|battery", name="Battery",