def show_config_overview(cfgs: List[ThingConfigChanger], all_params): t = Table(heading='Current configuration') c_p = t.add_column('Parameter') c_s = [ t.add_column( c.uid, alias=':'.join(c.uid.split(':')[-2:]) if ':' in c.uid else None) for c in cfgs ] for p in itertools.chain( sorted(filter(lambda x: isinstance(x, int), all_params)), sorted(filter(lambda x: not isinstance(x, int), all_params))): c_p.add(str(p)) for col, cfg in zip(c_s, cfgs): v = cfg.get(p, '') col.add(str(v)) for line in t.get_lines(): log.info(line)
def log_overview(data: List[dict], aliases: Dict[str, str], heading=''): table = Table(heading) for k in aliases: table.add_column(k, align='<') for _item in data: table.add_dict( {k: _item.get(alias, '') for k, alias in aliases.items()}) for line in table.get_lines(sort_columns=['thing_type'] if 'thing_type' in table.columns else None): log.info(line)
def test_wrap(): table = Table('my heading') c1 = table.add_column('col1') c2 = table.add_column('col2', wrap=20) c1.add('12') c1.add(345) c2.add(['asdfasdfasdf', 'defdefdef', 'adfiopfafds', 'jkdfsj']) c2.add('ljkäöjio') assert table.get_lines() == [ '+--------------------------------+', '| my heading |', '+------+-------------------------+', '| col1 | col2 |', '+------+-------------------------+', '| 12 | asdfasdfasdf, defdefdef |', '| | adfiopfafds, jkdfsj |', '| 345 | ljkäöjio |', '+------+-------------------------+' ]
def test_table(): table = Table('my heading') c1 = table.add_column('col1') c2 = table.add_column('col2') c1.add('12') c1.add(345) c1.add(23) c2.add('asdf') c2.add('ljkäöjio') c2.add('jfhgf') assert table.get_lines() == [ '+-----------------+', '| my heading |', '+------+----------+', '| col1 | col2 |', '+------+----------+', '| 12 | asdf |', '| 345 | ljkäöjio |', '| 23 | jfhgf |', '+------+----------+' ] assert table.get_lines(sort_columns=['col2']) == [ '+-----------------+', '| my heading |', '+------+----------+', '| col1 | col2 |', '+------+----------+', '| 12 | asdf |', '| 23 | jfhgf |', '| 345 | ljkäöjio |', '+------+----------+' ]
async def on_connect_function(self): # don't run this after the connect, let the rules load etc. await asyncio.sleep(60) # show this overview only once! if self.run: return None self.run = True thing_data = await async_get_things() thing_table = Table('Things') thing_stat = thing_table.add_column('Status', '^') thing_label = thing_table.add_column('Label') thing_location = thing_table.add_column('Location') thing_type = thing_table.add_column('Thing type') thing_uid = thing_table.add_column('Thing UID') zw_table = Table(heading='Z-Wave Things') zw_node = zw_table.add_column('Node', '^') zw_stat = zw_table.add_column('Status', '^') zw_label = zw_table.add_column('Label') zw_location = zw_table.add_column('Location') zw_model = zw_table.add_column('Model', '^') zw_fw = zw_table.add_column('Firmware', '^') zw_type = zw_table.add_column('Thing type') zw_uid = zw_table.add_column('Thing UID') # zw_l_channels = zw_table.add_column('Linked channel types') # zw_u_channels = zw_table.add_column('Unlinked channel types') for node in thing_data: uid = node['UID'] type_uid = node['thingTypeUID'] is_zw = type_uid.startswith('zwave:') col_uid, col_stat, col_label, col_location, col_type = \ (thing_uid, thing_stat, thing_label, thing_location, thing_type) if not is_zw else \ (zw_uid, zw_stat, zw_label, zw_location, zw_type) col_uid.add(uid) col_type.add(type_uid) col_label.add(node.get('label', '')) col_stat.add(node['statusInfo']['status']) col_location.add(node.get('location', '')) if not is_zw: continue # optional properties which can be set props = node['properties'] # channels = node.get('channels', []) # Node-ID, e.g. 5 node_id = props.get('zwave_nodeid') zw_node.add(int(node_id) if node_id is not None else '') zw_model.add(props.get('modelId', '')) zw_fw.add(props.get('zwave_version', '')) # This generates very long logs and doesn't look good # zw_l_channels.add( # tuple(map(lambda x: x.get('channelTypeUID', ''), filter(lambda x: x.get('linkedItems'), channels))) # ) # zw_u_channels.add( # tuple(map(lambda x: x.get('channelTypeUID', ''), # filter(lambda x: not x.get('linkedItems'), channels))) # ) log = logging.getLogger('HABApp.openhab.things') for line in thing_table.get_lines(sort_columns=[thing_uid]): log.info(line) log = logging.getLogger('HABApp.openhab.zwave') for line in zw_table.get_lines(sort_columns=[zw_type, 'Node']): log.info(line)