add lookup table for entities

This commit is contained in:
Johannes
2022-08-08 18:17:24 +02:00
parent bf7b315437
commit ddbce578e4
3 changed files with 39 additions and 5 deletions

View File

@@ -1,5 +1,8 @@
import uuid
class Entity(object): class Entity(object):
def __init__(self, entity_input_config): def __init__(self, entity_input_config):
self.uuid = f"uuid.{uuid.uuid4().hex}"
if type(entity_input_config) is not dict: if type(entity_input_config) is not dict:
#self._ha_api.log("Config error, not a dict check your entity configs") #self._ha_api.log("Config error, not a dict check your entity configs")
self.entityId = "error" self.entityId = "error"
@@ -30,7 +33,7 @@ class Card(object):
self.id = f"{self.cardType}_{self.key}".replace(".","_").replace("~","_").replace(" ","_") self.id = f"{self.cardType}_{self.key}".replace(".","_").replace("~","_").replace(" ","_")
#self._ha_api.log(f"Created Card {self.cardType} with pos {pos} and id {self.id}") #self._ha_api.log(f"Created Card {self.cardType} with pos {pos} and id {self.id}")
def get_entity_list(self): def get_entity_names(self):
entityIds = [] entityIds = []
if self.entity is not None: if self.entity is not None:
entityIds.append(self.entity.entityId) entityIds.append(self.entity.entityId)
@@ -50,6 +53,16 @@ class Card(object):
entityIds.append(val.get("entity")) entityIds.append(val.get("entity"))
return entityIds return entityIds
def get_entity_list(self):
entitys = []
if self.entity is not None:
entitys.append(self.entity)
else:
for e in self.entities:
entitys.append(e)
return entitys
class LuiBackendConfig(object): class LuiBackendConfig(object):
def dict_recursive_update(self, source: dict, target: dict) -> dict: def dict_recursive_update(self, source: dict, target: dict) -> dict:
@@ -138,10 +151,13 @@ class LuiBackendConfig(object):
# parse screensaver # parse screensaver
self._config_screensaver = Card(self.get("screensaver")) self._config_screensaver = Card(self.get("screensaver"))
# parsed hidden pages that can be accessed through navigate # parse hidden pages that can be accessed through navigate
for card in self.get("hiddenCards"): for card in self.get("hiddenCards"):
self._config_hidden_cards.append(Card(card)) self._config_hidden_cards.append(Card(card))
# all entites sorted by generated key, to be able to use short identifiers
self._config_entites_table = {x.uuid: x for x in self.get_all_entitys()}
def get(self, name): def get(self, name):
path = name.split(".") path = name.split(".")
value = self._config value = self._config
@@ -158,12 +174,20 @@ class LuiBackendConfig(object):
return value return value
def get_all_entity_names(self): def get_all_entity_names(self):
entities = []
for card in self._config_cards:
entities.extend(card.get_entity_names())
for card in self._config_hidden_cards:
entities.extend(card.get_entity_names())
entities.extend(self._config_screensaver.get_entity_names())
return entities
def get_all_entitys(self):
entities = [] entities = []
for card in self._config_cards: for card in self._config_cards:
entities.extend(card.get_entity_list()) entities.extend(card.get_entity_list())
for card in self._config_hidden_cards: for card in self._config_hidden_cards:
entities.extend(card.get_entity_list()) entities.extend(card.get_entity_list())
entities.extend(self._config_screensaver.get_entity_list())
return entities return entities
def getCard(self, pos): def getCard(self, pos):

View File

@@ -157,8 +157,8 @@ class LuiController(object):
def state_change_callback(self, entity, attribute, old, new, kwargs): def state_change_callback(self, entity, attribute, old, new, kwargs):
self._ha_api.log(f"Got callback for: {entity}", level="DEBUG") self._ha_api.log(f"Got callback for: {entity}", level="DEBUG")
self._ha_api.log(f"Current page has the following items: {self._current_card.get_entity_list()}", level="DEBUG") self._ha_api.log(f"Current page has the following items: {self._current_card.get_entity_names()}", level="DEBUG")
if entity in self._current_card.get_entity_list(): if entity in self._current_card.get_entity_names():
self._ha_api.log(f"Callback Entity is on current page: {entity}", level="DEBUG") self._ha_api.log(f"Callback Entity is on current page: {entity}", level="DEBUG")
self._pages_gen.render_card(self._current_card, send_page_type=False) self._pages_gen.render_card(self._current_card, send_page_type=False)
# send detail page update, just in case # send detail page update, just in case
@@ -266,6 +266,10 @@ class LuiController(object):
if button_type == "button": if button_type == "button":
if entity_id.startswith('uuid'):
le = self._config._config_entites_table.get(entity_id)
entity_id = le.entityId
if entity_id.startswith('navigate'): if entity_id.startswith('navigate'):
# internal for navigation to nested pages # internal for navigation to nested pages
dstCard = self._config.searchCard(entity_id) dstCard = self._config.searchCard(entity_id)
@@ -291,6 +295,7 @@ class LuiController(object):
elif entity_id.startswith('input_select'): elif entity_id.startswith('input_select'):
self._ha_api.get_entity(entity_id).call_service("select_next") self._ha_api.get_entity(entity_id).call_service("select_next")
# for media page # for media page
if button_type == "media-next": if button_type == "media-next":
self._ha_api.get_entity(entity_id).call_service("media_next_track") self._ha_api.get_entity(entity_id).call_service("media_next_track")

View File

@@ -162,6 +162,7 @@ class LuiPagesGen(object):
icon = item.iconOverride icon = item.iconOverride
colorOverride = item.colorOverride colorOverride = item.colorOverride
name = item.nameOverride name = item.nameOverride
uuid = item.uuid
# type of the item is the string before the "." in the entityId # type of the item is the string before the "." in the entityId
entityType = entityId.split(".")[0] entityType = entityId.split(".")[0]
@@ -194,6 +195,10 @@ class LuiPagesGen(object):
name = name if name is not None else "conf name missing" name = name if name is not None else "conf name missing"
icon_res = get_icon_id(icon) if icon is not None else get_icon_id("alert-circle-outline") icon_res = get_icon_id(icon) if icon is not None else get_icon_id("alert-circle-outline")
return f"~text~{entityId}~{icon_res}~17299~{name}~{value}" return f"~text~{entityId}~{icon_res}~17299~{name}~{value}"
if entityType == "service":
icon_id = get_icon_id_ha("script", overwrite=icon)
text = get_translation(self._locale, "frontend.ui.card.script.run")
return f"~button~{uuid}~{icon_id}~17299~{name}~{text}"
if not self._ha_api.entity_exists(entityId): if not self._ha_api.entity_exists(entityId):
return f"~text~{entityId}~{get_icon_id('alert-circle-outline')}~17299~Not found check~ apps.yaml" return f"~text~{entityId}~{get_icon_id('alert-circle-outline')}~17299~Not found check~ apps.yaml"