Skip to content

Commit

Permalink
Add support for SG01HP3 inverters and add enum-type inverter status
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-deptula authored and CarstenGrohmann committed Dec 20, 2024
1 parent aab52f2 commit d423579
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 58 additions & 10 deletions plugins/deye_plugin_ha_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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}"
Expand All @@ -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,
Expand All @@ -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)

Expand Down

0 comments on commit d423579

Please sign in to comment.