import slugify


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


def name_to_id(name):
    return slugify.slugify(name, separator='_')


def add_to_group(groups_dict, device_groups, device_name):
    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'] = []
        groups_dict[group]['entities'].append(name_to_id(device_name))
