Fan controller updates (#155)
* Fan controller updates. This updates the fan controller to be more dynamic on which dp's it uses. Fan speeds can 1,2,3,4 or low,medium,mid,high,auto. Fans with more than 3 speeds is not supported by Home Assistant. Variable speed fans are not supported at this time. * rename set_dps -> set_dp * change to use self._dp_id instead of a fixed dp Co-authored-by: Thomas Davis <tdavis@nersc.gov> Co-authored-by: Pierre Ståhl <pierre.staahl@gmail.com>
This commit is contained in:
@@ -29,6 +29,13 @@ CONF_SET_POSITION_DP = "set_position_dp"
|
|||||||
CONF_POSITION_INVERTED = "position_inverted"
|
CONF_POSITION_INVERTED = "position_inverted"
|
||||||
CONF_SPAN_TIME = "span_time"
|
CONF_SPAN_TIME = "span_time"
|
||||||
|
|
||||||
|
# fan
|
||||||
|
CONF_FAN_SPEED_CONTROL = "fan_speed_control"
|
||||||
|
CONF_FAN_OSCILLATING_CONTROL = "fan_oscillating_control"
|
||||||
|
CONF_FAN_SPEED_LOW = "fan_speed_low"
|
||||||
|
CONF_FAN_SPEED_MEDIUM = "fan_speed_medium"
|
||||||
|
CONF_FAN_SPEED_HIGH = "fan_speed_high"
|
||||||
|
|
||||||
# sensor
|
# sensor
|
||||||
CONF_SCALING = "scaling"
|
CONF_SCALING = "scaling"
|
||||||
|
|
||||||
|
@@ -1,111 +1,159 @@
|
|||||||
"""Platform to locally control Tuya-based fan devices."""
|
"""Platform to locally control Tuya-based fan devices."""
|
||||||
import logging
|
import logging
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from homeassistant.components.fan import (
|
import voluptuous as vol
|
||||||
DOMAIN,
|
|
||||||
SPEED_HIGH,
|
from homeassistant.components.fan import (
|
||||||
SPEED_LOW,
|
FanEntity,
|
||||||
SPEED_MEDIUM,
|
DOMAIN,
|
||||||
SPEED_OFF,
|
SPEED_OFF,
|
||||||
SUPPORT_OSCILLATE,
|
SPEED_LOW,
|
||||||
SUPPORT_SET_SPEED,
|
SPEED_MEDIUM,
|
||||||
FanEntity,
|
SPEED_HIGH,
|
||||||
)
|
SUPPORT_SET_SPEED,
|
||||||
|
SUPPORT_OSCILLATE,
|
||||||
from .common import LocalTuyaEntity, async_setup_entry
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
from .const import (
|
||||||
|
CONF_FAN_SPEED_CONTROL,
|
||||||
|
CONF_FAN_OSCILLATING_CONTROL,
|
||||||
def flow_schema(dps):
|
CONF_FAN_SPEED_LOW,
|
||||||
"""Return schema used in config flow."""
|
CONF_FAN_SPEED_MEDIUM,
|
||||||
return {}
|
CONF_FAN_SPEED_HIGH,
|
||||||
|
)
|
||||||
|
|
||||||
class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|
||||||
"""Representation of a Tuya fan."""
|
from .common import LocalTuyaEntity, async_setup_entry
|
||||||
|
|
||||||
def __init__(
|
_LOGGER = logging.getLogger(__name__)
|
||||||
self,
|
|
||||||
device,
|
|
||||||
config_entry,
|
def flow_schema(dps):
|
||||||
fanid,
|
"""Return schema used in config flow."""
|
||||||
**kwargs,
|
return {
|
||||||
):
|
vol.Optional(CONF_FAN_SPEED_CONTROL): vol.In(dps),
|
||||||
"""Initialize the entity."""
|
vol.Optional(CONF_FAN_OSCILLATING_CONTROL): vol.In(dps),
|
||||||
super().__init__(device, config_entry, fanid, **kwargs)
|
vol.Optional(CONF_FAN_SPEED_LOW, default=SPEED_LOW): vol.In(
|
||||||
self._is_on = False
|
[SPEED_LOW, "1", "2"]
|
||||||
self._speed = None
|
),
|
||||||
self._oscillating = None
|
vol.Optional(CONF_FAN_SPEED_MEDIUM, default=SPEED_MEDIUM): vol.In(
|
||||||
|
[SPEED_MEDIUM, "mid", "2", "3"]
|
||||||
@property
|
),
|
||||||
def oscillating(self):
|
vol.Optional(CONF_FAN_SPEED_HIGH, default=SPEED_HIGH): vol.In(
|
||||||
"""Return current oscillating status."""
|
[SPEED_HIGH, "auto", "3", "4"]
|
||||||
return self._oscillating
|
),
|
||||||
|
}
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Check if Tuya fan is on."""
|
class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
||||||
return self._is_on
|
"""Representation of a Tuya fan."""
|
||||||
|
|
||||||
@property
|
def __init__(
|
||||||
def speed(self) -> str:
|
self,
|
||||||
"""Return the current speed."""
|
device,
|
||||||
return self._speed
|
config_entry,
|
||||||
|
fanid,
|
||||||
@property
|
**kwargs,
|
||||||
def speed_list(self) -> list:
|
):
|
||||||
"""Get the list of available speeds."""
|
"""Initialize the entity."""
|
||||||
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
super().__init__(device, config_entry, fanid, **kwargs)
|
||||||
|
self._is_on = False
|
||||||
async def async_turn_on(self, speed: str = None, **kwargs) -> None:
|
self._speed = None
|
||||||
"""Turn on the entity."""
|
self._oscillating = None
|
||||||
await self._device.set_dp(True, "1")
|
|
||||||
if speed is not None:
|
@property
|
||||||
await self.async_set_speed(speed)
|
def oscillating(self):
|
||||||
else:
|
"""Return current oscillating status."""
|
||||||
self.schedule_update_ha_state()
|
return self._oscillating
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs) -> None:
|
@property
|
||||||
"""Turn off the entity."""
|
def is_on(self):
|
||||||
await self._device.set_dp(False, "1")
|
"""Check if Tuya fan is on."""
|
||||||
self.schedule_update_ha_state()
|
return self._is_on
|
||||||
|
|
||||||
async def async_set_speed(self, speed: str) -> None:
|
@property
|
||||||
"""Set the speed of the fan."""
|
def speed(self) -> str:
|
||||||
if speed == SPEED_OFF:
|
"""Return the current speed."""
|
||||||
await self._device.set_dp(False, "1")
|
return self._speed
|
||||||
elif speed == SPEED_LOW:
|
|
||||||
await self._device.set_dp("1", "2")
|
@property
|
||||||
elif speed == SPEED_MEDIUM:
|
def speed_list(self) -> list:
|
||||||
await self._device.set_dp("2", "2")
|
"""Get the list of available speeds."""
|
||||||
elif speed == SPEED_HIGH:
|
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
||||||
await self._device.set_dp("3", "2")
|
|
||||||
self.schedule_update_ha_state()
|
async def async_turn_on(self, speed: str = None, **kwargs) -> None:
|
||||||
|
"""Turn on the entity."""
|
||||||
async def async_oscillate(self, oscillating: bool) -> None:
|
await self._device.set_dp(True, self._dp_id)
|
||||||
"""Set oscillation."""
|
if speed is not None:
|
||||||
await self._device.set_dp(oscillating, "8")
|
await self.async_set_speed(speed)
|
||||||
self.schedule_update_ha_state()
|
else:
|
||||||
|
self.schedule_update_ha_state()
|
||||||
@property
|
|
||||||
def supported_features(self) -> int:
|
async def async_turn_off(self, **kwargs) -> None:
|
||||||
"""Flag supported features."""
|
"""Turn off the entity."""
|
||||||
return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE
|
await self._device.set_dp(False, self._dp_id)
|
||||||
|
self.schedule_update_ha_state()
|
||||||
def status_updated(self):
|
|
||||||
"""Get state of Tuya fan."""
|
async def async_set_speed(self, speed: str) -> None:
|
||||||
self._is_on = self._status["dps"]["1"]
|
"""Set the speed of the fan."""
|
||||||
if not self._status["dps"]["1"]:
|
mapping = {
|
||||||
self._speed = SPEED_OFF
|
SPEED_LOW: self._config.get(CONF_FAN_SPEED_LOW),
|
||||||
elif self._status["dps"]["2"] == "1":
|
SPEED_MEDIUM: self._config.get(CONF_FAN_SPEED_MEDIUM),
|
||||||
self._speed = SPEED_LOW
|
SPEED_HIGH: self._config.get(CONF_FAN_SPEED_HIGH),
|
||||||
elif self._status["dps"]["2"] == "2":
|
}
|
||||||
self._speed = SPEED_MEDIUM
|
|
||||||
elif self._status["dps"]["2"] == "3":
|
if speed == SPEED_OFF:
|
||||||
self._speed = SPEED_HIGH
|
await self._device.set_dp(False, self._dp_id)
|
||||||
self._oscillating = self._status["dps"]["8"]
|
else:
|
||||||
|
await self._device.set_dp(
|
||||||
|
mapping.get(speed), self._config.get(CONF_FAN_SPEED_CONTROL)
|
||||||
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)
|
)
|
||||||
|
|
||||||
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
async def async_oscillate(self, oscillating: bool) -> None:
|
||||||
|
"""Set oscillation."""
|
||||||
|
await self._device.set_dp(
|
||||||
|
oscillating, self._config.get(CONF_FAN_OSCILLATING_CONTROL)
|
||||||
|
)
|
||||||
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def supported_features(self) -> int:
|
||||||
|
"""Flag supported features."""
|
||||||
|
supports = 0
|
||||||
|
|
||||||
|
if self.has_config(CONF_FAN_OSCILLATING_CONTROL):
|
||||||
|
supports |= SUPPORT_OSCILLATE
|
||||||
|
if self.has_config(CONF_FAN_SPEED_CONTROL):
|
||||||
|
supports |= SUPPORT_SET_SPEED
|
||||||
|
|
||||||
|
return supports
|
||||||
|
|
||||||
|
def status_updated(self):
|
||||||
|
"""Get state of Tuya fan."""
|
||||||
|
mappings = {
|
||||||
|
self._config.get(CONF_FAN_SPEED_LOW): SPEED_LOW,
|
||||||
|
self._config.get(CONF_FAN_SPEED_MEDIUM): SPEED_MEDIUM,
|
||||||
|
self._config.get(CONF_FAN_SPEED_HIGH): SPEED_HIGH,
|
||||||
|
}
|
||||||
|
|
||||||
|
self._is_on = self.dps(self._dps_id)
|
||||||
|
|
||||||
|
if self.has_config(CONF_FAN_SPEED_CONTROL):
|
||||||
|
self._speed = mappings.get(self.dps_conf(CONF_FAN_SPEED_CONTROL))
|
||||||
|
if self.speed is None:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"%s/%s: Ignoring unknown fan controller state: %s",
|
||||||
|
self.name,
|
||||||
|
self.entity_id,
|
||||||
|
self.dps_conf(CONF_FAN_SPEED_CONTROL),
|
||||||
|
)
|
||||||
|
self._speed = None
|
||||||
|
|
||||||
|
if self.has_config(CONF_FAN_OSCILLATING_CONTROL):
|
||||||
|
self._oscillating = self.dps_conf(CONF_FAN_OSCILLATING_CONTROL)
|
||||||
|
|
||||||
|
|
||||||
|
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)
|
||||||
|
@@ -63,7 +63,12 @@
|
|||||||
"color": "Color",
|
"color": "Color",
|
||||||
"color_mode": "Color Mode",
|
"color_mode": "Color Mode",
|
||||||
"color_temp_min_kelvin": "Minimum Color Temperature in K",
|
"color_temp_min_kelvin": "Minimum Color Temperature in K",
|
||||||
"color_temp_max_kelvin": "Maximum Color Temperature in K"
|
"color_temp_max_kelvin": "Maximum Color Temperature in K",
|
||||||
|
"fan_speed_control": "Fan Speed Control",
|
||||||
|
"fan_oscillating_control": "Fan Oscillating Control",
|
||||||
|
"fan_speed_low": "Fan Low Speed Setting",
|
||||||
|
"fan_speed_medium": "Fan Medium Speed Setting",
|
||||||
|
"fan_speed_high": "Fan High Speed Setting"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,7 +112,12 @@
|
|||||||
"color": "Color",
|
"color": "Color",
|
||||||
"color_mode": "Color Mode",
|
"color_mode": "Color Mode",
|
||||||
"color_temp_min_kelvin": "Minimum Color Temperature in K",
|
"color_temp_min_kelvin": "Minimum Color Temperature in K",
|
||||||
"color_temp_max_kelvin": "Maximum Color Temperature in K"
|
"color_temp_max_kelvin": "Maximum Color Temperature in K",
|
||||||
|
"fan_speed_control": "Fan Speed Control",
|
||||||
|
"fan_oscillating_control": "Fan Oscillating Control",
|
||||||
|
"fan_speed_low": "Fan Low Speed Setting",
|
||||||
|
"fan_speed_medium": "Fan Medium Speed Setting",
|
||||||
|
"fan_speed_high": "Fan High Speed Setting"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"yaml_import": {
|
"yaml_import": {
|
||||||
@@ -117,4 +127,4 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"title": "LocalTuya"
|
"title": "LocalTuya"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user