mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Update comfoconnect to use new fan entity model (#45593)
This commit is contained in:
parent
d7e0391e03
commit
d148f9aa85
@ -1,5 +1,6 @@
|
|||||||
"""Platform to control a Zehnder ComfoAir Q350/450/600 ventilation unit."""
|
"""Platform to control a Zehnder ComfoAir Q350/450/600 ventilation unit."""
|
||||||
import logging
|
import logging
|
||||||
|
import math
|
||||||
|
|
||||||
from pycomfoconnect import (
|
from pycomfoconnect import (
|
||||||
CMD_FAN_MODE_AWAY,
|
CMD_FAN_MODE_AWAY,
|
||||||
@ -9,21 +10,25 @@ from pycomfoconnect import (
|
|||||||
SENSOR_FAN_SPEED_MODE,
|
SENSOR_FAN_SPEED_MODE,
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.components.fan import (
|
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
|
||||||
SPEED_HIGH,
|
|
||||||
SPEED_LOW,
|
|
||||||
SPEED_MEDIUM,
|
|
||||||
SPEED_OFF,
|
|
||||||
SUPPORT_SET_SPEED,
|
|
||||||
FanEntity,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.util.percentage import (
|
||||||
|
percentage_to_ranged_value,
|
||||||
|
ranged_value_to_percentage,
|
||||||
|
)
|
||||||
|
|
||||||
from . import DOMAIN, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, ComfoConnectBridge
|
from . import DOMAIN, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, ComfoConnectBridge
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SPEED_MAPPING = {0: SPEED_OFF, 1: SPEED_LOW, 2: SPEED_MEDIUM, 3: SPEED_HIGH}
|
CMD_MAPPING = {
|
||||||
|
0: CMD_FAN_MODE_AWAY,
|
||||||
|
1: CMD_FAN_MODE_LOW,
|
||||||
|
2: CMD_FAN_MODE_MEDIUM,
|
||||||
|
3: CMD_FAN_MODE_HIGH,
|
||||||
|
}
|
||||||
|
|
||||||
|
SPEED_RANGE = (1, 3) # away is not included in speeds and instead mapped to off
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
@ -89,50 +94,36 @@ class ComfoConnectFan(FanEntity):
|
|||||||
return SUPPORT_SET_SPEED
|
return SUPPORT_SET_SPEED
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def speed(self):
|
def percentage(self) -> str:
|
||||||
"""Return the current fan mode."""
|
"""Return the current speed percentage."""
|
||||||
try:
|
speed = self._ccb.data[SENSOR_FAN_SPEED_MODE]
|
||||||
speed = self._ccb.data[SENSOR_FAN_SPEED_MODE]
|
if speed is None:
|
||||||
return SPEED_MAPPING[speed]
|
|
||||||
except KeyError:
|
|
||||||
return None
|
return None
|
||||||
|
return ranged_value_to_percentage(SPEED_RANGE, speed)
|
||||||
|
|
||||||
@property
|
|
||||||
def speed_list(self):
|
|
||||||
"""List of available fan modes."""
|
|
||||||
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
||||||
|
|
||||||
#
|
|
||||||
# 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/
|
|
||||||
#
|
|
||||||
def turn_on(
|
def turn_on(
|
||||||
self, speed: str = None, percentage=None, preset_mode=None, **kwargs
|
self, speed: str = None, percentage=None, preset_mode=None, **kwargs
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Turn on the fan."""
|
"""Turn on the fan."""
|
||||||
if speed is None:
|
self.set_percentage(percentage)
|
||||||
speed = SPEED_LOW
|
|
||||||
self.set_speed(speed)
|
|
||||||
|
|
||||||
def turn_off(self, **kwargs) -> None:
|
def turn_off(self, **kwargs) -> None:
|
||||||
"""Turn off the fan (to away)."""
|
"""Turn off the fan (to away)."""
|
||||||
self.set_speed(SPEED_OFF)
|
self.set_percentage(0)
|
||||||
|
|
||||||
def set_speed(self, speed: str):
|
def set_percentage(self, percentage: int):
|
||||||
"""Set fan speed."""
|
"""Set fan speed percentage."""
|
||||||
_LOGGER.debug("Changing fan speed to %s", speed)
|
_LOGGER.debug("Changing fan speed percentage to %s", percentage)
|
||||||
|
|
||||||
if speed == SPEED_OFF:
|
if percentage is None:
|
||||||
self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_AWAY)
|
cmd = CMD_FAN_MODE_LOW
|
||||||
elif speed == SPEED_LOW:
|
elif percentage == 0:
|
||||||
self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_LOW)
|
cmd = CMD_FAN_MODE_AWAY
|
||||||
elif speed == SPEED_MEDIUM:
|
else:
|
||||||
self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_MEDIUM)
|
speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
|
||||||
elif speed == SPEED_HIGH:
|
cmd = CMD_MAPPING[speed]
|
||||||
self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_HIGH)
|
|
||||||
|
self._ccb.comfoconnect.cmd_rmi_request(cmd)
|
||||||
|
|
||||||
# Update current mode
|
# Update current mode
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user