From 4fdb36efc0e36fe1761e862ecbe3da744d8bb6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Sun, 14 Nov 2021 03:32:30 +0100 Subject: [PATCH] More mqtt --- consumers/mqtt.py | 72 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/consumers/mqtt.py b/consumers/mqtt.py index a8e4d2b..f7fa483 100644 --- a/consumers/mqtt.py +++ b/consumers/mqtt.py @@ -6,7 +6,62 @@ import paho.mqtt.client as mqtt from . import BaseConsumer -# MAP_VALUES = {} +MAP_VALUES: Dict[str, Dict[str, Any]] = { + # "battery_voltage_min", + # "battery_voltage_max", + # "charge_max_current", + # "_discharge_max_current?", + # "charge_max_power", + # "discharge_max_power", + # "charge_amp_hour", + # "discharge_amp_hour", + "production_power": { + "unit": "Wh", + "type": "energy", + "state_class": "total_increasing", + }, + "consumption_power": { + "unit": "Wh", + "type": "energy", + "state_class": "total_increasing", + }, + # "run_days", + # "discharge_count", + # "full_charge_count", + # "total_charge_amp_hours", + # "total_discharge_amp_hours", + "total_production_power": { + "unit": "Wh", + "type": "energy", + "state_class": "total_increasing", + }, + "total_consumption_power": { + "unit": "Wh", + "type": "energy", + "state_class": "total_increasing", + }, + # + "battery_charge": {"unit": "%", "type": "battery", "state_class": "measurement"}, + "battery_voltage": {"unit": "V", "type": "voltage", "state_class": "measurement"}, + "battery_current": {"unit": "A", "type": "current", "state_class": "measurement"}, + "internal_temperature": { + "unit": "°C", + "type": "temperature", + "state_class": "measurement", + }, + "battery_temperature": { + "unit": "°C", + "type": "temperature", + "state_class": "measurement", + }, + "load_voltage": {"unit": "V", "type": "voltage", "state_class": "measurement"}, + "load_current": {"unit": "A", "type": "current", "state_class": "measurement"}, + "load_power": {"unit": "W", "type": "power", "state_class": "measurement"}, + "panel_voltage": {"unit": "V", "type": "voltage", "state_class": "measurement"}, + "panel_current": {"unit": "A", "type": "current", "state_class": "measurement"}, + "panel_power": {"unit": "W", "type": "power", "state_class": "measurement"}, + # "load_enabled", +} class MqttConsumer(BaseConsumer): @@ -65,6 +120,7 @@ class MqttConsumer(BaseConsumer): "unique_id": f"{self.settings['device_id']}_{id}", "availability_topic": "~/available", "state_topic": f"~/{id}", + "name": name, "device": { "identifiers": [ self.settings["device_id"], @@ -107,7 +163,19 @@ class MqttConsumer(BaseConsumer): def write(self, data: Dict[str, Any]): self.client.publish(f"{self.topic_prefix}/raw", payload=data) - return super().write(data) + + for k, v in data.items(): + if k in MAP_VALUES: + if k not in self.initialized: + pretty_name = k.replace("_", " ").capitalize() + self.client.publish( + f"{self.settings['discovery_prefix']}/sensor/{self.settings['device_id']}/config", # noqa: E501 + self.get_ha_config(k, pretty_name, **MAP_VALUES[k]), + retain=True, + ) + self.initialized.append(k) + + self.client.publish(f"{self.topic_prefix}/{k}", v, retain=True) def exit(self): self.client.publish(