Improve type hints in hive lights (#76025)

This commit is contained in:
epenet 2022-08-03 21:26:34 +02:00 committed by GitHub
parent fbde347e64
commit 1806172551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View File

@ -139,7 +139,7 @@ def refresh_system(
class HiveEntity(Entity):
"""Initiate Hive Base Class."""
def __init__(self, hive, hive_device):
def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None:
"""Initialize the instance."""
self.hive = hive
self.device = hive_device
@ -153,9 +153,9 @@ class HiveEntity(Entity):
sw_version=self.device["deviceData"]["version"],
via_device=(DOMAIN, self.device["parentDevice"]),
)
self.attributes = {}
self.attributes: dict[str, Any] = {}
async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""When entity is added to Home Assistant."""
self.async_on_remove(
async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state)

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from datetime import timedelta
from typing import TYPE_CHECKING, Any
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
@ -18,6 +19,9 @@ import homeassistant.util.color as color_util
from . import HiveEntity, refresh_system
from .const import ATTR_MODE, DOMAIN
if TYPE_CHECKING:
from apyhiveapi import Hive
PARALLEL_UPDATES = 0
SCAN_INTERVAL = timedelta(seconds=15)
@ -27,7 +31,7 @@ async def async_setup_entry(
) -> None:
"""Set up Hive thermostat based on a config entry."""
hive = hass.data[DOMAIN][entry.entry_id]
hive: Hive = hass.data[DOMAIN][entry.entry_id]
devices = hive.session.deviceList.get("light")
entities = []
if devices:
@ -39,7 +43,7 @@ async def async_setup_entry(
class HiveDeviceLight(HiveEntity, LightEntity):
"""Hive Active Light Device."""
def __init__(self, hive, hive_device):
def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None:
"""Initialise hive light."""
super().__init__(hive, hive_device)
if self.device["hiveType"] == "warmwhitelight":
@ -55,22 +59,22 @@ class HiveDeviceLight(HiveEntity, LightEntity):
self._attr_max_mireds = 370
@refresh_system
async def async_turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
new_brightness = None
new_color_temp = None
new_color = None
if ATTR_BRIGHTNESS in kwargs:
tmp_new_brightness = kwargs.get(ATTR_BRIGHTNESS)
tmp_new_brightness = kwargs[ATTR_BRIGHTNESS]
percentage_brightness = (tmp_new_brightness / 255) * 100
new_brightness = int(round(percentage_brightness / 5.0) * 5.0)
if new_brightness == 0:
new_brightness = 5
if ATTR_COLOR_TEMP in kwargs:
tmp_new_color_temp = kwargs.get(ATTR_COLOR_TEMP)
tmp_new_color_temp = kwargs[ATTR_COLOR_TEMP]
new_color_temp = round(1000000 / tmp_new_color_temp)
if ATTR_HS_COLOR in kwargs:
get_new_color = kwargs.get(ATTR_HS_COLOR)
get_new_color = kwargs[ATTR_HS_COLOR]
hue = int(get_new_color[0])
saturation = int(get_new_color[1])
new_color = (hue, saturation, 100)
@ -80,11 +84,11 @@ class HiveDeviceLight(HiveEntity, LightEntity):
)
@refresh_system
async def async_turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
await self.hive.light.turnOff(self.device)
async def async_update(self):
async def async_update(self) -> None:
"""Update all Node data from Hive."""
await self.hive.session.updateData(self.device)
self.device = await self.hive.light.getLight(self.device)