mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Update lutron_caseta to use new fan entity model (#45540)
This commit is contained in:
parent
92efe4f491
commit
f0e4510213
@ -3,14 +3,10 @@ import logging
|
|||||||
|
|
||||||
from pylutron_caseta import FAN_HIGH, FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_OFF
|
from pylutron_caseta import FAN_HIGH, FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_OFF
|
||||||
|
|
||||||
from homeassistant.components.fan import (
|
from homeassistant.components.fan import DOMAIN, SUPPORT_SET_SPEED, FanEntity
|
||||||
DOMAIN,
|
from homeassistant.util.percentage import (
|
||||||
SPEED_HIGH,
|
ordered_list_item_to_percentage,
|
||||||
SPEED_LOW,
|
percentage_to_ordered_list_item,
|
||||||
SPEED_MEDIUM,
|
|
||||||
SPEED_OFF,
|
|
||||||
SUPPORT_SET_SPEED,
|
|
||||||
FanEntity,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import LutronCasetaDevice
|
from . import LutronCasetaDevice
|
||||||
@ -18,23 +14,8 @@ from .const import BRIDGE_DEVICE, BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
VALUE_TO_SPEED = {
|
DEFAULT_ON_PERCENTAGE = 50
|
||||||
None: SPEED_OFF,
|
ORDERED_NAMED_FAN_SPEEDS = [FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_HIGH]
|
||||||
FAN_OFF: SPEED_OFF,
|
|
||||||
FAN_LOW: SPEED_LOW,
|
|
||||||
FAN_MEDIUM: SPEED_MEDIUM,
|
|
||||||
FAN_MEDIUM_HIGH: SPEED_MEDIUM,
|
|
||||||
FAN_HIGH: SPEED_HIGH,
|
|
||||||
}
|
|
||||||
|
|
||||||
SPEED_TO_VALUE = {
|
|
||||||
SPEED_OFF: FAN_OFF,
|
|
||||||
SPEED_LOW: FAN_LOW,
|
|
||||||
SPEED_MEDIUM: FAN_MEDIUM,
|
|
||||||
SPEED_HIGH: FAN_HIGH,
|
|
||||||
}
|
|
||||||
|
|
||||||
FAN_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
@ -61,27 +42,17 @@ class LutronCasetaFan(LutronCasetaDevice, FanEntity):
|
|||||||
"""Representation of a Lutron Caseta fan. Including Fan Speed."""
|
"""Representation of a Lutron Caseta fan. Including Fan Speed."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def speed(self) -> str:
|
def percentage(self) -> str:
|
||||||
"""Return the current speed."""
|
"""Return the current speed percentage."""
|
||||||
return VALUE_TO_SPEED[self._device["fan_speed"]]
|
return ordered_list_item_to_percentage(
|
||||||
|
ORDERED_NAMED_FAN_SPEEDS, self._device["fan_speed"]
|
||||||
@property
|
)
|
||||||
def speed_list(self) -> list:
|
|
||||||
"""Get the list of available speeds."""
|
|
||||||
return FAN_SPEEDS
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self) -> int:
|
def supported_features(self) -> int:
|
||||||
"""Flag supported features. Speed Only."""
|
"""Flag supported features. Speed Only."""
|
||||||
return SUPPORT_SET_SPEED
|
return SUPPORT_SET_SPEED
|
||||||
|
|
||||||
#
|
|
||||||
# The fan entity model has changed to use percentages and preset_modes
|
|
||||||
# instead of speeds.
|
|
||||||
#
|
|
||||||
# Please review
|
|
||||||
# https://developers.home-assistant.io/docs/core/entity/fan/
|
|
||||||
#
|
|
||||||
async def async_turn_on(
|
async def async_turn_on(
|
||||||
self,
|
self,
|
||||||
speed: str = None,
|
speed: str = None,
|
||||||
@ -90,26 +61,30 @@ class LutronCasetaFan(LutronCasetaDevice, FanEntity):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
"""Turn the fan on."""
|
"""Turn the fan on."""
|
||||||
if speed is None:
|
if percentage is None:
|
||||||
speed = SPEED_MEDIUM
|
percentage = DEFAULT_ON_PERCENTAGE
|
||||||
await self.async_set_speed(speed)
|
|
||||||
|
await self.async_set_percentage(percentage)
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Turn the fan off."""
|
"""Turn the fan off."""
|
||||||
await self.async_set_speed(SPEED_OFF)
|
await self.async_set_percentage(0)
|
||||||
|
|
||||||
async def async_set_speed(self, speed: str) -> None:
|
async def async_set_percentage(self, percentage: int) -> None:
|
||||||
"""Set the speed of the fan."""
|
"""Set the speed of the fan."""
|
||||||
await self._smartbridge.set_fan(self.device_id, SPEED_TO_VALUE[speed])
|
if percentage == 0:
|
||||||
|
named_speed = FAN_OFF
|
||||||
|
else:
|
||||||
|
named_speed = percentage_to_ordered_list_item(
|
||||||
|
ORDERED_NAMED_FAN_SPEEDS, percentage
|
||||||
|
)
|
||||||
|
|
||||||
|
await self._smartbridge.set_fan(self.device_id, named_speed)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return VALUE_TO_SPEED[self._device["fan_speed"]] in [
|
return self.percentage and self.percentage > 0
|
||||||
SPEED_LOW,
|
|
||||||
SPEED_MEDIUM,
|
|
||||||
SPEED_HIGH,
|
|
||||||
]
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update when forcing a refresh of the device."""
|
"""Update when forcing a refresh of the device."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user