Skip to content

Commit b532262

Browse files
authored
fix: resolve defect #316 (#317)
* fix: resolve defect #316
1 parent 7c0058f commit b532262

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

custom_components/entity_controller/__init__.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from homeassistant.helpers.entity_component import EntityComponent
4040
from homeassistant.helpers.sun import get_astral_event_date
4141
from homeassistant.util import dt
42+
import homeassistant.util.yaml.objects as YamlObjects
4243
import homeassistant.util.uuid as uuid_util
4344
from transitions import Machine
4445
from transitions.extensions import HierarchicalMachine as Machine
@@ -399,6 +400,7 @@ def __init__(self, hass, config, machine):
399400
self.attributes = {}
400401
self.may_update = False
401402
self.model = None
403+
self.context_id = None
402404
self.friendly_name = config.get(CONF_NAME, "Motion Light")
403405
if "friendly_name" in config:
404406
self.friendly_name = config.get("friendly_name")
@@ -1577,10 +1579,13 @@ def set_context(self, parent: Optional[Context] = None) -> None:
15771579
"""
15781580
# Unique name per EC instance, but short enough to fit within id length
15791581
name_hash = hashlib.sha1(self.name.encode("UTF-8")).hexdigest()[:6]
1582+
self.log.debug("set_context :: name_hash: %s", name_hash)
15801583
unique_id = uuid_util.random_uuid_hex()
1581-
context_id = f"{DOMAIN_SHORT}_{name_hash}_{unique_id}"
1584+
15821585
# Restrict id length to database field size
1583-
context_id = context_id[:CONTEXT_ID_CHARACTER_LIMIT]
1586+
context_id = f"{DOMAIN_SHORT}_{name_hash}_{unique_id}"[:CONTEXT_ID_CHARACTER_LIMIT]
1587+
self.log.debug("set_context :: context_id: %s", context_id)
1588+
self.context_id = context_id
15841589
# parent_id only exists for a non-None parent
15851590
parent_id = parent.id if parent else None
15861591
self.context = Context(parent_id=parent_id, id=context_id)
@@ -1633,21 +1638,32 @@ def add(self, list, config, key=None):
16331638
self.add(self.controlEntities, config, CONF_CONTROL_ENTITIES)
16341639
16351640
"""
1636-
if config is not None:
1637-
v = []
1638-
if key is not None:
1639-
if key in config: # must be in separate if statement
1640-
v = config[key]
1641-
else:
1642-
v = config
1643-
if type(v) == str:
1641+
self.log.debug("add :: Adding config key `%s` to the config list", key)
1642+
if config is None:
1643+
self.log.debug("Tried to configure %s but supplied config was None" % (key))
1644+
return False
16441645

1645-
list.append(v)
1646-
else:
1647-
list.extend(v)
1646+
v = None
1647+
if key is not None:
1648+
if key in config: # must be in separate if statement
1649+
v = config[key]
16481650
else:
1649-
self.log.debug("Tried to configure %s but supplied config was None" % (key))
1650-
return len(v) > 0
1651+
v = config
1652+
1653+
if type(v) is YamlObjects.NodeStrClass:
1654+
self.log.debug("Found string value %s for key %s, now adding to exiting list %s. (Type: %s)", v, key, list, type(v))
1655+
list.append(v)
1656+
return len(v) > 0
1657+
elif type(v) is YamlObjects.NodeListClass:
1658+
self.log.debug("Found list value %s for key %s, now adding to exiting list %s. (Type: %s)", v, key, list, type(v))
1659+
list.extend(v)
1660+
return len(v) > 0
1661+
elif v == None:
1662+
self.log.debug(f'Config key {key} not provided by user. Skipping.')
1663+
return False
1664+
else:
1665+
self.log.error(f'Cannot determine type of provided config value. Key: {key}, Type: {type(v)}, Value: {str(v)}')
1666+
return False
16511667

16521668
def futurize(self, timet):
16531669
""" Returns tomorrows time if time is in the past.

0 commit comments

Comments
 (0)