Add platform specific DPS to requests

This commit is contained in:
Pierre Ståhl
2020-10-05 08:15:35 +02:00
committed by rospogrigio
parent 8cc978a04d
commit 8456c6039c
7 changed files with 135 additions and 119 deletions

View File

@@ -65,4 +65,6 @@ class LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity):
) )
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaBinarySensor) async_setup_entry = partial(
async_setup_entry, DOMAIN, LocaltuyaBinarySensor, flow_schema
)

View File

@@ -40,7 +40,7 @@ def prepare_setup_entities(hass, config_entry, platform):
async def async_setup_entry( async def async_setup_entry(
domain, entity_class, hass, config_entry, async_add_entities domain, entity_class, flow_schema, hass, config_entry, async_add_entities
): ):
"""Set up a Tuya platform based on a config entry. """Set up a Tuya platform based on a config entry.
@@ -53,8 +53,15 @@ async def async_setup_entry(
if not entities_to_setup: if not entities_to_setup:
return return
dps_config_fields = list(get_dps_for_platform(flow_schema))
entities = [] entities = []
for device_config in entities_to_setup: for device_config in entities_to_setup:
# Add DPS used by this platform to the request list
for dp_conf in dps_config_fields:
if dp_conf in device_config:
tuyainterface._interface.add_dps_to_request(device_config[dp_conf])
entities.append( entities.append(
entity_class( entity_class(
tuyainterface, tuyainterface,
@@ -66,6 +73,13 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
def get_dps_for_platform(flow_schema):
"""Return config keys for all platform keys that depends on a datapoint."""
for key, value in flow_schema(None).items():
if hasattr(value, "container") and value.container is None:
yield key.schema
def get_entity_config(config_entry, dps_id): def get_entity_config(config_entry, dps_id):
"""Return entity config for a given DPS id.""" """Return entity config for a given DPS id."""
for entity in config_entry.data[CONF_ENTITIES]: for entity in config_entry.data[CONF_ENTITIES]:

View File

@@ -165,4 +165,4 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
self._current_cover_position = 50 self._current_cover_position = 50
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaCover) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaCover, flow_schema)

View File

@@ -1,113 +1,113 @@
"""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 ( from homeassistant.components.fan import (
FanEntity, FanEntity,
DOMAIN, DOMAIN,
SPEED_OFF, SPEED_OFF,
SPEED_LOW, SPEED_LOW,
SPEED_MEDIUM, SPEED_MEDIUM,
SPEED_HIGH, SPEED_HIGH,
SUPPORT_SET_SPEED, SUPPORT_SET_SPEED,
SUPPORT_OSCILLATE, SUPPORT_OSCILLATE,
) )
from .common import LocalTuyaEntity, async_setup_entry from .common import LocalTuyaEntity, async_setup_entry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def flow_schema(dps): def flow_schema(dps):
"""Return schema used in config flow.""" """Return schema used in config flow."""
return {} return {}
class LocaltuyaFan(LocalTuyaEntity, FanEntity): class LocaltuyaFan(LocalTuyaEntity, FanEntity):
"""Representation of a Tuya fan.""" """Representation of a Tuya fan."""
def __init__( def __init__(
self, self,
device, device,
config_entry, config_entry,
fanid, fanid,
**kwargs, **kwargs,
): ):
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(device, config_entry, fanid, **kwargs) super().__init__(device, config_entry, fanid, **kwargs)
self._is_on = False self._is_on = False
self._speed = SPEED_OFF self._speed = SPEED_OFF
self._oscillating = False self._oscillating = False
@property @property
def oscillating(self): def oscillating(self):
"""Return current oscillating status.""" """Return current oscillating status."""
return self._oscillating return self._oscillating
@property @property
def is_on(self): def is_on(self):
"""Check if Tuya fan is on.""" """Check if Tuya fan is on."""
return self._is_on return self._is_on
@property @property
def speed(self) -> str: def speed(self) -> str:
"""Return the current speed.""" """Return the current speed."""
return self._speed return self._speed
@property @property
def speed_list(self) -> list: def speed_list(self) -> list:
"""Get the list of available speeds.""" """Get the list of available speeds."""
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
def turn_on(self, speed: str = None, **kwargs) -> None: def turn_on(self, speed: str = None, **kwargs) -> None:
"""Turn on the entity.""" """Turn on the entity."""
self._device.set_dps(True, "1") self._device.set_dps(True, "1")
if speed is not None: if speed is not None:
self.set_speed(speed) self.set_speed(speed)
else: else:
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self, **kwargs) -> None: def turn_off(self, **kwargs) -> None:
"""Turn off the entity.""" """Turn off the entity."""
self._device.set_dps(False, "1") self._device.set_dps(False, "1")
self.schedule_update_ha_state() self.schedule_update_ha_state()
def set_speed(self, speed: str) -> None: def set_speed(self, speed: str) -> None:
"""Set the speed of the fan.""" """Set the speed of the fan."""
self._speed = speed self._speed = speed
if speed == SPEED_OFF: if speed == SPEED_OFF:
self._device.set_dps(False, "1") self._device.set_dps(False, "1")
elif speed == SPEED_LOW: elif speed == SPEED_LOW:
self._device.set_dps("1", "2") self._device.set_dps("1", "2")
elif speed == SPEED_MEDIUM: elif speed == SPEED_MEDIUM:
self._device.set_dps("2", "2") self._device.set_dps("2", "2")
elif speed == SPEED_HIGH: elif speed == SPEED_HIGH:
self._device.set_dps("3", "2") self._device.set_dps("3", "2")
self.schedule_update_ha_state() self.schedule_update_ha_state()
def oscillate(self, oscillating: bool) -> None: def oscillate(self, oscillating: bool) -> None:
"""Set oscillation.""" """Set oscillation."""
self._oscillating = oscillating self._oscillating = oscillating
self._device.set_value("8", oscillating) self._device.set_value("8", oscillating)
self.schedule_update_ha_state() self.schedule_update_ha_state()
@property @property
def supported_features(self) -> int: def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE
def status_updated(self): def status_updated(self):
"""Get state of Tuya fan.""" """Get state of Tuya fan."""
self._is_on = self._status["dps"]["1"] self._is_on = self._status["dps"]["1"]
if not self._status["dps"]["1"]: if not self._status["dps"]["1"]:
self._speed = SPEED_OFF self._speed = SPEED_OFF
elif self._status["dps"]["2"] == "1": elif self._status["dps"]["2"] == "1":
self._speed = SPEED_LOW self._speed = SPEED_LOW
elif self._status["dps"]["2"] == "2": elif self._status["dps"]["2"] == "2":
self._speed = SPEED_MEDIUM self._speed = SPEED_MEDIUM
elif self._status["dps"]["2"] == "3": elif self._status["dps"]["2"] == "3":
self._speed = SPEED_HIGH self._speed = SPEED_HIGH
self._oscillating = self._status["dps"]["8"] self._oscillating = self._status["dps"]["8"]
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)

View File

@@ -121,4 +121,4 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
self._color_temp = self.dps(DPS_INDEX_COLOURTEMP) self._color_temp = self.dps(DPS_INDEX_COLOURTEMP)
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaLight) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaLight, flow_schema)

View File

@@ -69,4 +69,4 @@ class LocaltuyaSensor(LocalTuyaEntity):
self._state = state self._state = state
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaSensor) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaSensor, flow_schema)

View File

@@ -78,4 +78,4 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
self._state = self.dps(self._dps_id) self._state = self.dps(self._dps_id)
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaSwitch) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaSwitch, flow_schema)