Streamline setup of deCONZ fan platform (#71658)

pull/69851/head
Robert Svensson 2022-05-11 13:18:20 +02:00 committed by GitHub
parent 04e3cee6c3
commit b9f3f1af6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 22 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from typing import Any, Literal
from pydeconz.models.event import EventType
from pydeconz.models.light.fan import (
FAN_SPEED_25_PERCENT,
FAN_SPEED_50_PERCENT,
@ -15,7 +16,6 @@ from pydeconz.models.light.fan import (
from homeassistant.components.fan import DOMAIN, FanEntity, FanEntityFeature
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 homeassistant.util.percentage import (
ordered_list_item_to_percentage,
@ -43,33 +43,19 @@ async def async_setup_entry(
gateway.entities[DOMAIN] = set()
@callback
def async_add_fan(lights: list[Fan] | None = None) -> None:
def async_add_fan(_: EventType, fan_id: str) -> None:
"""Add fan from deCONZ."""
entities = []
if lights is None:
lights = list(gateway.api.lights.fans.values())
for light in lights:
if (
isinstance(light, Fan)
and light.unique_id not in gateway.entities[DOMAIN]
):
entities.append(DeconzFan(light, gateway))
if entities:
async_add_entities(entities)
fan = gateway.api.lights.fans[fan_id]
async_add_entities([DeconzFan(fan, gateway)])
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
gateway.signal_new_light,
gateway.api.lights.fans.subscribe(
async_add_fan,
EventType.ADDED,
)
)
async_add_fan()
for fan_id in gateway.api.lights.fans:
async_add_fan(EventType.ADDED, fan_id)
class DeconzFan(DeconzDevice, FanEntity):