2019-06-02 14:34:08 +02:00
|
|
|
import slugify
|
2019-06-02 09:36:19 +02:00
|
|
|
|
2019-06-02 14:34:08 +02:00
|
|
|
|
|
|
|
class DeviceInfo:
|
|
|
|
def __init__(self, csv_name, display_name, groups=()):
|
|
|
|
self.csv_name = csv_name
|
|
|
|
self.display_name = display_name
|
|
|
|
if not (isinstance(groups, list) or isinstance(groups, tuple)):
|
|
|
|
groups = [groups]
|
|
|
|
self.groups = groups
|
|
|
|
|
|
|
|
|
|
|
|
def extent(result_dict, input_dict, platform):
|
|
|
|
for k, v in input_dict.items():
|
|
|
|
if k not in result_dict:
|
|
|
|
result_dict[k] = []
|
|
|
|
for entry in v:
|
|
|
|
entry['platform'] = platform
|
|
|
|
result_dict[k] += v
|
|
|
|
|
|
|
|
|
2019-06-11 19:28:39 +02:00
|
|
|
def name_to_id(name, device_type):
|
2019-07-07 17:31:38 +02:00
|
|
|
if device_type is None:
|
|
|
|
return slugify.slugify(name, separator='_')
|
|
|
|
else:
|
|
|
|
return "{}.{}".format(device_type, slugify.slugify(name, separator='_'))
|
2019-06-02 14:34:08 +02:00
|
|
|
|
|
|
|
|
2019-06-11 19:28:39 +02:00
|
|
|
def add_to_group(groups_dict, device_groups, device_name, device_type):
|
2019-06-02 14:34:08 +02:00
|
|
|
for group in device_groups:
|
|
|
|
if group not in groups_dict:
|
|
|
|
raise ValueError(f"FHEM device {device_name} wants to be added to unknown group {group}")
|
|
|
|
if 'entities' not in groups_dict[group]:
|
|
|
|
groups_dict[group]['entities'] = []
|
2019-06-11 19:28:39 +02:00
|
|
|
groups_dict[group]['entities'].append(name_to_id(device_name, device_type))
|