Merge branch 'master' into light_scenes

This commit is contained in:
ultratoto14
2020-11-16 08:26:41 +01:00
committed by GitHub Actions
4 changed files with 177 additions and 120 deletions

View File

@@ -30,6 +30,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"

View File

@@ -1,111 +1,156 @@
"""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, from homeassistant.components.fan import (
SPEED_HIGH, DOMAIN,
SPEED_LOW, SPEED_HIGH,
SPEED_MEDIUM, SPEED_LOW,
SPEED_OFF, SPEED_MEDIUM,
SUPPORT_OSCILLATE, SPEED_OFF,
SUPPORT_SET_SPEED, SUPPORT_OSCILLATE,
FanEntity, SUPPORT_SET_SPEED,
) FanEntity,
)
from .common import LocalTuyaEntity, async_setup_entry
from .common import LocalTuyaEntity, async_setup_entry
_LOGGER = logging.getLogger(__name__) from .const import (
CONF_FAN_OSCILLATING_CONTROL,
CONF_FAN_SPEED_CONTROL,
def flow_schema(dps): CONF_FAN_SPEED_HIGH,
"""Return schema used in config flow.""" CONF_FAN_SPEED_LOW,
return {} CONF_FAN_SPEED_MEDIUM,
)
class LocaltuyaFan(LocalTuyaEntity, FanEntity): _LOGGER = logging.getLogger(__name__)
"""Representation of a Tuya fan."""
def __init__( def flow_schema(dps):
self, """Return schema used in config flow."""
device, return {
config_entry, vol.Optional(CONF_FAN_SPEED_CONTROL): vol.In(dps),
fanid, vol.Optional(CONF_FAN_OSCILLATING_CONTROL): vol.In(dps),
**kwargs, vol.Optional(CONF_FAN_SPEED_LOW, default=SPEED_LOW): vol.In(
): [SPEED_LOW, "1", "2"]
"""Initialize the entity.""" ),
super().__init__(device, config_entry, fanid, **kwargs) vol.Optional(CONF_FAN_SPEED_MEDIUM, default=SPEED_MEDIUM): vol.In(
self._is_on = False [SPEED_MEDIUM, "mid", "2", "3"]
self._speed = None ),
self._oscillating = None vol.Optional(CONF_FAN_SPEED_HIGH, default=SPEED_HIGH): vol.In(
[SPEED_HIGH, "auto", "3", "4"]
@property ),
def oscillating(self): }
"""Return current oscillating status."""
return self._oscillating
class LocaltuyaFan(LocalTuyaEntity, FanEntity):
@property """Representation of a Tuya fan."""
def is_on(self):
"""Check if Tuya fan is on.""" def __init__(
return self._is_on self,
device,
@property config_entry,
def speed(self) -> str: fanid,
"""Return the current speed.""" **kwargs,
return self._speed ):
"""Initialize the entity."""
@property super().__init__(device, config_entry, fanid, **kwargs)
def speed_list(self) -> list: self._is_on = False
"""Get the list of available speeds.""" self._speed = None
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] self._oscillating = None
async def async_turn_on(self, speed: str = None, **kwargs) -> None: @property
"""Turn on the entity.""" def oscillating(self):
await self._device.set_dp(True, "1") """Return current oscillating status."""
if speed is not None: return self._oscillating
await self.async_set_speed(speed)
else: @property
self.schedule_update_ha_state() def is_on(self):
"""Check if Tuya fan is on."""
async def async_turn_off(self, **kwargs) -> None: return self._is_on
"""Turn off the entity."""
await self._device.set_dp(False, "1") @property
self.schedule_update_ha_state() def speed(self) -> str:
"""Return the current speed."""
async def async_set_speed(self, speed: str) -> None: return self._speed
"""Set the speed of the fan."""
if speed == SPEED_OFF: @property
await self._device.set_dp(False, "1") def speed_list(self) -> list:
elif speed == SPEED_LOW: """Get the list of available speeds."""
await self._device.set_dp("1", "2") return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
elif speed == SPEED_MEDIUM:
await self._device.set_dp("2", "2") async def async_turn_on(self, speed: str = None, **kwargs) -> None:
elif speed == SPEED_HIGH: """Turn on the entity."""
await self._device.set_dp("3", "2") await self._device.set_dp(True, self._dp_id)
self.schedule_update_ha_state() if speed is not None:
await self.async_set_speed(speed)
async def async_oscillate(self, oscillating: bool) -> None: else:
"""Set oscillation.""" self.schedule_update_ha_state()
await self._device.set_dp(oscillating, "8")
self.schedule_update_ha_state() async def async_turn_off(self, **kwargs) -> None:
"""Turn off the entity."""
@property await self._device.set_dp(False, self._dp_id)
def supported_features(self) -> int: self.schedule_update_ha_state()
"""Flag supported features."""
return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
def status_updated(self): mapping = {
"""Get state of Tuya fan.""" SPEED_LOW: self._config.get(CONF_FAN_SPEED_LOW),
self._is_on = self._status["dps"]["1"] SPEED_MEDIUM: self._config.get(CONF_FAN_SPEED_MEDIUM),
if not self._status["dps"]["1"]: SPEED_HIGH: self._config.get(CONF_FAN_SPEED_HIGH),
self._speed = SPEED_OFF }
elif self._status["dps"]["2"] == "1":
self._speed = SPEED_LOW if speed == SPEED_OFF:
elif self._status["dps"]["2"] == "2": await self._device.set_dp(False, self._dp_id)
self._speed = SPEED_MEDIUM else:
elif self._status["dps"]["2"] == "3": await self._device.set_dp(
self._speed = SPEED_HIGH mapping.get(speed), self._config.get(CONF_FAN_SPEED_CONTROL)
self._oscillating = self._status["dps"]["8"] )
self.schedule_update_ha_state()
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)
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)

View File

@@ -4,7 +4,6 @@ import textwrap
from functools import partial from functools import partial
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
import voluptuous as vol import voluptuous as vol
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
@@ -18,11 +17,7 @@ from homeassistant.components.light import (
SUPPORT_EFFECT, SUPPORT_EFFECT,
LightEntity, LightEntity,
) )
from homeassistant.const import ( from homeassistant.const import CONF_BRIGHTNESS, CONF_COLOR_TEMP, CONF_SCENE
CONF_BRIGHTNESS,
CONF_COLOR_TEMP,
CONF_SCENE,
)
from .common import LocalTuyaEntity, async_setup_entry from .common import LocalTuyaEntity, async_setup_entry
from .const import ( from .const import (

View File

@@ -65,7 +65,12 @@
"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",
"music_mode": "Music mode available", "music_mode": "Music mode available",
"scene": "Scene" "scene": "Scene",
"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"
} }
} }
} }
@@ -111,7 +116,12 @@
"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",
"music_mode": "Music mode available", "music_mode": "Music mode available",
"scene": "Scene" "scene": "Scene",
"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": {
@@ -121,4 +131,4 @@
} }
}, },
"title": "LocalTuya" "title": "LocalTuya"
} }