mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-31 23:10:05 +02:00
started refactoring controller
added debug messages
This commit is contained in:
parent
81f25765f7
commit
477dd630d8
2 changed files with 132 additions and 91 deletions
74
custom_components/mikrotik_router/helper.py
Normal file
74
custom_components/mikrotik_router/helper.py
Normal file
|
@ -0,0 +1,74 @@
|
|||
"""Helper functions for Mikrotik Router."""
|
||||
|
||||
import logging
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# from_entry
|
||||
# ---------------------------
|
||||
def from_entry(entry, param, default=""):
|
||||
"""Validate and return a value from a Mikrotik API dict"""
|
||||
if param not in entry:
|
||||
return default
|
||||
|
||||
return entry[param]
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# from_entry_bool
|
||||
# ---------------------------
|
||||
def from_entry_bool(entry, param, default=False, reverse=False):
|
||||
"""Validate and return a bool value from a Mikrotik API dict"""
|
||||
if param not in entry:
|
||||
return default
|
||||
|
||||
if not reverse:
|
||||
ret = entry[param]
|
||||
else:
|
||||
if entry[param]:
|
||||
ret = False
|
||||
else:
|
||||
ret = True
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# from_list
|
||||
# ---------------------------
|
||||
async def from_list(data=None, source=None, key=None, vals=[], ensure_vals=[]):
|
||||
if not source:
|
||||
return data
|
||||
|
||||
for entry in source:
|
||||
if key not in entry:
|
||||
continue
|
||||
|
||||
if not entry[key]:
|
||||
continue
|
||||
|
||||
_LOGGER.debug("Processing entry {}, entry {}".format(source, entry))
|
||||
uid = entry[key]
|
||||
if uid not in data:
|
||||
data[uid] = {}
|
||||
|
||||
for val in vals:
|
||||
_name = val['name']
|
||||
_type = val['type'] if 'type' in val else 'str'
|
||||
_source = val['source'] if 'source' in val else _name
|
||||
|
||||
if _type == 'str':
|
||||
_default = val['default'] if 'default' in val else ''
|
||||
data[uid][_name] = from_entry(entry, _source, default=_default)
|
||||
elif _type == 'bool':
|
||||
_default = val['default'] if 'default' in val else False
|
||||
_reverse = val['reverse'] if 'reverse' in val else False
|
||||
data[uid][_name] = from_entry_bool(entry, _source, default=_default, reverse=_reverse)
|
||||
|
||||
for val in ensure_vals:
|
||||
if val['name'] not in data[uid]:
|
||||
_default = val['default'] if 'default' in val else ''
|
||||
data[uid][val['name']] = _default
|
||||
|
||||
return data
|
Loading…
Add table
Add a link
Reference in a new issue