Rename set_dps to set_dp in pytuya

This commit is contained in:
Pierre Ståhl
2020-10-14 22:26:50 +02:00
committed by rospogrigio
parent 81c04bfc4e
commit 44db913514
8 changed files with 149 additions and 149 deletions

View File

@@ -54,7 +54,7 @@ class LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity):
def status_updated(self): def status_updated(self):
"""Device status was updated.""" """Device status was updated."""
state = str(self.dps(self._dps_id)).lower() state = str(self.dps(self._dp_id)).lower()
if state == self._config[CONF_STATE_ON].lower(): if state == self._config[CONF_STATE_ON].lower():
self._is_on = True self._is_on = True
elif state == self._config[CONF_STATE_OFF].lower(): elif state == self._config[CONF_STATE_OFF].lower():

View File

@@ -83,12 +83,12 @@ def get_dps_for_platform(flow_schema):
yield key.schema yield key.schema
def get_entity_config(config_entry, dps_id): def get_entity_config(config_entry, dp_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]:
if entity[CONF_ID] == dps_id: if entity[CONF_ID] == dp_id:
return entity return entity
raise Exception(f"missing entity config for id {dps_id}") raise Exception(f"missing entity config for id {dp_id}")
class TuyaDevice(pytuya.TuyaListener): class TuyaDevice(pytuya.TuyaListener):
@@ -171,13 +171,13 @@ class TuyaDevice(pytuya.TuyaListener):
if self._interface: if self._interface:
self._interface.close() self._interface.close()
async def set_dps(self, state, dps_index): async def set_dp(self, state, dp_index):
"""Change value of a DP of the Tuya device.""" """Change value of a DP of the Tuya device."""
if self._interface is not None: if self._interface is not None:
try: try:
await self._interface.set_dps(state, dps_index) await self._interface.set_dp(state, dp_index)
except Exception: except Exception:
_LOGGER.exception("Failed to set DP %d to %d", dps_index, state) _LOGGER.exception("Failed to set DP %d to %d", dp_index, state)
else: else:
_LOGGER.error( _LOGGER.error(
"Not connected to device %s", self._config_entry[CONF_FRIENDLY_NAME] "Not connected to device %s", self._config_entry[CONF_FRIENDLY_NAME]
@@ -208,12 +208,12 @@ class TuyaDevice(pytuya.TuyaListener):
class LocalTuyaEntity(Entity): class LocalTuyaEntity(Entity):
"""Representation of a Tuya entity.""" """Representation of a Tuya entity."""
def __init__(self, device, config_entry, dps_id, **kwargs): def __init__(self, device, config_entry, dp_id, **kwargs):
"""Initialize the Tuya entity.""" """Initialize the Tuya entity."""
self._device = device self._device = device
self._config_entry = config_entry self._config_entry = config_entry
self._config = get_entity_config(config_entry, dps_id) self._config = get_entity_config(config_entry, dp_id)
self._dps_id = dps_id self._dp_id = dp_id
self._status = {} self._status = {}
async def async_added_to_hass(self): async def async_added_to_hass(self):
@@ -262,7 +262,7 @@ class LocalTuyaEntity(Entity):
@property @property
def unique_id(self): def unique_id(self):
"""Return unique device identifier.""" """Return unique device identifier."""
return f"local_{self._config_entry.data[CONF_DEVICE_ID]}_{self._dps_id}" return f"local_{self._config_entry.data[CONF_DEVICE_ID]}_{self._dp_id}"
def has_config(self, attr): def has_config(self, attr):
"""Return if a config parameter has a valid value.""" """Return if a config parameter has a valid value."""
@@ -272,16 +272,16 @@ class LocalTuyaEntity(Entity):
@property @property
def available(self): def available(self):
"""Return if device is available or not.""" """Return if device is available or not."""
return str(self._dps_id) in self._status return str(self._dp_id) in self._status
def dps(self, dps_index): def dps(self, dp_index):
"""Return cached value for DPS index.""" """Return cached value for DPS index."""
value = self._status.get(str(dps_index)) value = self._status.get(str(dp_index))
if value is None: if value is None:
_LOGGER.warning( _LOGGER.warning(
"Entity %s is requesting unknown DPS index %s", "Entity %s is requesting unknown DPS index %s",
self.entity_id, self.entity_id,
dps_index, dp_index,
) )
return value return value

View File

@@ -135,28 +135,28 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
elif self._config[CONF_POSITIONING_MODE] == COVER_MODE_POSITION: elif self._config[CONF_POSITIONING_MODE] == COVER_MODE_POSITION:
converted_position = int(kwargs[ATTR_POSITION]) converted_position = int(kwargs[ATTR_POSITION])
if 0 <= converted_position <= 100 and self.has_config(CONF_SET_POSITION_DP): if 0 <= converted_position <= 100 and self.has_config(CONF_SET_POSITION_DP):
await self._device.set_dps( await self._device.set_dp(
converted_position, self._config[CONF_SET_POSITION_DP] converted_position, self._config[CONF_SET_POSITION_DP]
) )
async def async_open_cover(self, **kwargs): async def async_open_cover(self, **kwargs):
"""Open the cover.""" """Open the cover."""
_LOGGER.debug("Launching command %s to cover ", self._open_cmd) _LOGGER.debug("Launching command %s to cover ", self._open_cmd)
await self._device.set_dps(self._open_cmd, self._dps_id) await self._device.set_dp(self._open_cmd, self._dp_id)
async def async_close_cover(self, **kwargs): async def async_close_cover(self, **kwargs):
"""Close cover.""" """Close cover."""
_LOGGER.debug("Launching command %s to cover ", self._close_cmd) _LOGGER.debug("Launching command %s to cover ", self._close_cmd)
await self._device.set_dps(self._close_cmd, self._dps_id) await self._device.set_dp(self._close_cmd, self._dp_id)
async def async_stop_cover(self, **kwargs): async def async_stop_cover(self, **kwargs):
"""Stop the cover.""" """Stop the cover."""
_LOGGER.debug("Launching command %s to cover ", COVER_STOP_CMD) _LOGGER.debug("Launching command %s to cover ", COVER_STOP_CMD)
await self._device.set_dps(COVER_STOP_CMD, self._dps_id) await self._device.set_dp(COVER_STOP_CMD, self._dp_id)
def status_updated(self): def status_updated(self):
"""Device status was updated.""" """Device status was updated."""
self._state = self.dps(self._dps_id) self._state = self.dps(self._dp_id)
if self.has_config(CONF_CURRENT_POSITION_DP): if self.has_config(CONF_CURRENT_POSITION_DP):
self._current_cover_position = self.dps( self._current_cover_position = self.dps(
self._config[CONF_CURRENT_POSITION_DP] self._config[CONF_CURRENT_POSITION_DP]

View File

@@ -61,7 +61,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
async def async_turn_on(self, speed: str = None, **kwargs) -> None: async def async_turn_on(self, speed: str = None, **kwargs) -> None:
"""Turn on the entity.""" """Turn on the entity."""
await self._device.set_dps(True, "1") await self._device.set_dp(True, "1")
if speed is not None: if speed is not None:
await self.async_set_speed(speed) await self.async_set_speed(speed)
else: else:
@@ -69,24 +69,24 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
async def async_turn_off(self, **kwargs) -> None: async def async_turn_off(self, **kwargs) -> None:
"""Turn off the entity.""" """Turn off the entity."""
await self._device.set_dps(False, "1") await self._device.set_dp(False, "1")
self.schedule_update_ha_state() self.schedule_update_ha_state()
async def async_set_speed(self, speed: str) -> None: async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan.""" """Set the speed of the fan."""
if speed == SPEED_OFF: if speed == SPEED_OFF:
await self._device.set_dps(False, "1") await self._device.set_dp(False, "1")
elif speed == SPEED_LOW: elif speed == SPEED_LOW:
await self._device.set_dps("1", "2") await self._device.set_dp("1", "2")
elif speed == SPEED_MEDIUM: elif speed == SPEED_MEDIUM:
await self._device.set_dps("2", "2") await self._device.set_dp("2", "2")
elif speed == SPEED_HIGH: elif speed == SPEED_HIGH:
await self._device.set_dps("3", "2") await self._device.set_dp("3", "2")
self.schedule_update_ha_state() self.schedule_update_ha_state()
async def async_oscillate(self, oscillating: bool) -> None: async def async_oscillate(self, oscillating: bool) -> None:
"""Set oscillation.""" """Set oscillation."""
await self._device.set_dps(oscillating, "8") await self._device.set_dp(oscillating, "8")
self.schedule_update_ha_state() self.schedule_update_ha_state()
@property @property

View File

@@ -88,10 +88,10 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn on or control the light.""" """Turn on or control the light."""
await self._device.set_dps(True, self._dps_id) await self._device.set_dp(True, self._dp_id)
if ATTR_BRIGHTNESS in kwargs: if ATTR_BRIGHTNESS in kwargs:
await self._device.set_dps( await self._device.set_dp(
max(int(kwargs[ATTR_BRIGHTNESS]), 25), self._config.get(CONF_BRIGHTNESS) max(int(kwargs[ATTR_BRIGHTNESS]), 25), self._config.get(CONF_BRIGHTNESS)
) )
@@ -104,15 +104,15 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity):
- (255 / (MAX_MIRED - MIN_MIRED)) - (255 / (MAX_MIRED - MIN_MIRED))
* (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED) * (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED)
) )
await self._device.set_dps(color_temp, self._config.get(CONF_COLOR_TEMP)) await self._device.set_dp(color_temp, self._config.get(CONF_COLOR_TEMP))
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs):
"""Turn Tuya light off.""" """Turn Tuya light off."""
await self._device.set_dps(False, self._dps_id) await self._device.set_dp(False, self._dp_id)
def status_updated(self): def status_updated(self):
"""Device status was updated.""" """Device status was updated."""
self._state = self.dps(self._dps_id) self._state = self.dps(self._dp_id)
brightness = self.dps_conf(CONF_BRIGHTNESS) brightness = self.dps_conf(CONF_BRIGHTNESS)
if brightness is not None: if brightness is not None:

