Add entities during 'Edit a device'
This commit is contained in:
@@ -43,6 +43,7 @@ from .const import (
|
|||||||
CONF_RESET_DPIDS,
|
CONF_RESET_DPIDS,
|
||||||
CONF_SETUP_CLOUD,
|
CONF_SETUP_CLOUD,
|
||||||
CONF_USER_ID,
|
CONF_USER_ID,
|
||||||
|
CONF_ENABLE_ADD_ENTITIES,
|
||||||
DATA_CLOUD,
|
DATA_CLOUD,
|
||||||
DATA_DISCOVERY,
|
DATA_DISCOVERY,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@@ -74,7 +75,7 @@ CONFIGURE_SCHEMA = vol.Schema(
|
|||||||
|
|
||||||
CLOUD_SETUP_SCHEMA = vol.Schema(
|
CLOUD_SETUP_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_REGION, default="eu"): vol.In(["eu", "us", "cn", "in"]),
|
vol.Required(CONF_REGION, default="cn"): vol.In(["eu", "us", "cn", "in"]),
|
||||||
vol.Optional(CONF_CLIENT_ID): cv.string,
|
vol.Optional(CONF_CLIENT_ID): cv.string,
|
||||||
vol.Optional(CONF_CLIENT_SECRET): cv.string,
|
vol.Optional(CONF_CLIENT_SECRET): cv.string,
|
||||||
vol.Optional(CONF_USER_ID): cv.string,
|
vol.Optional(CONF_USER_ID): cv.string,
|
||||||
@@ -146,6 +147,7 @@ def options_schema(entities):
|
|||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_ENTITIES, description={"suggested_value": entity_names}
|
CONF_ENTITIES, description={"suggested_value": entity_names}
|
||||||
): cv.multi_select(entity_names),
|
): cv.multi_select(entity_names),
|
||||||
|
vol.Required(CONF_ENABLE_ADD_ENTITIES, default=False): bool,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -554,30 +556,41 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
CONF_PRODUCT_NAME
|
CONF_PRODUCT_NAME
|
||||||
)
|
)
|
||||||
if self.editing_device:
|
if self.editing_device:
|
||||||
self.device_data.update(
|
if user_input[CONF_ENABLE_ADD_ENTITIES]:
|
||||||
{
|
self.editing_device = False
|
||||||
CONF_DEVICE_ID: dev_id,
|
user_input[CONF_DEVICE_ID] = dev_id
|
||||||
CONF_DPS_STRINGS: self.dps_strings,
|
self.device_data.update(
|
||||||
CONF_ENTITIES: [],
|
{
|
||||||
}
|
CONF_DEVICE_ID: dev_id,
|
||||||
)
|
CONF_DPS_STRINGS: self.dps_strings,
|
||||||
if len(user_input[CONF_ENTITIES]) == 0:
|
}
|
||||||
return self.async_abort(
|
|
||||||
reason="no_entities",
|
|
||||||
description_placeholders={},
|
|
||||||
)
|
)
|
||||||
if user_input[CONF_ENTITIES]:
|
return await self.async_step_pick_entity_type()
|
||||||
entity_ids = [
|
else:
|
||||||
int(entity.split(":")[0])
|
self.device_data.update(
|
||||||
for entity in user_input[CONF_ENTITIES]
|
{
|
||||||
]
|
CONF_DEVICE_ID: dev_id,
|
||||||
device_config = self.config_entry.data[CONF_DEVICES][dev_id]
|
CONF_DPS_STRINGS: self.dps_strings,
|
||||||
self.entities = [
|
CONF_ENTITIES: [],
|
||||||
entity
|
}
|
||||||
for entity in device_config[CONF_ENTITIES]
|
)
|
||||||
if entity[CONF_ID] in entity_ids
|
if len(user_input[CONF_ENTITIES]) == 0:
|
||||||
]
|
return self.async_abort(
|
||||||
return await self.async_step_configure_entity()
|
reason="no_entities",
|
||||||
|
description_placeholders={},
|
||||||
|
)
|
||||||
|
if user_input[CONF_ENTITIES]:
|
||||||
|
entity_ids = [
|
||||||
|
int(entity.split(":")[0])
|
||||||
|
for entity in user_input[CONF_ENTITIES]
|
||||||
|
]
|
||||||
|
device_config = self.config_entry.data[CONF_DEVICES][dev_id]
|
||||||
|
self.entities = [
|
||||||
|
entity
|
||||||
|
for entity in device_config[CONF_ENTITIES]
|
||||||
|
if entity[CONF_ID] in entity_ids
|
||||||
|
]
|
||||||
|
return await self.async_step_configure_entity()
|
||||||
|
|
||||||
self.dps_strings = await validate_input(self.hass, user_input)
|
self.dps_strings = await validate_input(self.hass, user_input)
|
||||||
return await self.async_step_pick_entity_type()
|
return await self.async_step_pick_entity_type()
|
||||||
@@ -608,6 +621,7 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
defaults[CONF_LOCAL_KEY] = cloud_devs[dev_id].get(CONF_LOCAL_KEY)
|
defaults[CONF_LOCAL_KEY] = cloud_devs[dev_id].get(CONF_LOCAL_KEY)
|
||||||
note = "\nNOTE: a new local_key has been retrieved using cloud API"
|
note = "\nNOTE: a new local_key has been retrieved using cloud API"
|
||||||
placeholders = {"for_device": f" for device `{dev_id}`.{note}"}
|
placeholders = {"for_device": f" for device `{dev_id}`.{note}"}
|
||||||
|
defaults[CONF_ENABLE_ADD_ENTITIES] = False
|
||||||
schema = schema_defaults(options_schema(self.entities), **defaults)
|
schema = schema_defaults(options_schema(self.entities), **defaults)
|
||||||
else:
|
else:
|
||||||
defaults[CONF_PROTOCOL_VERSION] = "3.3"
|
defaults[CONF_PROTOCOL_VERSION] = "3.3"
|
||||||
@@ -647,17 +661,6 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
}
|
}
|
||||||
|
|
||||||
dev_id = self.device_data.get(CONF_DEVICE_ID)
|
dev_id = self.device_data.get(CONF_DEVICE_ID)
|
||||||
if dev_id in self.config_entry.data[CONF_DEVICES]:
|
|
||||||
self.hass.config_entries.async_update_entry(
|
|
||||||
self.config_entry, data=config
|
|
||||||
)
|
|
||||||
return self.async_abort(
|
|
||||||
reason="device_success",
|
|
||||||
description_placeholders={
|
|
||||||
"dev_name": config.get(CONF_FRIENDLY_NAME),
|
|
||||||
"action": "updated",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
new_data = self.config_entry.data.copy()
|
new_data = self.config_entry.data.copy()
|
||||||
new_data[ATTR_UPDATED_AT] = str(int(time.time() * 1000))
|
new_data[ATTR_UPDATED_AT] = str(int(time.time() * 1000))
|
||||||
@@ -740,7 +743,7 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
new_data = self.config_entry.data.copy()
|
new_data = self.config_entry.data.copy()
|
||||||
entry_id = self.config_entry.entry_id
|
entry_id = self.config_entry.entry_id
|
||||||
# removing entities from registry (they will be recreated)
|
# removing entities from registry (they will be recreated)
|
||||||
ent_reg = await er.async_get_registry(self.hass)
|
ent_reg = er.async_get(self.hass)
|
||||||
reg_entities = {
|
reg_entities = {
|
||||||
ent.unique_id: ent.entity_id
|
ent.unique_id: ent.entity_id
|
||||||
for ent in er.async_entries_for_config_entry(ent_reg, entry_id)
|
for ent in er.async_entries_for_config_entry(ent_reg, entry_id)
|
||||||
|
@@ -35,6 +35,7 @@ CONF_MODEL = "model"
|
|||||||
CONF_PRODUCT_KEY = "product_key"
|
CONF_PRODUCT_KEY = "product_key"
|
||||||
CONF_PRODUCT_NAME = "product_name"
|
CONF_PRODUCT_NAME = "product_name"
|
||||||
CONF_USER_ID = "user_id"
|
CONF_USER_ID = "user_id"
|
||||||
|
CONF_ENABLE_ADD_ENTITIES = "add_entities"
|
||||||
|
|
||||||
|
|
||||||
CONF_ACTION = "action"
|
CONF_ACTION = "action"
|
||||||
|
@@ -99,6 +99,7 @@
|
|||||||
"enable_debug": "Enable debugging for this device (debug must be enabled also in configuration.yaml)",
|
"enable_debug": "Enable debugging for this device (debug must be enabled also in configuration.yaml)",
|
||||||
"scan_interval": "Scan interval (seconds, only when not updating automatically)",
|
"scan_interval": "Scan interval (seconds, only when not updating automatically)",
|
||||||
"entities": "Entities (uncheck an entity to remove it)",
|
"entities": "Entities (uncheck an entity to remove it)",
|
||||||
|
"add_entities": "Add more entities in 'edit device' mode",
|
||||||
"manual_dps_strings": "Manual DPS to add (separated by commas ',') - used when detection is not working (optional)",
|
"manual_dps_strings": "Manual DPS to add (separated by commas ',') - used when detection is not working (optional)",
|
||||||
"reset_dpids": "DPIDs to send in RESET command (separated by commas ',')- Used when device does not respond to status requests after turning on (optional)"
|
"reset_dpids": "DPIDs to send in RESET command (separated by commas ',')- Used when device does not respond to status requests after turning on (optional)"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user