From dcee07cffc6db940f5106f971283522f653fc4c2 Mon Sep 17 00:00:00 2001 From: cyrilp Date: Thu, 21 Mar 2024 22:32:33 +0100 Subject: [PATCH] parse cdata --- .../deltadore_tydom/ha_entities.py | 12 ++++- .../deltadore_tydom/tydom/MessageHandler.py | 50 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index 33bbdb1..0aca5ee 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -97,19 +97,26 @@ def get_sensors(self): and value is not None and attribute not in self._registered_sensors ): - if attribute in self.filtered_attrs: + alt_name = attribute.split('_')[0] + if attribute in self.filtered_attrs or alt_name in self.filtered_attrs: continue sensor_class = None if attribute in self.sensor_classes: sensor_class = self.sensor_classes[attribute] + elif alt_name in self.sensor_classes: + sensor_class = self.sensor_classes[alt_name] state_class = None if attribute in self.state_classes: state_class = self.state_classes[attribute] + elif alt_name in self.state_classes: + state_class = self.state_classes[alt_name] unit = None if attribute in self.units: unit = self.units[attribute] + elif alt_name in self.units: + unit = self.units[alt_name] if isinstance(value, bool): sensors.append( @@ -374,6 +381,7 @@ class HAEnergy(SensorEntity, HAEntity): "energyIndexHeatWatt": SensorDeviceClass.ENERGY, "energyIndexECSWatt": SensorDeviceClass.ENERGY, "energyIndexHeatGas": SensorDeviceClass.ENERGY, + "energyIndex": SensorDeviceClass.ENERGY, "outTemperature": SensorDeviceClass.TEMPERATURE, } @@ -383,6 +391,7 @@ class HAEnergy(SensorEntity, HAEntity): "energyIndexECSWatt": SensorStateClass.TOTAL_INCREASING, "energyIndexHeatWatt": SensorStateClass.TOTAL_INCREASING, "energyIndexHeatGas": SensorStateClass.TOTAL_INCREASING, + "energyIndex": SensorStateClass.TOTAL_INCREASING, } units = { @@ -411,6 +420,7 @@ class HAEnergy(SensorEntity, HAEntity): "energyIndexHeatWatt": UnitOfEnergy.WATT_HOUR, "energyIndexECSWatt": UnitOfEnergy.WATT_HOUR, "energyIndexHeatGas": UnitOfEnergy.WATT_HOUR, + "energyIndex": UnitOfEnergy.WATT_HOUR, "outTemperature": UnitOfTemperature.CELSIUS, } diff --git a/custom_components/deltadore_tydom/tydom/MessageHandler.py b/custom_components/deltadore_tydom/tydom/MessageHandler.py index d1f0ccb..8b85b66 100644 --- a/custom_components/deltadore_tydom/tydom/MessageHandler.py +++ b/custom_components/deltadore_tydom/tydom/MessageHandler.py @@ -472,7 +472,55 @@ async def parse_devices_data(self, parsed): async def parse_devices_cdata(self, parsed): """Parse devices cdata.""" - LOGGER.debug("parse_devices_data : %s", parsed) + LOGGER.debug("parse_devices_cdata : %s", parsed) + devices = [] + + for i in parsed: + for endpoint in i["endpoints"]: + if endpoint["error"] == 0 and len(endpoint["cdata"]) > 0: + try: + device_id = i["id"] + endpoint_id = endpoint["id"] + unique_id = str(endpoint_id) + "_" + str(device_id) + name_of_id = self.get_name_from_id(unique_id) + type_of_id = self.get_type_from_id(unique_id) + + data = {} + for elem in endpoint["cdata"]: + if type_of_id == 'conso': + + element_name = None + if elem["parameters"].get("dest"): + element_name = elem["name"] + "_" + elem["parameters"]["dest"] + else: + continue + + element_value = elem["values"]["counter"] + data[element_name] = element_value + + # Create the device + device = await MessageHandler.get_device( + self.tydom_client, + type_of_id, + unique_id, + device_id, + name_of_id, + endpoint_id, + data, + ) + if device is not None: + devices.append(device) + LOGGER.debug( + "Device update (id=%s, endpoint=%s, name=%s, type=%s)", + device_id, + endpoint_id, + name_of_id, + type_of_id, + ) + + except Exception as e: + LOGGER.exception('Error when parsing msg_cdata') + return devices # PUT response DIRTY parsing def parse_put_response(self, bytes_str, start=6):