Fixing formatting/style errors

This commit is contained in:
sibowler
2022-07-14 08:59:54 +10:00
parent 06968d8a9c
commit f85b25dea9
7 changed files with 46 additions and 28 deletions

View File

@@ -52,9 +52,9 @@ class LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity):
return self._config.get(CONF_DEVICE_CLASS) return self._config.get(CONF_DEVICE_CLASS)
def status_updated(self): def status_updated(self):
"""Device status was updated."""
super().status_updated() super().status_updated()
"""Device status was updated."""
state = str(self.dps(self._dp_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
@@ -67,7 +67,7 @@ class LocaltuyaBinarySensor(LocalTuyaEntity, BinarySensorEntity):
# No need to restore state for a sensor # No need to restore state for a sensor
async def restore_state_when_connected(self): async def restore_state_when_connected(self):
"""Do nothing for a sensor""" """Do nothing for a sensor."""
return return

View File

@@ -95,7 +95,7 @@ async def async_setup_entry(
entity_config[CONF_ID], 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) tuyainterface.add_entities(entities)
async_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 self.dps_to_request[entity[CONF_ID]] = None
def add_entities(self, entities): def add_entities(self, entities):
"""Set the entities associated with this device""" """Set the entities associated with this device."""
self._entities.extend(entities) self._entities.extend(entities)
@property
def is_connecting(self):
"""Return whether device is currently connecting."""
return self._connect_task is not None
@property @property
def connected(self): def connected(self):
"""Return if connected to device.""" """Return if connected to device."""
@@ -184,7 +189,8 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
self.status_updated(status) 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: for entity in self._entities:
await entity.restore_state_when_connected() 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 # Default value is available to be provided by Platform entities if required
self._default_value = self._config.get(CONF_DEFAULT_VALUE) 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._restore_on_reconnect = (
self._config.get(CONF_RESTORE_ON_RECONNECT) or False self._config.get(CONF_RESTORE_ON_RECONNECT) or False
) )
@@ -365,8 +372,10 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return entity specific state attributes to be saved & then available for restore """Return entity specific state attributes to be saved.
when the entity is restored at startup.
These attributes are then available for restore when the
entity is restored at startup.
""" """
attributes = {} attributes = {}
if self._state is not None: 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, # Keep record in last_state as long as not during connection/re-connection,
# as last state will be used to restore the previous state # 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 self._last_state = state
def status_restored(self, stored_state): def status_restored(self, stored_state):
@@ -464,8 +473,6 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
""" """
raw_state = stored_state.attributes.get(ATTR_STATE) raw_state = stored_state.attributes.get(ATTR_STATE)
if raw_state is not None: if raw_state is not None:
# (stored_state.state == "unavailable") | (stored_state.state == "unknown")
# ):
self._last_state = raw_state self._last_state = raw_state
self.debug( self.debug(
"Restoring state for entity: %s - state: %s", "Restoring state for entity: %s - state: %s",
@@ -474,7 +481,7 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
) )
def default_value(self): 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. Override in subclasses to specify the default value for the entity.
""" """
@@ -484,8 +491,8 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
return self._default_value return self._default_value
def entity_default_value(self): def entity_default_value(self): # pylint: disable=no-self-use
"""Default value of the entity type """Return default value of the entity type.
Override in subclasses to specify the default value for the entity. Override in subclasses to specify the default value for the entity.
""" """
@@ -493,14 +500,22 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
@property @property
def restore_on_reconnect(self): 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 return self._restore_on_reconnect
async def restore_state_when_connected(self): 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): if not self.restore_on_reconnect and (str(self._dp_id) in self._status):
self.debug( 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.name,
self._dp_id, self._dp_id,
) )

View File

@@ -189,8 +189,9 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
self.debug("Restored cover position %s", self._current_cover_position) self.debug("Restored cover position %s", self._current_cover_position)
def status_updated(self): def status_updated(self):
super.status_updated(self)
"""Device status was updated.""" """Device status was updated."""
super.status_updated(self)
self._previous_state = self._state self._previous_state = self._state
self._state = self.dps(self._dp_id) self._state = self.dps(self._dp_id)
if self._state.isupper(): if self._state.isupper():

View File

@@ -8,10 +8,7 @@ from homeassistant.const import CONF_DEVICE_CLASS, STATE_UNKNOWN
from .common import LocalTuyaEntity, async_setup_entry from .common import LocalTuyaEntity, async_setup_entry
_LOGGER = logging.getLogger(__name__)
from .const import ( from .const import (
CONF_DEFAULT_VALUE,
CONF_MIN_VALUE, CONF_MIN_VALUE,
CONF_MAX_VALUE, CONF_MAX_VALUE,
CONF_DEFAULT_VALUE, CONF_DEFAULT_VALUE,
@@ -19,6 +16,8 @@ from .const import (
CONF_STEPSIZE_VALUE, CONF_STEPSIZE_VALUE,
) )
_LOGGER = logging.getLogger(__name__)
DEFAULT_MIN = 0 DEFAULT_MIN = 0
DEFAULT_MAX = 100000 DEFAULT_MAX = 100000
DEFAULT_STEP = 1.0 DEFAULT_STEP = 1.0
@@ -66,17 +65,15 @@ class LocaltuyaNumber(LocalTuyaEntity, NumberEntity):
if CONF_MAX_VALUE in self._config: if CONF_MAX_VALUE in self._config:
self._max_value = self._config.get(CONF_MAX_VALUE) self._max_value = self._config.get(CONF_MAX_VALUE)
self._step_size = DEFAULT_STEP self._step_size = DEFAULT_STEP
if CONF_STEPSIZE_VALUE in self._config: if CONF_STEPSIZE_VALUE in self._config:
self._step_size = self._config.get(CONF_STEPSIZE_VALUE) 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) default_value = self._config.get(CONF_DEFAULT_VALUE)
if default_value is not None: if default_value is not None:
self._default_value = float(default_value) self._default_value = float(default_value)
@property @property
def native_value(self) -> float: def native_value(self) -> float:
"""Return sensor state.""" """Return sensor state."""
@@ -108,6 +105,7 @@ class LocaltuyaNumber(LocalTuyaEntity, NumberEntity):
# Default value is the minimum value # Default value is the minimum value
def entity_default_value(self): def entity_default_value(self):
"""Return the minimum value as the default for this entity type."""
return self._min_value return self._min_value

View File

@@ -11,8 +11,6 @@ from homeassistant.const import (
from .common import LocalTuyaEntity, async_setup_entry from .common import LocalTuyaEntity, async_setup_entry
_LOGGER = logging.getLogger(__name__)
from .const import ( from .const import (
CONF_OPTIONS, CONF_OPTIONS,
CONF_OPTIONS_FRIENDLY, CONF_OPTIONS_FRIENDLY,
@@ -31,6 +29,9 @@ def flow_schema(dps):
} }
_LOGGER = logging.getLogger(__name__)
class LocaltuyaSelect(LocalTuyaEntity, SelectEntity): class LocaltuyaSelect(LocalTuyaEntity, SelectEntity):
"""Representation of a Tuya Enumeration.""" """Representation of a Tuya Enumeration."""
@@ -100,8 +101,9 @@ class LocaltuyaSelect(LocalTuyaEntity, SelectEntity):
await self._device.set_dp(option_value, self._dp_id) await self._device.set_dp(option_value, self._dp_id)
def status_updated(self): def status_updated(self):
super().status_updated()
"""Device status was updated.""" """Device status was updated."""
super().status_updated()
state = self.dps(self._dp_id) state = self.dps(self._dp_id)
# Check that received status update for this entity. # Check that received status update for this entity.
@@ -116,6 +118,7 @@ class LocaltuyaSelect(LocalTuyaEntity, SelectEntity):
# Default value is the first option # Default value is the first option
def entity_default_value(self): def entity_default_value(self):
"""Return the first option as the default value for this entity type."""
return self._valid_options[0] return self._valid_options[0]

View File

@@ -68,7 +68,7 @@ class LocaltuyaSensor(LocalTuyaEntity):
# No need to restore state for a sensor # No need to restore state for a sensor
async def restore_state_when_connected(self): async def restore_state_when_connected(self):
"""Do nothing for a sensor""" """Do nothing for a sensor."""
return return

View File

@@ -82,6 +82,7 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
# Default value is the "OFF" state # Default value is the "OFF" state
def entity_default_value(self): def entity_default_value(self):
"""Return False as the defaualt value for this entity type."""
return False return False