From be73067f9c1408e1d28ca86f17abf1323134fcf0 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sun, 9 May 2021 20:46:53 +0300 Subject: [PATCH] Fix Shelly type hints (#50322) --- homeassistant/components/shelly/__init__.py | 9 ++++++--- homeassistant/components/shelly/const.py | 2 ++ homeassistant/components/shelly/entity.py | 2 +- homeassistant/components/shelly/light.py | 3 ++- homeassistant/components/shelly/utils.py | 20 ++++++++++++-------- mypy.ini | 3 --- script/hassfest/mypy_config.py | 1 - 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 2da2c5a6ea5..d2c56217afe 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -90,8 +90,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): ) dev_reg = await device_registry.async_get_registry(hass) - identifier = (DOMAIN, entry.unique_id) - device_entry = dev_reg.async_get_device(identifiers={identifier}, connections=set()) + device_entry = None + if entry.unique_id is not None: + device_entry = dev_reg.async_get_device( + identifiers={(DOMAIN, entry.unique_id)}, connections=set() + ) if device_entry and entry.entry_id not in device_entry.config_entries: device_entry = None @@ -185,7 +188,7 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): self._async_remove_device_updates_handler = self.async_add_listener( self._async_device_updates_handler ) - self._last_input_events_count = {} + self._last_input_events_count: dict = {} hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop) diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index bff82057120..964dab31698 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -81,3 +81,5 @@ SHBTN_MODELS = ["SHBTN-1", "SHBTN-2"] KELVIN_MAX_VALUE = 6500 KELVIN_MIN_VALUE_WHITE = 2700 KELVIN_MIN_VALUE_COLOR = 3000 + +UPTIME_DEVIATION = 5 diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 675eead2155..9445c792c7b 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -178,7 +178,7 @@ class ShellyBlockEntity(entity.Entity): """Initialize Shelly entity.""" self.wrapper = wrapper self.block = block - self._name = get_entity_name(wrapper.device, block) + self._name: str | None = get_entity_name(wrapper.device, block) @property def name(self): diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index adca498c3f9..d6afcd8841e 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio import logging +from typing import Any from aioshelly import Block import async_timeout @@ -212,7 +213,7 @@ class ShellyLight(ShellyBlockEntity, LightEntity): set_mode = None supported_color_modes = self._supported_color_modes - params = {"turn": "on"} + params: dict[str, Any] = {"turn": "on"} if ATTR_BRIGHTNESS in kwargs and brightness_supported(supported_color_modes): brightness_pct = int(100 * (kwargs[ATTR_BRIGHTNESS] + 1) / 255) diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 2490eceaba5..37b34dfe9e8 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -1,7 +1,7 @@ """Shelly helpers functions.""" from __future__ import annotations -from datetime import timedelta +from datetime import datetime, timedelta import logging import aioshelly @@ -9,7 +9,7 @@ import aioshelly from homeassistant.const import EVENT_HOMEASSISTANT_STOP, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import singleton -from homeassistant.util.dt import parse_datetime, utcnow +from homeassistant.util.dt import utcnow from .const import ( BASIC_INPUTS_EVENTS_TYPES, @@ -21,6 +21,7 @@ from .const import ( SHBTN_INPUTS_EVENTS_TYPES, SHBTN_MODELS, SHIX3_1_INPUTS_EVENTS_TYPES, + UPTIME_DEVIATION, ) _LOGGER = logging.getLogger(__name__) @@ -118,6 +119,8 @@ def is_momentary_input(settings: dict, block: aioshelly.Block) -> bool: return True button = settings.get("relays") or settings.get("lights") or settings.get("inputs") + if button is None: + return False # Shelly 1L has two button settings in the first channel if settings["device"]["type"] == "SHSW-L": @@ -133,13 +136,14 @@ def is_momentary_input(settings: dict, block: aioshelly.Block) -> bool: def get_device_uptime(status: dict, last_uptime: str) -> str: """Return device uptime string, tolerate up to 5 seconds deviation.""" - uptime = utcnow() - timedelta(seconds=status["uptime"]) + delta_uptime = utcnow() - timedelta(seconds=status["uptime"]) - if not last_uptime: - return uptime.replace(microsecond=0).isoformat() - - if abs((uptime - parse_datetime(last_uptime)).total_seconds()) > 5: - return uptime.replace(microsecond=0).isoformat() + if ( + not last_uptime + or abs((delta_uptime - datetime.fromisoformat(last_uptime)).total_seconds()) + > UPTIME_DEVIATION + ): + return delta_uptime.replace(microsecond=0).isoformat() return last_uptime diff --git a/mypy.ini b/mypy.ini index 3f57c90f2a8..7d78261e238 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1175,9 +1175,6 @@ ignore_errors = true [mypy-homeassistant.components.sharkiq.*] ignore_errors = true -[mypy-homeassistant.components.shelly.*] -ignore_errors = true - [mypy-homeassistant.components.sma.*] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index b806fcdbf46..85dcab6efef 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -186,7 +186,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.sentry.*", "homeassistant.components.sesame.*", "homeassistant.components.sharkiq.*", - "homeassistant.components.shelly.*", "homeassistant.components.sma.*", "homeassistant.components.smart_meter_texas.*", "homeassistant.components.smartthings.*",