From 904f4feb6c0248b317750bc587cdc082362185f9 Mon Sep 17 00:00:00 2001 From: GaoJing <270615179@qq.com> Date: Thu, 16 Mar 2023 15:50:31 +0800 Subject: [PATCH] Add entities during 'Edit a device' --- custom_components/localtuya/config_flow.py | 75 ++++++++++--------- custom_components/localtuya/const.py | 1 + .../localtuya/translations/en.json | 1 + 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index d4272d4..4bdf5c6 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -43,6 +43,7 @@ from .const import ( CONF_RESET_DPIDS, CONF_SETUP_CLOUD, CONF_USER_ID, + CONF_ENABLE_ADD_ENTITIES, DATA_CLOUD, DATA_DISCOVERY, DOMAIN, @@ -74,7 +75,7 @@ CONFIGURE_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_SECRET): cv.string, vol.Optional(CONF_USER_ID): cv.string, @@ -146,6 +147,7 @@ def options_schema(entities): vol.Required( CONF_ENTITIES, description={"suggested_value": 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 ) if self.editing_device: - self.device_data.update( - { - CONF_DEVICE_ID: dev_id, - CONF_DPS_STRINGS: self.dps_strings, - CONF_ENTITIES: [], - } - ) - if len(user_input[CONF_ENTITIES]) == 0: - return self.async_abort( - reason="no_entities", - description_placeholders={}, + if user_input[CONF_ENABLE_ADD_ENTITIES]: + self.editing_device = False + user_input[CONF_DEVICE_ID] = dev_id + self.device_data.update( + { + CONF_DEVICE_ID: dev_id, + CONF_DPS_STRINGS: self.dps_strings, + } ) - 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() + return await self.async_step_pick_entity_type() + else: + self.device_data.update( + { + CONF_DEVICE_ID: dev_id, + CONF_DPS_STRINGS: self.dps_strings, + CONF_ENTITIES: [], + } + ) + if len(user_input[CONF_ENTITIES]) == 0: + return self.async_abort( + 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) 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) note = "\nNOTE: a new local_key has been retrieved using cloud API" placeholders = {"for_device": f" for device `{dev_id}`.{note}"} + defaults[CONF_ENABLE_ADD_ENTITIES] = False schema = schema_defaults(options_schema(self.entities), **defaults) else: defaults[CONF_PROTOCOL_VERSION] = "3.3" @@ -647,17 +661,6 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow): } 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[ATTR_UPDATED_AT] = str(int(time.time() * 1000)) @@ -740,7 +743,7 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow): new_data = self.config_entry.data.copy() entry_id = self.config_entry.entry_id # 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 = { ent.unique_id: ent.entity_id for ent in er.async_entries_for_config_entry(ent_reg, entry_id) diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index 3a6c252..630d630 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -35,6 +35,7 @@ CONF_MODEL = "model" CONF_PRODUCT_KEY = "product_key" CONF_PRODUCT_NAME = "product_name" CONF_USER_ID = "user_id" +CONF_ENABLE_ADD_ENTITIES = "add_entities" CONF_ACTION = "action" diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 947141c..b9beee4 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -99,6 +99,7 @@ "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)", "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)", "reset_dpids": "DPIDs to send in RESET command (separated by commas ',')- Used when device does not respond to status requests after turning on (optional)" }