Skip to content

Commit

Permalink
Merge pull request #23 from xZetsubou/fix_manual_dps_and_reg_remove
Browse files Browse the repository at this point in the history
Confi_Flow: Delete only unchecked entitles in edit device `won't reset entities settings`
  • Loading branch information
xZetsubou authored Sep 1, 2023
2 parents e8ad6d4 + b31f4a2 commit acbba2e
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import homeassistant.helpers.config_validation as cv

# import homeassistant.helpers.entity_registry as er # Disabled it because no need to delete registry.
import homeassistant.helpers.entity_registry as er
import voluptuous as vol
from homeassistant import config_entries, core, exceptions
from homeassistant.helpers.selector import (
Expand Down Expand Up @@ -989,16 +989,21 @@ async def async_step_configure_entity(self, user_input=None):
# finished editing device. Let's store the new config entry....
dev_id = self.device_data[CONF_DEVICE_ID]
new_data = self.config_entry.data.copy()
# entry_id = self.config_entry.entry_id
# removing entities from registry (they will be recreated)
# 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)
# if dev_id in ent.unique_id
# }
# for entity_id in reg_entities.values():
# ent_reg.async_remove(entity_id)
entry_id = self.config_entry.entry_id
# Removing the unwanted entites.
entitesNames = [
name[CONF_FRIENDLY_NAME]
for name in self.device_data[CONF_ENTITIES]
]
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)
if dev_id in ent.unique_id
and ent.original_name not in entitesNames
}
for entity_id in reg_entities.values():
ent_reg.async_remove(entity_id)

new_data[CONF_DEVICES][dev_id] = self.device_data
new_data[ATTR_UPDATED_AT] = str(int(time.time() * 1000))
Expand Down

0 comments on commit acbba2e

Please sign in to comment.