Skip to content

Commit

Permalink
Code quality for Shelly integration (#109061)
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 committed Jan 30, 2024
1 parent b3c1e16 commit 7359449
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/shelly/binary_sensor.py
Expand Up @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions homeassistant/components/shelly/config_flow.py
Expand Up @@ -201,12 +201,18 @@ async def async_step_credentials(

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(
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/shelly/coordinator.py
Expand Up @@ -7,10 +7,9 @@
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

Expand Down Expand Up @@ -137,7 +136,7 @@ def async_setup(self) -> None:
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]}",
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/shelly/cover.py
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/shelly/entity.py
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/shelly/light.py
Expand Up @@ -221,7 +221,7 @@ def rgb_color(self) -> tuple[int, int, int]:
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]:
Expand All @@ -231,7 +231,7 @@ def rgbw_color(self) -> tuple[int, int, int, int]:
else:
white = self.block.white

return (*self.rgb_color, white)
return (*self.rgb_color, cast(int, white))

@property
def color_temp_kelvin(self) -> int:
Expand Down Expand Up @@ -262,9 +262,9 @@ def effect(self) -> str | None:
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."""
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/shelly/number.py
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/shelly/sensor.py
Expand Up @@ -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",
Expand Down

0 comments on commit 7359449

Please sign in to comment.