mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2025-12-24 00:14:26 +01:00
started on new config format
This commit is contained in:
@@ -2,98 +2,27 @@ import logging
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class PageNode(object):
|
||||
def __init__(self, data, parent=None):
|
||||
self.data = data
|
||||
self.name = None
|
||||
self.childs = []
|
||||
self.parent = parent
|
||||
self.pos = None
|
||||
HA_API = None
|
||||
|
||||
if "items" in data:
|
||||
childs = data.pop("items")
|
||||
for page in childs:
|
||||
self.add_child(PageNode(page, self))
|
||||
class Entity(object):
|
||||
def __init__(self, entity_input_config):
|
||||
self.entityId = entity_input_config.get("entity", "unknown")
|
||||
self.nameOverride = entity_input_config.get("name")
|
||||
self.iconOverride = entity_input_config.get("icon")
|
||||
|
||||
name = self.data.get("heading", "unkown") if type(self.data) is dict else self.data
|
||||
ptype = self.data.get("type", "unkown") if type(self.data) is dict else "leaf"
|
||||
class Card(object):
|
||||
def __init__(self, card_input_config):
|
||||
self.cardType = card_input_config.get("type", "unknown")
|
||||
self.title = card_input_config.get("title", "unknown")
|
||||
# for single entity card like climate or media
|
||||
self.entity = None
|
||||
if card_input_config.get("entity") is not None:
|
||||
self.entity = Entity(card_input_config.get("entity"))
|
||||
# for pages like grid or entities
|
||||
self.entities = []
|
||||
for e in card_input_config.get("entities", []):
|
||||
self.entities.append(Entity(e))
|
||||
|
||||
self.name = f"{ptype}.{name}" if type(self.data) is dict else self.data
|
||||
self.name = self.name.replace(".","_")
|
||||
self.name = self.name.replace(",","_")
|
||||
self.name = self.name.replace(" ","_")
|
||||
|
||||
def add_child(self, obj):
|
||||
obj.pos = len(self.childs)
|
||||
self.childs.append(obj)
|
||||
|
||||
def next(self):
|
||||
if self.parent is not None:
|
||||
pos = self.pos
|
||||
length = len(self.parent.childs)
|
||||
return self.parent.childs[(pos+1)%length]
|
||||
else:
|
||||
return self
|
||||
def prev(self):
|
||||
if self.parent is not None:
|
||||
pos = self.pos
|
||||
length = len(self.parent.childs)
|
||||
return self.parent.childs[(pos-1)%length]
|
||||
else:
|
||||
return self
|
||||
|
||||
def search_page_by_name(self, name):
|
||||
name = name.replace("navigate.", "")
|
||||
pages = []
|
||||
for i in self.childs:
|
||||
# compare name of current page
|
||||
if i.name == name:
|
||||
pages.append(i)
|
||||
# current pages has also childs
|
||||
if len(i.childs) > 0:
|
||||
pages.extend(i.search_page_by_name(name))
|
||||
return pages
|
||||
|
||||
return items
|
||||
|
||||
def dump(self, indent=0):
|
||||
"""dump tree to string"""
|
||||
tab = ' '*(indent-1) + ' |- ' if indent > 0 else ''
|
||||
name = self.name
|
||||
parent = self.parent.name if self.parent is not None else "root"
|
||||
dumpstring = f"{tab}{self.pos}:{name} -> {parent} \n"
|
||||
for obj in self.childs:
|
||||
dumpstring += obj.dump(indent + 1)
|
||||
return dumpstring
|
||||
|
||||
def get_items(self):
|
||||
items = []
|
||||
for i in self.childs:
|
||||
if len(i.childs) > 0:
|
||||
items.append(f"navigate.{i.name}")
|
||||
else:
|
||||
items.append(i.data)
|
||||
return items
|
||||
|
||||
def get_all_item_names(self, recursive=True):
|
||||
items = []
|
||||
# current page
|
||||
if type(self.data) is dict:
|
||||
items.append(self.data.get("item", next(iter(self.data))))
|
||||
else:
|
||||
items.append(self.data)
|
||||
# childs of page
|
||||
for i in self.childs:
|
||||
if len(i.childs) > 0:
|
||||
if recursive:
|
||||
items.extend(i.get_all_item_names())
|
||||
else:
|
||||
if type(i.data) is dict:
|
||||
items.append(i.data.get("item", next(iter(i.data))))
|
||||
else:
|
||||
items.append(i.data)
|
||||
return items
|
||||
|
||||
class LuiBackendConfig(object):
|
||||
|
||||
_DEFAULT_CONFIG = {
|
||||
@@ -115,27 +44,41 @@ class LuiBackendConfig(object):
|
||||
'weatherOverrideForecast3': None,
|
||||
'weatherOverrideForecast4': None,
|
||||
'doubleTapToUnlock': False,
|
||||
'pages': [{
|
||||
'type': 'cardEntities',
|
||||
'heading': 'Test Entities 1',
|
||||
'items': ['switch.test_item', 'switch.test_item', 'switch.test_item']
|
||||
}, {
|
||||
'type': 'cardGrid',
|
||||
'heading': 'Test Grid 1',
|
||||
'items': ['switch.test_item', 'switch.test_item', 'switch.test_item']
|
||||
}
|
||||
]
|
||||
'cards': [{
|
||||
'type': 'entities',
|
||||
'entities': [{
|
||||
'entity': 'switch.test_item',
|
||||
'name': 'Test Item'
|
||||
}, {
|
||||
'entity': 'switch.test_item'
|
||||
}],
|
||||
'title': 'Example Entities Page'
|
||||
}, {
|
||||
'type': 'grid',
|
||||
'entities': [{
|
||||
'entity': 'switch.test_item'
|
||||
}, {
|
||||
'entity': 'switch.test_item'
|
||||
}, {
|
||||
'entity': 'switch.test_item'
|
||||
}
|
||||
],
|
||||
'title': 'Example Grid Page'
|
||||
}, {
|
||||
'type': 'climate',
|
||||
'entity': 'climate.test_item'
|
||||
'title': 'Example Climate Page'
|
||||
}]
|
||||
}
|
||||
|
||||
def __init__(self, args=None, check=True):
|
||||
def __init__(self, ha_api, config_in):
|
||||
global HA_API
|
||||
HA_API = ha_api
|
||||
self._config = {}
|
||||
self._page_config = None
|
||||
|
||||
if args:
|
||||
self.load(args)
|
||||
self._config_cards = []
|
||||
self._current_card = None
|
||||
|
||||
if check:
|
||||
self.check()
|
||||
self.load(config_in)
|
||||
|
||||
def load(self, args):
|
||||
for k, v in args.items():
|
||||
@@ -143,13 +86,11 @@ class LuiBackendConfig(object):
|
||||
self._config[k] = v
|
||||
LOGGER.info(f"Loaded config: {self._config}")
|
||||
|
||||
root_page = {"items": self.get("pages"), "type": "internal", "heading": "root"}
|
||||
self._page_config = PageNode(root_page)
|
||||
for card in self.get("cards"):
|
||||
self._config_cards.append(Card(card))
|
||||
# set current card to first card
|
||||
self._current_card = self._config_cards[0]
|
||||
|
||||
LOGGER.info(f"Parsed Page config to the following Tree: \n {self._page_config.dump()}")
|
||||
|
||||
def check(self):
|
||||
return
|
||||
|
||||
def get(self, name):
|
||||
value = self._config.get(name)
|
||||
@@ -157,6 +98,3 @@ class LuiBackendConfig(object):
|
||||
value = self._DEFAULT_CONFIG.get(name)
|
||||
return value
|
||||
|
||||
def get_root_page(self):
|
||||
return self._page_config
|
||||
|
||||
|
||||
Reference in New Issue
Block a user