async def parse_devices_data(self, parsed):
        for i in parsed:
            for endpoint in i["endpoints"]:
                if endpoint["error"] == 0 and len(endpoint["data"]) > 0:
                    try:
                        attr_alarm = {}
                        attr_alarm_details = {}
                        attr_cover = {}
                        attr_cover_details = {}
                        attr_door = {}
                        attr_window = {}
                        attr_light = {}
                        attr_boiler = {}
                        attr_light_details = {}
                        device_id = i["id"]
                        endpoint_id = endpoint["id"]
                        name_of_id = self.get_name_from_id(device_id)
                        type_of_id = self.get_type_from_id(device_id)

                        _LOGGER.debug("======[ DEVICE INFOS ]======")
                        _LOGGER.debug("ID {}".format(device_id))
                        _LOGGER.debug("ENDPOINT ID {}".format(endpoint_id))
                        _LOGGER.debug("Name {}".format(name_of_id))
                        _LOGGER.debug("Type {}".format(type_of_id))
                        _LOGGER.debug("==========================")

                        for elem in endpoint["data"]:
                            _LOGGER.debug("CURRENT ELEM={}".format(elem))
                            # endpoint_id = None

                            # Element name
                            elementName = elem["name"]
                            # Element value
                            elementValue = elem["value"]
                            elementValidity = elem["validity"]
                            print_id = None
                            if len(name_of_id) != 0:
                                print_id = name_of_id
                            #    endpoint_id = device_endpoint[device_id]
                            else:
                                print_id = device_id
                            #    endpoint_id = device_endpoint[device_id]

                            if type_of_id == 'light':
                                if elementName in deviceLightKeywords and elementValidity == 'upToDate':  # NEW METHOD
                                    attr_light['device_id'] = device_id
                                    attr_light['endpoint_id'] = endpoint_id
                                    attr_light['id'] = str(
                                        device_id) + '_' + str(endpoint_id)
                                    attr_light['light_name'] = print_id
                                    attr_light['name'] = print_id
                                    attr_light['device_type'] = 'light'
                                    attr_light[elementName] = elementValue

                            if type_of_id == 'shutter' or type_of_id == 'klineShutter':
                                if elementName in deviceCoverKeywords and elementValidity == 'upToDate':  #NEW METHOD
                                    attr_cover['device_id'] = device_id
                                    attr_cover['endpoint_id'] = endpoint_id
                                    attr_cover['id'] = str(
                                        device_id) + '_' + str(endpoint_id)
                                    attr_cover['cover_name'] = print_id
                                    attr_cover['name'] = print_id
                                    attr_cover['device_type'] = 'cover'
                                    attr_cover[elementName] = elementValue

                            if type_of_id == 'belmDoor' or type_of_id == 'klineDoor':
                                if elementName in deviceDoorKeywords and elementValidity == 'upToDate':  #NEW METHOD
                                    attr_door['device_id'] = device_id
                                    attr_door['endpoint_id'] = endpoint_id
                                    attr_door['id'] = str(
                                        device_id) + '_' + str(endpoint_id)
                                    attr_door['door_name'] = print_id
                                    attr_door['name'] = print_id
                                    attr_door['device_type'] = 'sensor'
                                    attr_door[elementName] = elementValue

                            if type_of_id == 'windowFrench' or type_of_id == 'window' or type_of_id == 'klineWindowFrench':
                                if elementName in deviceDoorKeywords and elementValidity == 'upToDate':  #NEW METHOD
                                    attr_window['device_id'] = device_id
                                    attr_window['endpoint_id'] = endpoint_id
                                    attr_window['id'] = str(
                                        device_id) + '_' + str(endpoint_id)
                                    attr_window['door_name'] = print_id
                                    attr_window['name'] = print_id
                                    attr_window['device_type'] = 'sensor'
                                    attr_window[elementName] = elementValue

                            if type_of_id == 'boiler':
                                if elementName in deviceBoilerKeywords and elementValidity == 'upToDate':  #NEW METHOD
                                    attr_boiler['device_id'] = device_id
                                    attr_boiler['endpoint_id'] = endpoint_id
                                    attr_boiler['id'] = str(
                                        device_id) + '_' + str(endpoint_id)
                                    # attr_boiler['boiler_name'] = print_id
                                    attr_boiler['name'] = print_id
                                    attr_boiler['device_type'] = 'climate'
                                    attr_boiler[elementName] = elementValue

                            if type_of_id == 'alarm':
                                if elementName in deviceAlarmKeywords and elementValidity == 'upToDate':
                                    attr_alarm['device_id'] = device_id
                                    attr_alarm['endpoint_id'] = endpoint_id
                                    attr_alarm['id'] = str(
                                        device_id) + '_' + str(endpoint_id)
                                    attr_alarm['alarm_name'] = "Tyxal Alarm"
                                    attr_alarm['name'] = "Tyxal Alarm"
                                    attr_alarm[
                                        'device_type'] = 'alarm_control_panel'
                                    attr_alarm[elementName] = elementValue

                            if type_of_id == 'conso':
                                if elementName in device_conso_keywords and elementValidity == "upToDate":
                                    attr_conso = {
                                        'device_id':
                                        device_id,
                                        'endpoint_id':
                                        endpoint_id,
                                        'id':
                                        str(device_id) + '_' +
                                        str(endpoint_id),
                                        'name':
                                        print_id,
                                        'device_type':
                                        'sensor',
                                        elementName:
                                        elementValue
                                    }

                                    if elementName in device_conso_classes:
                                        attr_conso[
                                            'device_class'] = device_conso_classes[
                                                elementName]

                                    if elementName in device_conso_unit_of_measurement:
                                        attr_conso[
                                            'unit_of_measurement'] = device_conso_unit_of_measurement[
                                                elementName]

                                    new_conso = sensor(
                                        elem_name=elementName,
                                        tydom_attributes_payload=attr_conso,
                                        attributes_topic_from_device='useless',
                                        mqtt=self.mqtt_client)
                                    await new_conso.update()

                    except Exception as e:
                        print('msg_data error in parsing !')
                        print(e)

                    if 'device_type' in attr_cover and attr_cover[
                            'device_type'] == 'cover':
                        # print(attr_cover)
                        new_cover = "cover_tydom_" + str(device_id)
                        new_cover = Cover(tydom_attributes=attr_cover,
                                          mqtt=self.mqtt_client)  #NEW METHOD
                        # new_cover = Cover(id=endpoint_id,name=print_id, current_position=elementValue, attributes=i, mqtt=self.mqtt_client)
                        await new_cover.update()
                    elif 'device_type' in attr_door and attr_door[
                            'device_type'] == 'sensor':
                        # print(attr_cover)
                        new_door = "door_tydom_" + str(device_id)
                        new_door = sensor(
                            elem_name='openState',
                            tydom_attributes_payload=attr_door,
                            attributes_topic_from_device='useless',
                            mqtt=self.mqtt_client)
                        # new_cover = Cover(id=endpoint_id,name=print_id, current_position=elementValue, attributes=i, mqtt=self.mqtt_client)
                        await new_door.update()
                    elif 'device_type' in attr_window and attr_window[
                            'device_type'] == 'sensor':
                        # print(attr_cover)
                        new_window = "window_tydom_" + str(device_id)
                        new_window = sensor(
                            elem_name='openState',
                            tydom_attributes_payload=attr_window,
                            attributes_topic_from_device='useless',
                            mqtt=self.mqtt_client)
                        # new_cover = Cover(id=endpoint_id,name=print_id, current_position=elementValue, attributes=i, mqtt=self.mqtt_client)
                        await new_window.update()
                    elif 'device_type' in attr_light and attr_light[
                            'device_type'] == 'light':
                        # print(attr_cover)
                        new_light = "light_tydom_" + str(device_id)
                        new_light = Light(tydom_attributes=attr_light,
                                          mqtt=self.mqtt_client)  #NEW METHOD
                        # new_cover = Cover(id=endpoint_id,name=print_id, current_position=elementValue, attributes=i, mqtt=self.mqtt_client)
                        await new_light.update()
                    elif 'device_type' in attr_boiler and attr_boiler[
                            'device_type'] == 'climate':
                        # print(attr_boiler)
                        new_boiler = "boiler_tydom_" + str(device_id)
                        new_boiler = Boiler(tydom_attributes=attr_boiler,
                                            tydom_client=self.tydom_client,
                                            mqtt=self.mqtt_client)  #NEW METHOD
                        # new_cover = Cover(id=endpoint_id,name=print_id, current_position=elementValue, attributes=i, mqtt=self.mqtt_client)
                        await new_boiler.update()
                # Get last known state (for alarm) # NEW METHOD
                    elif 'device_type' in attr_alarm and attr_alarm[
                            'device_type'] == 'alarm_control_panel':
                        # print(attr_alarm)
                        state = None
                        sos_state = False
                        maintenance_mode = False
                        out = None
                        try:
                            # {
                            # "name": "alarmState",
                            # "type": "string",
                            # "permission": "r",
                            # "enum_values": ["OFF", "DELAYED", "ON", "QUIET"]
                            # },
                            # {
                            # "name": "alarmMode",
                            # "type": "string",
                            # "permission": "r",
                            # "enum_values": ["OFF", "ON", "TEST", "ZONE", "MAINTENANCE"]
                            # }

                            if ('alarmState' in attr_alarm
                                    and attr_alarm['alarmState'] == "ON"
                                ) or ('alarmState' in attr_alarm
                                      and attr_alarm['alarmState']) == "QUIET":
                                state = "triggered"

                            elif 'alarmState' in attr_alarm and attr_alarm[
                                    'alarmState'] == "DELAYED":
                                state = "pending"

                            if 'alarmSOS' in attr_alarm and attr_alarm[
                                    'alarmSOS'] == "true":
                                state = "triggered"
                                sos_state = True

                            elif 'alarmMode' in attr_alarm and attr_alarm[
                                    "alarmMode"] == "ON":
                                state = "armed_away"
                            elif 'alarmMode' in attr_alarm and attr_alarm[
                                    "alarmMode"] == "ZONE":
                                state = "armed_home"
                            elif 'alarmMode' in attr_alarm and attr_alarm[
                                    "alarmMode"] == "OFF":
                                state = "disarmed"
                            elif 'alarmMode' in attr_alarm and attr_alarm[
                                    "alarmMode"] == "MAINTENANCE":
                                maintenance_mode = True
                                state = "disarmed"

                            if 'outTemperature' in attr_alarm:
                                out = attr_alarm["outTemperature"]

                            if (sos_state == True):
                                print("SOS !")

                            if not (state == None):
                                # print(state)
                                alarm = "alarm_tydom_" + str(endpoint_id)
                                # print("Alarm created / updated : "+alarm)
                                alarm = Alarm(current_state=state,
                                              tydom_attributes=attr_alarm,
                                              mqtt=self.mqtt_client)
                                await alarm.update()

                        except Exception as e:
                            print("Error in alarm parsing !")
                            print(e)
                            pass
                    else:
                        pass
示例#2
0
def runCover():
    top.iconify()
    Cover()