Fix Shelly type hints (#50322)

This commit is contained in:
Shay Levy 2021-05-09 20:46:53 +03:00 committed by GitHub
parent 4e4042a869
commit be73067f9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 17 deletions

View file

@ -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)

View file

@ -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

View file

@ -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):

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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.*",