More mqtt

This commit is contained in:
Odd Stråbø 2021-11-14 03:32:30 +01:00
parent 71680e9976
commit 4fdb36efc0

View file

@ -6,7 +6,62 @@ import paho.mqtt.client as mqtt
from . import BaseConsumer 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): class MqttConsumer(BaseConsumer):
@ -65,6 +120,7 @@ class MqttConsumer(BaseConsumer):
"unique_id": f"{self.settings['device_id']}_{id}", "unique_id": f"{self.settings['device_id']}_{id}",
"availability_topic": "~/available", "availability_topic": "~/available",
"state_topic": f"~/{id}", "state_topic": f"~/{id}",
"name": name,
"device": { "device": {
"identifiers": [ "identifiers": [
self.settings["device_id"], self.settings["device_id"],
@ -107,7 +163,19 @@ class MqttConsumer(BaseConsumer):
def write(self, data: Dict[str, Any]): def write(self, data: Dict[str, Any]):
self.client.publish(f"{self.topic_prefix}/raw", payload=data) 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): def exit(self):
self.client.publish( self.client.publish(