Changes to make Passive DPS entites a configurable item

This commit is contained in:
sibowler
2022-09-10 06:40:53 +10:00
parent 1db48e5012
commit f7ce9be5f6
6 changed files with 28 additions and 9 deletions

View File

@@ -36,6 +36,7 @@ from .const import (
ATTR_STATE,
CONF_RESTORE_ON_RECONNECT,
CONF_RESET_DPIDS,
CONF_PASSIVE_ENTITY,
)
_LOGGER = logging.getLogger(__name__)
@@ -375,6 +376,9 @@ 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)
# Determine whether is a passive entity
self._is_passive_entity = self._config.get(CONF_PASSIVE_ENTITY) or False
""" Restore on connect setting is available to be provided by Platform entities
if required"""
self._restore_on_reconnect = (
@@ -555,10 +559,13 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
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) or (not self._is_passive_entity)
):
self.debug(
"Entity %s (DP %d) - Not restoring as restore on reconnect is "
+ "disabled for this entity and the entity has an initial status",
+ "disabled for this entity and the entity has an initial status "
+ "or it is not a passive entity",
self.name,
self._dp_id,
)
@@ -575,8 +582,12 @@ class LocalTuyaEntity(RestoreEntity, pytuya.ContextualLogger):
# If no current or saved state, then use the default value
if restore_state is None:
if self._is_passive_entity:
self.debug("No last restored state - using default")
restore_state = self.default_value()
else:
self.debug("Not a passive entity and no state found - aborting restore")
return
self.debug(
"Entity %s (DP %d) - Restoring state: %s",

View File

@@ -44,6 +44,7 @@ CONF_NO_CLOUD = "no_cloud"
CONF_MANUAL_DPS = "manual_dps_strings"
CONF_DEFAULT_VALUE = "dps_default_value"
CONF_RESET_DPIDS = "reset_dpids"
CONF_PASSIVE_ENTITY = "is_passive_entity"
# light
CONF_BRIGHTNESS_LOWER = "brightness_lower"

View File

@@ -14,6 +14,7 @@ from .const import (
CONF_DEFAULT_VALUE,
CONF_RESTORE_ON_RECONNECT,
CONF_STEPSIZE_VALUE,
CONF_PASSIVE_ENTITY,
)
_LOGGER = logging.getLogger(__name__)
@@ -38,8 +39,9 @@ def flow_schema(dps):
vol.Coerce(float),
vol.Range(min=0.0, max=1000000.0),
),
vol.Optional(CONF_DEFAULT_VALUE): str,
vol.Required(CONF_RESTORE_ON_RECONNECT): bool,
vol.Required(CONF_PASSIVE_ENTITY): bool,
vol.Optional(CONF_DEFAULT_VALUE): str,
}

View File

@@ -16,6 +16,7 @@ from .const import (
CONF_OPTIONS_FRIENDLY,
CONF_DEFAULT_VALUE,
CONF_RESTORE_ON_RECONNECT,
CONF_PASSIVE_ENTITY,
)
@@ -24,8 +25,9 @@ def flow_schema(dps):
return {
vol.Required(CONF_OPTIONS): str,
vol.Optional(CONF_OPTIONS_FRIENDLY): str,
vol.Optional(CONF_DEFAULT_VALUE): str,
vol.Required(CONF_RESTORE_ON_RECONNECT): bool,
vol.Required(CONF_PASSIVE_ENTITY): bool,
vol.Optional(CONF_DEFAULT_VALUE): str,
}

View File

@@ -16,6 +16,7 @@ from .const import (
CONF_VOLTAGE,
CONF_DEFAULT_VALUE,
CONF_RESTORE_ON_RECONNECT,
CONF_PASSIVE_ENTITY,
)
_LOGGER = logging.getLogger(__name__)
@@ -27,8 +28,9 @@ def flow_schema(dps):
vol.Optional(CONF_CURRENT): vol.In(dps),
vol.Optional(CONF_CURRENT_CONSUMPTION): vol.In(dps),
vol.Optional(CONF_VOLTAGE): vol.In(dps),
vol.Optional(CONF_DEFAULT_VALUE): str,
vol.Required(CONF_RESTORE_ON_RECONNECT): bool,
vol.Required(CONF_PASSIVE_ENTITY): bool,
vol.Optional(CONF_DEFAULT_VALUE): str,
}
@@ -82,7 +84,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 as the default value for this entity type."""
return False

View File

@@ -188,7 +188,8 @@
"restore_on_reconnect": "Restore the last set value in HomeAssistant after a lost connection",
"min_value": "Minimum Value",
"max_value": "Maximum Value",
"step_size": "Minimum increment between numbers"
"step_size": "Minimum increment between numbers",
"is_passive_entity": "Passive entity - requires integration to send initialisation value"
}
}
}