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 <farmio@alphart.net>

* Update homeassistant/components/deconz/switch.py

Co-authored-by: Matthias Alphart <farmio@alphart.net>

* Update homeassistant/components/deconz/switch.py

Co-authored-by: Matthias Alphart <farmio@alphart.net>

* Update homeassistant/components/deconz/siren.py

Co-authored-by: Matthias Alphart <farmio@alphart.net>

* Update homeassistant/components/deconz/siren.py

Co-authored-by: Matthias Alphart <farmio@alphart.net>

* Add Any import

* Update homeassistant/components/deconz/siren.py

Co-authored-by: Matthias Alphart <farmio@alphart.net>

Co-authored-by: Matthias Alphart <farmio@alphart.net>
This commit is contained in:
Robert Svensson 2021-11-16 17:25:56 +01:00 committed by GitHub
parent 16027b9f43
commit c7c1d6000f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 17 deletions

View File

@ -1,5 +1,10 @@
"""Support for deCONZ siren.""" """Support for deCONZ siren."""
from __future__ import annotations
from collections.abc import ValuesView
from typing import Any
from pydeconz.light import Siren from pydeconz.light import Siren
from homeassistant.components.siren import ( from homeassistant.components.siren import (
@ -10,20 +15,28 @@ from homeassistant.components.siren import (
SUPPORT_TURN_ON, SUPPORT_TURN_ON,
SirenEntity, 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.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .deconz_device import DeconzDevice 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.""" """Set up sirens for deCONZ component."""
gateway = get_gateway_from_config_entry(hass, config_entry) gateway = get_gateway_from_config_entry(hass, config_entry)
gateway.entities[DOMAIN] = set() gateway.entities[DOMAIN] = set()
@callback @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.""" """Add siren from deCONZ."""
entities = [] entities = []
@ -53,8 +66,9 @@ class DeconzSiren(DeconzDevice, SirenEntity):
"""Representation of a deCONZ siren.""" """Representation of a deCONZ siren."""
TYPE = DOMAIN TYPE = DOMAIN
_device: Siren
def __init__(self, device, gateway) -> None: def __init__(self, device: Siren, gateway: DeconzGateway) -> None:
"""Set up siren.""" """Set up siren."""
super().__init__(device, gateway) super().__init__(device, gateway)
@ -63,17 +77,17 @@ class DeconzSiren(DeconzDevice, SirenEntity):
) )
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if siren is on.""" """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.""" """Turn on siren."""
data = {} data = {}
if (duration := kwargs.get(ATTR_DURATION)) is not None: if (duration := kwargs.get(ATTR_DURATION)) is not None:
data["duration"] = duration * 10 data["duration"] = duration * 10
await self._device.turn_on(**data) 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.""" """Turn off siren."""
await self._device.turn_off() await self._device.turn_off()

View File

@ -1,18 +1,29 @@
"""Support for deCONZ switches.""" """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.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 import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect 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 .const import DOMAIN as DECONZ_DOMAIN, POWER_PLUGS
from .deconz_device import DeconzDevice from .deconz_device import DeconzDevice
from .gateway import get_gateway_from_config_entry 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. """Set up switches for deCONZ component.
Switches are based on the same device class as lights in deCONZ. 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) entity_registry.async_remove(entity_id)
@callback @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.""" """Add switch from deCONZ."""
entities = [] entities = []
@ -62,16 +75,17 @@ class DeconzPowerPlug(DeconzDevice, SwitchEntity):
"""Representation of a deCONZ power plug.""" """Representation of a deCONZ power plug."""
TYPE = DOMAIN TYPE = DOMAIN
_device: Light
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if switch is on.""" """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.""" """Turn on switch."""
await self._device.set_state(on=True) 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.""" """Turn off switch."""
await self._device.set_state(on=False) await self._device.set_state(on=False)