diff --git a/custom_components/localtuya/binary_sensor.py b/custom_components/localtuya/binary_sensor.py index 0011c2f..273880c 100644 --- a/custom_components/localtuya/binary_sensor.py +++ b/custom_components/localtuya/binary_sensor.py @@ -52,9 +52,9 @@ class LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity): return self._config.get(CONF_DEVICE_CLASS) def status_updated(self): + """Device status was updated.""" super().status_updated() - """Device status was updated.""" state = str(self.dps(self._dp_id)).lower() if state == self._config[CONF_STATE_ON].lower(): self._is_on = True @@ -67,7 +67,7 @@ class LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity): # No need to restore state for a sensor async def restore_state_when_connected(self): - """Do nothing for a sensor""" + """Do nothing for a sensor.""" return diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index 0e7fc8b..38a5f40 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -95,7 +95,7 @@ async def async_setup_entry( entity_config[CONF_ID], ) ) - #Once the entities have been created, add to the TuyaDevice instance + # Once the entities have been created, add to the TuyaDevice instance tuyainterface.add_entities(entities) async_add_entities(entities) @@ -150,9 +150,14 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self.dps_to_request[entity[CONF_ID]] = None def add_entities(self, entities): - """Set the entities associated with this device""" + """Set the entities associated with this device.""" self._entities.extend(entities) + @property + def is_connecting(self): + """Return whether device is currently connecting.""" + return self._connect_task is not None + @property def connected(self): """Return if connected to device.""" @@ -184,7 +189,8 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self.status_updated(status) - # Attempt to restore status for all entites that need to first set the DPS value before the device will respond with status. + # Attempt to restore status for all entites that need to first set + # the DPS value before the device will respond with status. for entity in self._entities: await entity.restore_state_when_connected() @@ -326,7 +332,8 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): # Default value is available to be provided by Platform entities if required self._default_value = self._config.get(CONF_DEFAULT_VALUE) - # Restore on connect setting is available to be provided by Platform entities if required + """ Restore on connect setting is available to be provided by Platform entities + if required""" self._restore_on_reconnect = ( self._config.get(CONF_RESTORE_ON_RECONNECT) or False ) @@ -365,8 +372,10 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): @property def extra_state_attributes(self): - """Return entity specific state attributes to be saved & then available for restore - when the entity is restored at startup. + """Return entity specific state attributes to be saved. + + These attributes are then available for restore when the + entity is restored at startup. """ attributes = {} if self._state is not None: @@ -454,7 +463,7 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): # Keep record in last_state as long as not during connection/re-connection, # as last state will be used to restore the previous state - if (state is not None) and (self._device._connect_task is None): + if (state is not None) and (not self._device.is_connecting): self._last_state = state def status_restored(self, stored_state): @@ -464,8 +473,6 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): """ raw_state = stored_state.attributes.get(ATTR_STATE) if raw_state is not None: - # (stored_state.state == "unavailable") | (stored_state.state == "unknown") - # ): self._last_state = raw_state self.debug( "Restoring state for entity: %s - state: %s", @@ -474,7 +481,7 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): ) def default_value(self): - """Default value of this entity + """Return default value of this entity. Override in subclasses to specify the default value for the entity. """ @@ -484,8 +491,8 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): return self._default_value - def entity_default_value(self): - """Default value of the entity type + def entity_default_value(self): # pylint: disable=no-self-use + """Return default value of the entity type. Override in subclasses to specify the default value for the entity. """ @@ -493,14 +500,22 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger): @property def restore_on_reconnect(self): - """Returns whether the last state should be restored on a reconnect - useful where the device loses settings if powered off""" + """Return whether the last state should be restored on a reconnect. + + Useful where the device loses settings if powered off + """ return self._restore_on_reconnect async def restore_state_when_connected(self): - """Restore if restore_on_reconnect is set, or if no status has been yet found - which indicates a DPS that needs to be set before it starts returning status""" + """Restore if restore_on_reconnect is set, or if no status has been yet found. + + Which indicates a DPS that needs to be set before it starts returning + status. + """ if not self.restore_on_reconnect and (str(self._dp_id) in self._status): self.debug( - "Entity %s (DP %d) - Not restoring as restore on reconnect is disabled for this entity and the entity has an initial status", + "Entity %s (DP %d) - Not restoring as restore on reconnect is \ + disabled for this entity and the entity has an initial status", self.name, self._dp_id, ) diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index d8e426f..b9c10f7 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -189,8 +189,9 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): self.debug("Restored cover position %s", self._current_cover_position) def status_updated(self): - super.status_updated(self) """Device status was updated.""" + super.status_updated(self) + self._previous_state = self._state self._state = self.dps(self._dp_id) if self._state.isupper(): diff --git a/custom_components/localtuya/number.py b/custom_components/localtuya/number.py index 85974e8..e8ab53d 100644 --- a/custom_components/localtuya/number.py +++ b/custom_components/localtuya/number.py @@ -8,10 +8,7 @@ from homeassistant.const import CONF_DEVICE_CLASS, STATE_UNKNOWN from .common import LocalTuyaEntity, async_setup_entry -_LOGGER = logging.getLogger(__name__) - from .const import ( - CONF_DEFAULT_VALUE, CONF_MIN_VALUE, CONF_MAX_VALUE, CONF_DEFAULT_VALUE, @@ -19,6 +16,8 @@ from .const import ( CONF_STEPSIZE_VALUE, ) +_LOGGER = logging.getLogger(__name__) + DEFAULT_MIN = 0 DEFAULT_MAX = 100000 DEFAULT_STEP = 1.0 @@ -66,16 +65,14 @@ class LocaltuyaNumber(LocalTuyaEntity, NumberEntity): if CONF_MAX_VALUE in self._config: self._max_value = self._config.get(CONF_MAX_VALUE) - self._step_size = DEFAULT_STEP if CONF_STEPSIZE_VALUE in self._config: self._step_size = self._config.get(CONF_STEPSIZE_VALUE) - #Override standard default value handling to cast to a float + # Override standard default value handling to cast to a float default_value = self._config.get(CONF_DEFAULT_VALUE) if default_value is not None: self._default_value = float(default_value) - @property def native_value(self) -> float: @@ -108,6 +105,7 @@ class LocaltuyaNumber(LocalTuyaEntity, NumberEntity): # Default value is the minimum value def entity_default_value(self): + """Return the minimum value as the default for this entity type.""" return self._min_value diff --git a/custom_components/localtuya/select.py b/custom_components/localtuya/select.py index 71f98cb..75dd217 100644 --- a/custom_components/localtuya/select.py +++ b/custom_components/localtuya/select.py @@ -11,8 +11,6 @@ from homeassistant.const import ( from .common import LocalTuyaEntity, async_setup_entry -_LOGGER = logging.getLogger(__name__) - from .const import ( CONF_OPTIONS, CONF_OPTIONS_FRIENDLY, @@ -31,6 +29,9 @@ def flow_schema(dps): } +_LOGGER = logging.getLogger(__name__) + + class LocaltuyaSelect(LocalTuyaEntity, SelectEntity): """Representation of a Tuya Enumeration.""" @@ -100,8 +101,9 @@ class LocaltuyaSelect(LocalTuyaEntity, SelectEntity): await self._device.set_dp(option_value, self._dp_id) def status_updated(self): - super().status_updated() """Device status was updated.""" + super().status_updated() + state = self.dps(self._dp_id) # Check that received status update for this entity. @@ -116,6 +118,7 @@ class LocaltuyaSelect(LocalTuyaEntity, SelectEntity): # Default value is the first option def entity_default_value(self): + """Return the first option as the default value for this entity type.""" return self._valid_options[0] diff --git a/custom_components/localtuya/sensor.py b/custom_components/localtuya/sensor.py index e769f4e..0eb0ae4 100644 --- a/custom_components/localtuya/sensor.py +++ b/custom_components/localtuya/sensor.py @@ -68,7 +68,7 @@ class LocaltuyaSensor(LocalTuyaEntity): # No need to restore state for a sensor async def restore_state_when_connected(self): - """Do nothing for a sensor""" + """Do nothing for a sensor.""" return diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index c448ff5..40e8ea1 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -82,6 +82,7 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity): # Default value is the "OFF" state def entity_default_value(self): + """Return False as the defaualt value for this entity type.""" return False