2019-06-06 22:23:00 +00:00
|
|
|
"""Platform to control a Salda Smarty XP/XV ventilation unit."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.fan import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2019-12-01 05:24:17 +00:00
|
|
|
from homeassistant.core import callback
|
2019-06-06 22:23:00 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from . import DOMAIN, SIGNAL_UPDATE_SMARTY
|
2019-06-06 22:23:00 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SPEED_LIST = [SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_MAPPING = {1: SPEED_LOW, 2: SPEED_MEDIUM, 3: SPEED_HIGH}
|
2019-06-06 22:23:00 +00:00
|
|
|
SPEED_TO_MODE = {v: k for k, v in SPEED_MAPPING.items()}
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-06-06 22:23:00 +00:00
|
|
|
"""Set up the Smarty Fan Platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
smarty = hass.data[DOMAIN]["api"]
|
|
|
|
name = hass.data[DOMAIN]["name"]
|
2019-06-06 22:23:00 +00:00
|
|
|
|
|
|
|
async_add_entities([SmartyFan(name, smarty)], True)
|
|
|
|
|
|
|
|
|
|
|
|
class SmartyFan(FanEntity):
|
|
|
|
"""Representation of a Smarty Fan."""
|
|
|
|
|
|
|
|
def __init__(self, name, smarty):
|
|
|
|
"""Initialize the entity."""
|
|
|
|
self._name = name
|
|
|
|
self._speed = SPEED_OFF
|
|
|
|
self._state = None
|
|
|
|
self._smarty = smarty
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Do not poll."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the fan."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:air-conditioner"
|
2019-06-06 22:23:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Return the list of supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed_list(self):
|
|
|
|
"""List of available fan modes."""
|
|
|
|
return SPEED_LIST
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return state of the fan."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed(self) -> str:
|
|
|
|
"""Return speed of the fan."""
|
|
|
|
return self._speed
|
|
|
|
|
2020-03-04 09:09:33 +00:00
|
|
|
def set_speed(self, speed: str) -> None:
|
|
|
|
"""Set the speed of the fan."""
|
|
|
|
_LOGGER.debug("Set the fan speed to %s", speed)
|
|
|
|
if speed == SPEED_OFF:
|
|
|
|
self.turn_off()
|
|
|
|
else:
|
|
|
|
self._smarty.set_fan_speed(SPEED_TO_MODE.get(speed))
|
|
|
|
self._speed = speed
|
|
|
|
self._state = True
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
#
|
|
|
|
# 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(self, speed=None, percentage=None, preset_mode=None, **kwargs):
|
2019-06-06 22:23:00 +00:00
|
|
|
"""Turn on the fan."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Turning on fan. Speed is %s", speed)
|
2019-06-06 22:23:00 +00:00
|
|
|
if speed is None:
|
|
|
|
if self._smarty.turn_on(SPEED_TO_MODE.get(self._speed)):
|
|
|
|
self._state = True
|
|
|
|
self._speed = SPEED_MEDIUM
|
|
|
|
else:
|
|
|
|
if self._smarty.set_fan_speed(SPEED_TO_MODE.get(speed)):
|
|
|
|
self._speed = speed
|
|
|
|
self._state = True
|
|
|
|
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn off the fan."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Turning off fan")
|
2019-06-06 22:23:00 +00:00
|
|
|
if self._smarty.turn_off():
|
|
|
|
self._state = False
|
|
|
|
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Call to update fan."""
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect(self.hass, SIGNAL_UPDATE_SMARTY, self._update_callback)
|
2019-06-06 22:23:00 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Updating state")
|
2019-06-06 22:23:00 +00:00
|
|
|
result = self._smarty.fan_speed
|
|
|
|
if result:
|
|
|
|
self._speed = SPEED_MAPPING[result]
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Speed is %s, Mode is %s", self._speed, result)
|
2019-06-06 22:23:00 +00:00
|
|
|
self._state = True
|
|
|
|
else:
|
|
|
|
self._state = False
|