Use pydeconz interface controls for fans (#75156)

This commit is contained in:
Robert Svensson 2022-07-14 23:53:09 +02:00 committed by GitHub
parent ff297cb902
commit 4a3d047dff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 42 deletions

View file

@ -1,17 +1,10 @@
"""Support for deCONZ fans."""
from __future__ import annotations
from typing import Any, Literal
from typing import Any
from pydeconz.models.event import EventType
from pydeconz.models.light.fan import (
FAN_SPEED_25_PERCENT,
FAN_SPEED_50_PERCENT,
FAN_SPEED_75_PERCENT,
FAN_SPEED_100_PERCENT,
FAN_SPEED_OFF,
Fan,
)
from pydeconz.models.light.light import Light, LightFanSpeed
from homeassistant.components.fan import DOMAIN, FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry
@ -25,11 +18,11 @@ from homeassistant.util.percentage import (
from .deconz_device import DeconzDevice
from .gateway import DeconzGateway, get_gateway_from_config_entry
ORDERED_NAMED_FAN_SPEEDS: list[Literal[0, 1, 2, 3, 4, 5, 6]] = [
FAN_SPEED_25_PERCENT,
FAN_SPEED_50_PERCENT,
FAN_SPEED_75_PERCENT,
FAN_SPEED_100_PERCENT,
ORDERED_NAMED_FAN_SPEEDS: list[LightFanSpeed] = [
LightFanSpeed.PERCENT_25,
LightFanSpeed.PERCENT_50,
LightFanSpeed.PERCENT_75,
LightFanSpeed.PERCENT_100,
]
@ -45,12 +38,14 @@ async def async_setup_entry(
@callback
def async_add_fan(_: EventType, fan_id: str) -> None:
"""Add fan from deCONZ."""
fan = gateway.api.lights.fans[fan_id]
fan = gateway.api.lights.lights[fan_id]
if not fan.supports_fan_speed:
return
async_add_entities([DeconzFan(fan, gateway)])
gateway.register_platform_add_device_callback(
async_add_fan,
gateway.api.lights.fans,
gateway.api.lights.lights,
)
@ -58,33 +53,32 @@ class DeconzFan(DeconzDevice, FanEntity):
"""Representation of a deCONZ fan."""
TYPE = DOMAIN
_device: Fan
_default_on_speed: Literal[0, 1, 2, 3, 4, 5, 6]
_device: Light
_default_on_speed = LightFanSpeed.PERCENT_50
_attr_supported_features = FanEntityFeature.SET_SPEED
def __init__(self, device: Fan, gateway: DeconzGateway) -> None:
def __init__(self, device: Light, gateway: DeconzGateway) -> None:
"""Set up fan."""
super().__init__(device, gateway)
self._default_on_speed = FAN_SPEED_50_PERCENT
if self._device.speed in ORDERED_NAMED_FAN_SPEEDS:
self._default_on_speed = self._device.speed
if device.fan_speed in ORDERED_NAMED_FAN_SPEEDS:
self._default_on_speed = device.fan_speed
@property
def is_on(self) -> bool:
"""Return true if fan is on."""
return self._device.speed != FAN_SPEED_OFF
return self._device.fan_speed != LightFanSpeed.OFF
@property
def percentage(self) -> int | None:
"""Return the current speed percentage."""
if self._device.speed == FAN_SPEED_OFF:
if self._device.fan_speed == LightFanSpeed.OFF:
return 0
if self._device.speed not in ORDERED_NAMED_FAN_SPEEDS:
if self._device.fan_speed not in ORDERED_NAMED_FAN_SPEEDS:
return None
return ordered_list_item_to_percentage(
ORDERED_NAMED_FAN_SPEEDS, self._device.speed
ORDERED_NAMED_FAN_SPEEDS, self._device.fan_speed
)
@property
@ -95,16 +89,19 @@ class DeconzFan(DeconzDevice, FanEntity):
@callback
def async_update_callback(self) -> None:
"""Store latest configured speed from the device."""
if self._device.speed in ORDERED_NAMED_FAN_SPEEDS:
self._default_on_speed = self._device.speed
if self._device.fan_speed in ORDERED_NAMED_FAN_SPEEDS:
self._default_on_speed = self._device.fan_speed
super().async_update_callback()
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
if percentage == 0:
return await self.async_turn_off()
await self._device.set_speed(
percentage_to_ordered_list_item(ORDERED_NAMED_FAN_SPEEDS, percentage)
await self.gateway.api.lights.lights.set_state(
id=self._device.resource_id,
fan_speed=percentage_to_ordered_list_item(
ORDERED_NAMED_FAN_SPEEDS, percentage
),
)
async def async_turn_on(
@ -117,8 +114,14 @@ class DeconzFan(DeconzDevice, FanEntity):
if percentage is not None:
await self.async_set_percentage(percentage)
return
await self._device.set_speed(self._default_on_speed)
await self.gateway.api.lights.lights.set_state(
id=self._device.resource_id,
fan_speed=self._default_on_speed,
)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off fan."""
await self._device.set_speed(FAN_SPEED_OFF)
await self.gateway.api.lights.lights.set_state(
id=self._device.resource_id,
fan_speed=LightFanSpeed.OFF,
)

View file

@ -85,8 +85,7 @@ async def async_setup_entry(
@callback
def async_add_light(_: EventType, light_id: str) -> None:
"""Add light from deCONZ."""
light = gateway.api.lights[light_id]
assert isinstance(light, Light)
light = gateway.api.lights.lights[light_id]
if light.type in POWER_PLUGS:
return
@ -97,11 +96,6 @@ async def async_setup_entry(
gateway.api.lights.lights,
)
gateway.register_platform_add_device_callback(
async_add_light,
gateway.api.lights.fans,
)
@callback
def async_add_group(_: EventType, group_id: str) -> None:
"""Add group from deCONZ.

View file

@ -3,7 +3,7 @@
"name": "deCONZ",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/deconz",
"requirements": ["pydeconz==98"],
"requirements": ["pydeconz==99"],
"ssdp": [
{
"manufacturer": "Royal Philips Electronics",

View file

@ -1447,7 +1447,7 @@ pydaikin==2.7.0
pydanfossair==0.1.0
# homeassistant.components.deconz
pydeconz==98
pydeconz==99
# homeassistant.components.delijn
pydelijn==1.0.0

View file

@ -980,7 +980,7 @@ pycoolmasternet-async==0.1.2
pydaikin==2.7.0
# homeassistant.components.deconz
pydeconz==98
pydeconz==99
# homeassistant.components.dexcom
pydexcom==0.2.3