From c7c1d6000f2205710887de95cc41605c3e2a3096 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 16 Nov 2021 17:25:56 +0100 Subject: [PATCH] Add type hints to Siren and Switch deCONZ platforms (#59602) * Add typing to Siren and Switch deCONZ platforms * Update homeassistant/components/deconz/switch.py Co-authored-by: Matthias Alphart * Update homeassistant/components/deconz/switch.py Co-authored-by: Matthias Alphart * Update homeassistant/components/deconz/switch.py Co-authored-by: Matthias Alphart * Update homeassistant/components/deconz/siren.py Co-authored-by: Matthias Alphart * Update homeassistant/components/deconz/siren.py Co-authored-by: Matthias Alphart * Add Any import * Update homeassistant/components/deconz/siren.py Co-authored-by: Matthias Alphart Co-authored-by: Matthias Alphart --- homeassistant/components/deconz/siren.py | 32 ++++++++++++++++------- homeassistant/components/deconz/switch.py | 30 +++++++++++++++------ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/deconz/siren.py b/homeassistant/components/deconz/siren.py index c3679b6ad89..7f0f6cc39ed 100644 --- a/homeassistant/components/deconz/siren.py +++ b/homeassistant/components/deconz/siren.py @@ -1,5 +1,10 @@ """Support for deCONZ siren.""" +from __future__ import annotations + +from collections.abc import ValuesView +from typing import Any + from pydeconz.light import Siren from homeassistant.components.siren import ( @@ -10,20 +15,28 @@ from homeassistant.components.siren import ( SUPPORT_TURN_ON, SirenEntity, ) -from homeassistant.core import callback +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity_platform import AddEntitiesCallback from .deconz_device import DeconzDevice -from .gateway import get_gateway_from_config_entry +from .gateway import DeconzGateway, get_gateway_from_config_entry -async def async_setup_entry(hass, config_entry, async_add_entities): +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: """Set up sirens for deCONZ component.""" gateway = get_gateway_from_config_entry(hass, config_entry) gateway.entities[DOMAIN] = set() @callback - def async_add_siren(lights=gateway.api.lights.values()): + def async_add_siren( + lights: list[Siren] | ValuesView[Siren] = gateway.api.lights.values(), + ) -> None: """Add siren from deCONZ.""" entities = [] @@ -53,8 +66,9 @@ class DeconzSiren(DeconzDevice, SirenEntity): """Representation of a deCONZ siren.""" TYPE = DOMAIN + _device: Siren - def __init__(self, device, gateway) -> None: + def __init__(self, device: Siren, gateway: DeconzGateway) -> None: """Set up siren.""" super().__init__(device, gateway) @@ -63,17 +77,17 @@ class DeconzSiren(DeconzDevice, SirenEntity): ) @property - def is_on(self): + def is_on(self) -> bool: """Return true if siren is on.""" - return self._device.is_on + return self._device.is_on # type: ignore[no-any-return] - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn on siren.""" data = {} if (duration := kwargs.get(ATTR_DURATION)) is not None: data["duration"] = duration * 10 await self._device.turn_on(**data) - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn off siren.""" await self._device.turn_off() diff --git a/homeassistant/components/deconz/switch.py b/homeassistant/components/deconz/switch.py index 39489fe1fc3..ab4577a427c 100644 --- a/homeassistant/components/deconz/switch.py +++ b/homeassistant/components/deconz/switch.py @@ -1,18 +1,29 @@ """Support for deCONZ switches.""" -from pydeconz.light import Siren +from __future__ import annotations + +from collections.abc import ValuesView +from typing import Any + +from pydeconz.light import Light, Siren from homeassistant.components.switch import DOMAIN, SwitchEntity -from homeassistant.core import callback +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN as DECONZ_DOMAIN, POWER_PLUGS from .deconz_device import DeconzDevice from .gateway import get_gateway_from_config_entry -async def async_setup_entry(hass, config_entry, async_add_entities): +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: """Set up switches for deCONZ component. Switches are based on the same device class as lights in deCONZ. @@ -32,7 +43,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): entity_registry.async_remove(entity_id) @callback - def async_add_switch(lights=gateway.api.lights.values()): + def async_add_switch( + lights: list[Light] | ValuesView[Light] = gateway.api.lights.values(), + ) -> None: """Add switch from deCONZ.""" entities = [] @@ -62,16 +75,17 @@ class DeconzPowerPlug(DeconzDevice, SwitchEntity): """Representation of a deCONZ power plug.""" TYPE = DOMAIN + _device: Light @property - def is_on(self): + def is_on(self) -> bool: """Return true if switch is on.""" - return self._device.state + return self._device.state # type: ignore[no-any-return] - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn on switch.""" await self._device.set_state(on=True) - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn off switch.""" await self._device.set_state(on=False)