From d42357989c175f7f08f38c858c6dab3d33ba08bb Mon Sep 17 00:00:00 2001 From: daniel-deptula Date: Fri, 29 Nov 2024 15:58:09 +0100 Subject: [PATCH] Add support for SG01HP3 inverters and add enum-type inverter status just added in https://github.com/kbialek/deye-inverter-mqtt/pull/214 --- README.md | 3 ++ plugins/deye_plugin_ha_discovery.py | 68 ++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 57c6ce9..0765ab3 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,9 @@ updating the "time of use" (ToU) configuration has not been implemented yet. ## Changelog +### 2024-12-11 +* Add support for sensor device class "enum" and some other sensors by [@daniel-deptula](https://github.com/daniel-deptula) + ### 2024-11-20 * Add support for active power regulation * Internal code changes diff --git a/plugins/deye_plugin_ha_discovery.py b/plugins/deye_plugin_ha_discovery.py index 1ef33c2..d534927 100644 --- a/plugins/deye_plugin_ha_discovery.py +++ b/plugins/deye_plugin_ha_discovery.py @@ -130,12 +130,26 @@ def _get_device_class(topic: str) -> str: # topic: ac/l*/voltage # topic: dc/pv*/voltage - if topic.endswith("/voltage"): + # topic: bms/*/charging_voltage + # topic: bms/*/discharge_voltage + if ( + topic.endswith("/voltage") + or topic.endswith("/charging_voltage") + or topic.endswith("/discharge_voltage") + ): device_class = "voltage" # topic: ac/l*/current # topic: dc/pv*/current - elif topic.endswith("/current"): + # topic: bms/*/charge_current_limit + # topic: bms/*/discharge_current_limit + # topic: bms/*/discharge_max_current + elif ( + topic.endswith("/current") + or topic.endswith("charge_current_limit") + or topic.endswith("/charging_max_current") + or topic.endswith("/discharge_max_current") + ): device_class = "current" # topic: battery/(daily|total)_(charge|discharge) @@ -166,18 +180,27 @@ def _get_device_class(topic: str) -> str: elif topic.endswith("/freq"): device_class = "frequency" + # topic: ac/temperature + # topic: battery/temperature + # topic: battery/*/temperature + # topic: radiator_temp + elif ( + topic.endswith("temperature") + or topic.endswith("/temp") + or topic == "radiator_temp" + ): + device_class = "temperature" + # topic: battery/soc - elif topic == "battery/soc": + # topic: bms/*/soc + elif topic.endswith("/soc") or topic.startswith("bms/"): device_class = "battery" elif topic == "uptime": device_class = "duration" - # topic: ac/temperature - # topic: battery/temperature - # topic: radiator_temp - elif topic.endswith("temperature") or topic == "radiator_temp": - device_class = "temperature" + elif topic == "inverter/status": + device_class = "enum" return device_class @@ -207,10 +230,13 @@ def _get_state_class(topic: str) -> str: # topic: (day|total)_energy # topic: dc/pv*/(day|total)_energy # topic: uptime + # topic: ac/(daily|total)_energy_(bought|sold) if ( topic.endswith("_charge") or topic.endswith("_discharge") or topic.endswith("_energy") + or topic.endswith("_energy_bought") + or topic.endswith("_energy_sold") or topic == "uptime" ): state_class = "total_increasing" @@ -231,6 +257,20 @@ def _get_state_class(topic: str) -> str: return state_class + @staticmethod + @functools.cache + def _get_options(topic: str) -> str: + """Return entity options based on a given topic + + Args: + topic (str): MQTT topic for the sensor value + """ + options = [] + if topic == "inverter/status": + options = ["standby", "selfcheck", "normal", "alarm", "fault"] + + return options + def publish_sensor_information(self, topic: str, observation: Observation): """Send HA discovery messages about available sensors @@ -248,7 +288,10 @@ def publish_sensor_information(self, topic: str, observation: Observation): ) return - state_class = self._get_state_class(mqtt_topic_suffix) + if device_class == "enum": + enum_options = self._get_options(mqtt_topic_suffix) + else: + state_class = self._get_state_class(mqtt_topic_suffix) discovery_prefix = self.ha_discovery_prefix node_id = f"{self.component_prefix}_{self._config.logger.serial_number}" @@ -263,7 +306,6 @@ def publish_sensor_information(self, topic: str, observation: Observation): "unique_id": self._get_unique_id(observation.sensor.name), "force_update": True, "device_class": device_class, - "state_class": state_class, "unit_of_measurement": self._adapt_unit(observation.sensor.unit), "availability_topic": f"{self._config.mqtt.topic_prefix}/status", "state_topic": topic, @@ -276,6 +318,12 @@ def publish_sensor_information(self, topic: str, observation: Observation): "sw_version": f"deye-inverter-mqtt with {self.get_id()}", }, } + if device_class == "enum": + del discover_config["unit_of_measurement"] + discover_config["options"] = enum_options + else: + discover_config["state_class"] = state_class + payload = json.dumps(discover_config) self._mqtt_client.publish(discovery_topic, payload)