2019-02-13 20:21:14 +00:00
|
|
|
"""Platform to control a Zehnder ComfoAir Q350/450/600 ventilation unit."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-06-28 16:04:54 +00:00
|
|
|
import logging
|
2021-01-28 16:23:10 +00:00
|
|
|
import math
|
2021-09-24 20:26:56 +00:00
|
|
|
from typing import Any
|
2017-06-28 16:04:54 +00:00
|
|
|
|
2019-10-18 22:01:03 +00:00
|
|
|
from pycomfoconnect import (
|
|
|
|
CMD_FAN_MODE_AWAY,
|
|
|
|
CMD_FAN_MODE_HIGH,
|
|
|
|
CMD_FAN_MODE_LOW,
|
|
|
|
CMD_FAN_MODE_MEDIUM,
|
|
|
|
SENSOR_FAN_SPEED_MODE,
|
|
|
|
)
|
|
|
|
|
2021-01-28 16:23:10 +00:00
|
|
|
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
|
2019-11-16 14:05:17 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-01-28 16:23:10 +00:00
|
|
|
from homeassistant.util.percentage import (
|
2021-02-19 07:05:09 +00:00
|
|
|
int_states_in_range,
|
2021-01-28 16:23:10 +00:00
|
|
|
percentage_to_ranged_value,
|
|
|
|
ranged_value_to_percentage,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import DOMAIN, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, ComfoConnectBridge
|
2017-06-28 16:04:54 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-01-28 16:23:10 +00:00
|
|
|
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
|
2017-06-28 16:04:54 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-06-29 09:44:35 +00:00
|
|
|
"""Set up the ComfoConnect fan platform."""
|
2017-06-28 16:04:54 +00:00
|
|
|
ccb = hass.data[DOMAIN]
|
|
|
|
|
2021-09-24 20:26:56 +00:00
|
|
|
add_entities([ComfoConnectFan(ccb)], True)
|
2017-06-28 16:04:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ComfoConnectFan(FanEntity):
|
2017-06-29 09:44:35 +00:00
|
|
|
"""Representation of the ComfoConnect fan platform."""
|
2017-06-28 16:04:54 +00:00
|
|
|
|
2021-09-24 20:26:56 +00:00
|
|
|
current_speed = None
|
|
|
|
|
|
|
|
def __init__(self, ccb: ComfoConnectBridge) -> None:
|
2017-06-28 16:04:54 +00:00
|
|
|
"""Initialize the ComfoConnect fan."""
|
|
|
|
self._ccb = ccb
|
|
|
|
|
2021-09-24 20:26:56 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-11-16 14:05:17 +00:00
|
|
|
"""Register for sensor updates."""
|
2019-11-17 23:39:49 +00:00
|
|
|
_LOGGER.debug("Registering for fan speed")
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
SIGNAL_COMFOCONNECT_UPDATE_RECEIVED.format(SENSOR_FAN_SPEED_MODE),
|
|
|
|
self._handle_update,
|
|
|
|
)
|
2019-11-17 23:39:49 +00:00
|
|
|
)
|
2019-11-16 14:05:17 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
self._ccb.comfoconnect.register_sensor, SENSOR_FAN_SPEED_MODE
|
|
|
|
)
|
|
|
|
|
2019-11-17 23:39:49 +00:00
|
|
|
def _handle_update(self, value):
|
2019-11-16 14:05:17 +00:00
|
|
|
"""Handle update callbacks."""
|
2019-11-17 23:39:49 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Handle update for fan speed (%d): %s", SENSOR_FAN_SPEED_MODE, value
|
|
|
|
)
|
2021-09-24 20:26:56 +00:00
|
|
|
self.current_speed = value
|
2019-11-17 23:39:49 +00:00
|
|
|
self.schedule_update_ha_state()
|
2017-06-28 16:04:54 +00:00
|
|
|
|
2019-11-16 14:05:17 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
|
|
|
"""Do not poll."""
|
|
|
|
return False
|
2017-06-28 16:04:54 +00:00
|
|
|
|
2019-11-17 23:39:49 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
return self._ccb.unique_id
|
|
|
|
|
2017-06-28 16:04:54 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the fan."""
|
2021-09-24 20:26:56 +00:00
|
|
|
return self._ccb.name
|
2017-06-28 16:04:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:air-conditioner"
|
2017-06-28 16:04:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def percentage(self) -> int | None:
|
2021-01-28 16:23:10 +00:00
|
|
|
"""Return the current speed percentage."""
|
2021-09-24 20:26:56 +00:00
|
|
|
if self.current_speed is None:
|
2019-01-24 07:20:20 +00:00
|
|
|
return None
|
2021-09-24 20:26:56 +00:00
|
|
|
return ranged_value_to_percentage(SPEED_RANGE, self.current_speed)
|
2017-06-28 16:04:54 +00:00
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
@property
|
|
|
|
def speed_count(self) -> int:
|
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
return int_states_in_range(SPEED_RANGE)
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
def turn_on(
|
2021-09-24 20:26:56 +00:00
|
|
|
self,
|
|
|
|
speed: str | None = None,
|
|
|
|
percentage: int | None = None,
|
|
|
|
preset_mode: str | None = None,
|
|
|
|
**kwargs,
|
2021-01-27 23:44:36 +00:00
|
|
|
) -> None:
|
2017-06-28 16:04:54 +00:00
|
|
|
"""Turn on the fan."""
|
2021-09-24 20:26:56 +00:00
|
|
|
if percentage is None:
|
|
|
|
self.set_percentage(1) # Set fan speed to low
|
|
|
|
else:
|
|
|
|
self.set_percentage(percentage)
|
2017-06-28 16:04:54 +00:00
|
|
|
|
2021-09-24 20:26:56 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2017-06-28 16:04:54 +00:00
|
|
|
"""Turn off the fan (to away)."""
|
2021-01-28 16:23:10 +00:00
|
|
|
self.set_percentage(0)
|
|
|
|
|
2021-09-24 20:26:56 +00:00
|
|
|
def set_percentage(self, percentage: int) -> None:
|
2021-01-28 16:23:10 +00:00
|
|
|
"""Set fan speed percentage."""
|
|
|
|
_LOGGER.debug("Changing fan speed percentage to %s", percentage)
|
|
|
|
|
2021-09-24 20:26:56 +00:00
|
|
|
if percentage == 0:
|
2021-01-28 16:23:10 +00:00
|
|
|
cmd = CMD_FAN_MODE_AWAY
|
|
|
|
else:
|
|
|
|
speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
|
|
|
|
cmd = CMD_MAPPING[speed]
|
|
|
|
|
|
|
|
self._ccb.comfoconnect.cmd_rmi_request(cmd)
|