Add typing to deCONZ Scene platform and deCONZ Services (#59603)

Co-authored-by: Matthias Alphart <farmio@alphart.net>
pull/59462/head
Robert Svensson 2021-11-14 19:47:15 +01:00 committed by GitHub
parent 20fbb5b951
commit c98172f9c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 15 deletions

View File

@ -1,19 +1,34 @@
"""Support for deCONZ scenes."""
from __future__ import annotations
from collections.abc import ValuesView
from typing import Any
from pydeconz.group import DeconzScene as PydeconzScene
from homeassistant.components.scene import Scene
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 .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 scenes for deCONZ component."""
gateway = get_gateway_from_config_entry(hass, config_entry)
@callback
def async_add_scene(scenes=gateway.api.scenes.values()):
def async_add_scene(
scenes: list[PydeconzScene]
| ValuesView[PydeconzScene] = gateway.api.scenes.values(),
) -> None:
"""Add scene from deCONZ."""
entities = [DeconzScene(scene, gateway) for scene in scenes]
@ -34,14 +49,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class DeconzScene(Scene):
"""Representation of a deCONZ scene."""
def __init__(self, scene, gateway):
def __init__(self, scene: PydeconzScene, gateway: DeconzGateway) -> None:
"""Set up a scene."""
self._scene = scene
self.gateway = gateway
self._attr_name = scene.full_name
async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""Subscribe to sensors events."""
self.gateway.deconz_ids[self.entity_id] = self._scene.deconz_id

View File

@ -1,9 +1,12 @@
"""deCONZ services."""
from types import MappingProxyType
from pydeconz.utils import normalize_bridge_id
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.components.deconz.gateway import DeconzGateway
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
@ -55,10 +58,10 @@ SERVICE_TO_SCHEMA = {
@callback
def async_setup_services(hass):
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up services for deCONZ integration."""
async def async_call_deconz_service(service_call):
async def async_call_deconz_service(service_call: ServiceCall) -> None:
"""Call correct deCONZ service."""
service = service_call.service
service_data = service_call.data
@ -97,13 +100,15 @@ def async_setup_services(hass):
@callback
def async_unload_services(hass):
def async_unload_services(hass: HomeAssistant) -> None:
"""Unload deCONZ services."""
for service in SUPPORTED_SERVICES:
hass.services.async_remove(DOMAIN, service)
async def async_configure_service(gateway, data):
async def async_configure_service(
gateway: DeconzGateway, data: MappingProxyType
) -> None:
"""Set attribute of device in deCONZ.
Entity is used to resolve to a device path (e.g. '/lights/1').
@ -133,7 +138,7 @@ async def async_configure_service(gateway, data):
await gateway.api.request("put", field, json=data)
async def async_refresh_devices_service(gateway):
async def async_refresh_devices_service(gateway: DeconzGateway) -> None:
"""Refresh available devices from deCONZ."""
gateway.ignore_state_updates = True
await gateway.api.refresh_state()
@ -143,7 +148,7 @@ async def async_refresh_devices_service(gateway):
gateway.async_add_device_callback(resource_type, force=True)
async def async_remove_orphaned_entries_service(gateway):
async def async_remove_orphaned_entries_service(gateway: DeconzGateway) -> None:
"""Remove orphaned deCONZ entries from device and entity registries."""
device_registry = dr.async_get(gateway.hass)
entity_registry = er.async_get(gateway.hass)
@ -164,14 +169,14 @@ async def async_remove_orphaned_entries_service(gateway):
connections={(CONNECTION_NETWORK_MAC, gateway.api.config.mac)},
identifiers=set(),
)
if gateway_host.id in devices_to_be_removed:
if gateway_host and gateway_host.id in devices_to_be_removed:
devices_to_be_removed.remove(gateway_host.id)
# Don't remove the Gateway service entry
gateway_service = device_registry.async_get_device(
identifiers={(DOMAIN, gateway.api.config.bridge_id)}, connections=set()
)
if gateway_service.id in devices_to_be_removed:
if gateway_service and gateway_service.id in devices_to_be_removed:
devices_to_be_removed.remove(gateway_service.id)
# Don't remove devices belonging to available events