View File

@@ -21,9 +21,9 @@ Functions
json = status() # returns json payload json = status() # returns json payload
set_version(version) # 3.1 [default] or 3.3 set_version(version) # 3.1 [default] or 3.3
detect_available_dps() # returns a list of available dps provided by the device detect_available_dps() # returns a list of available dps provided by the device
add_dps_to_request(dps_index) # adds dps_index to the list of dps used by the add_dps_to_request(dp_index) # adds dp_index to the list of dps used by the
# device (to be queried in the payload) # device (to be queried in the payload)
set_dps(on, dps_index) # Set value of any dps index. set_dp(on, dp_index) # Set value of any dps index.
Credits Credits
@@ -428,15 +428,15 @@ class TuyaProtocol(asyncio.Protocol):
"""Send a heartbeat message.""" """Send a heartbeat message."""
return await self.exchange(HEARTBEAT) return await self.exchange(HEARTBEAT)
async def set_dps(self, value, dps_index): async def set_dp(self, value, dp_index):
""" """
Set value (may be any type: bool, int or string) of any dps index. Set value (may be any type: bool, int or string) of any dps index.
Args: Args:
dps_index(int): dps index to set dp_index(int): dps index to set
value: new value for the dps index value: new value for the dps index
""" """
return await self.exchange(SET, {str(dps_index): value}) return await self.exchange(SET, {str(dp_index): value})
async def detect_available_dps(self): async def detect_available_dps(self):
"""Return which datapoints are supported by the device.""" """Return which datapoints are supported by the device."""
@@ -465,12 +465,12 @@ class TuyaProtocol(asyncio.Protocol):
self.log.debug("detected dps: %s", self.dps_cache) self.log.debug("detected dps: %s", self.dps_cache)
return self.dps_cache return self.dps_cache
def add_dps_to_request(self, dps_index): def add_dps_to_request(self, dp_indicies):
"""Add a datapoint (DP) to be included in requests.""" """Add a datapoint (DP) to be included in requests."""
if isinstance(dps_index, int): if isinstance(dp_indicies, int):
self.dps_to_request[str(dps_index)] = None self.dps_to_request[str(dp_indicies)] = None
else: else:
self.dps_to_request.update({str(index): None for index in dps_index}) self.dps_to_request.update({str(index): None for index in dp_indicies})
def _decode_payload(self, payload): def _decode_payload(self, payload):
self.log.debug("Decode payload: %s", payload) self.log.debug("Decode payload: %s", payload)

View File

@@ -62,7 +62,7 @@ class LocaltuyaSensor(LocalTuyaEntity):
def status_updated(self): def status_updated(self):
"""Device status was updated.""" """Device status was updated."""
state = self.dps(self._dps_id) state = self.dps(self._dp_id)
scale_factor = self._config.get(CONF_SCALING) scale_factor = self._config.get(CONF_SCALING)
if scale_factor is not None: if scale_factor is not None:
state = round(state * scale_factor, DEFAULT_PRECISION) state = round(state * scale_factor, DEFAULT_PRECISION)

View File

@@ -67,15 +67,15 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn Tuya switch on.""" """Turn Tuya switch on."""
await self._device.set_dps(True, self._dps_id) await self._device.set_dp(True, self._dp_id)
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs):
"""Turn Tuya switch off.""" """Turn Tuya switch off."""
await self._device.set_dps(False, self._dps_id) await self._device.set_dp(False, self._dp_id)
def status_updated(self): def status_updated(self):
"""Device status was updated.""" """Device status was updated."""
self._state = self.dps(self._dps_id) self._state = self.dps(self._dp_id)
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaSwitch, flow_schema) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaSwitch, flow_schema)