From 8f291cd9088e3a37532c6483a478f70f3b287124 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 13 Jan 2024 13:45:27 +0800 Subject: [PATCH] fix: resolve defect #316 --- .../entity_controller/__init__.py | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/custom_components/entity_controller/__init__.py b/custom_components/entity_controller/__init__.py index 257938f..e2c6c45 100644 --- a/custom_components/entity_controller/__init__.py +++ b/custom_components/entity_controller/__init__.py @@ -18,7 +18,7 @@ """ Entity controller component for Home Assistant. Maintainer: Daniel Mason -Version: v9.6.1 +Version: v9.6.0 Project Page: https://danielbkr.net/projects/entity-controller/ Documentation: https://github.com/danobot/entity-controller """ @@ -39,6 +39,7 @@ from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.sun import get_astral_event_date from homeassistant.util import dt +import homeassistant.util.yaml.objects as YamlObjects import homeassistant.util.uuid as uuid_util from transitions import Machine from transitions.extensions import HierarchicalMachine as Machine @@ -106,7 +107,7 @@ -VERSION = '9.6.1' +VERSION = '9.6.0' _LOGGER = logging.getLogger(__name__) @@ -399,6 +400,7 @@ def __init__(self, hass, config, machine): self.attributes = {} self.may_update = False self.model = None + self.context_id = None self.friendly_name = config.get(CONF_NAME, "Motion Light") if "friendly_name" in config: self.friendly_name = config.get("friendly_name") @@ -1578,9 +1580,12 @@ def set_context(self, parent: Optional[Context] = None) -> None: # Unique name per EC instance, but short enough to fit within id length name_hash = hashlib.sha1(self.name.encode("UTF-8")).hexdigest()[:6] unique_id = uuid_util.random_uuid_hex() + # ec_324a1b_7b6ca2a12ce2ff21ae4c06ab55 context_id = f"{DOMAIN_SHORT}_{name_hash}_{unique_id}" # Restrict id length to database field size context_id = context_id[:CONTEXT_ID_CHARACTER_LIMIT] + self.context_id = context_id + assert len(context_id) == CONTEXT_ID_CHARACTER_LIMIT # parent_id only exists for a non-None parent parent_id = parent.id if parent else None self.context = Context(parent_id=parent_id, id=context_id) @@ -1633,21 +1638,32 @@ def add(self, list, config, key=None): self.add(self.controlEntities, config, CONF_CONTROL_ENTITIES) """ - if config is not None: - v = [] - if key is not None: - if key in config: # must be in separate if statement - v = config[key] - else: - v = config - if type(v) == str: + self.log.debug("add :: Adding config key `%s` to the config list", key) + if config is None: + self.log.debug("Tried to configure %s but supplied config was None" % (key)) + return False - list.append(v) - else: - list.extend(v) + v = None + if key is not None: + if key in config: # must be in separate if statement + v = config[key] else: - self.log.debug("Tried to configure %s but supplied config was None" % (key)) - return len(v) > 0 + v = config + + if type(v) is YamlObjects.NodeStrClass: + self.log.debug("Found string value %s for key %s, now adding to exiting list %s. (Type: %s)", v, key, list, type(v)) + list.append(v) + return len(v) > 0 + elif type(v) is YamlObjects.NodeListClass: + self.log.debug("Found list value %s for key %s, now adding to exiting list %s. (Type: %s)", v, key, list, type(v)) + list.extend(v) + return len(v) > 0 + elif v == None: + self.log.debug(f'Config key {key} not provided by user. Skipping.') + return False + else: + self.log.error(f'Cannot determine type of provided config value. Key: {key}, Type: {type(v)}, Value: {str(v)}') + return False def futurize(self, timet): """ Returns tomorrows time if time is in the past.