Partial manual merge of forgotten branch output-toggle

This commit is contained in:
Odd Stråbø 2023-12-12 11:29:44 +01:00
parent 67a25eeef9
commit 33acd05b8a
2 changed files with 34 additions and 5 deletions

View file

@ -82,6 +82,10 @@ MAP_VALUES: Dict[DataName, Dict[str, Any]] = {
"state_class": "measurement", "state_class": "measurement",
}, },
DataName.LOAD_POWER: {"unit": "W", "type": "power", "state_class": "measurement"}, DataName.LOAD_POWER: {"unit": "W", "type": "power", "state_class": "measurement"},
DataName.LOAD_ENABLED: {
"type": "outlet",
"platform": "switch",
},
DataName.PANEL_VOLTAGE: { DataName.PANEL_VOLTAGE: {
"unit": "V", "unit": "V",
"type": "voltage", "type": "voltage",
@ -199,6 +203,7 @@ class MqttConsumer(BaseConsumer):
type: Optional[str] = None, type: Optional[str] = None,
expiry: int = 90, expiry: int = 90,
state_class: Optional[str] = None, state_class: Optional[str] = None,
platform: str = "sensor",
): ):
assert state_class in [None, "measurement", "total", "total_increasing"] assert state_class in [None, "measurement", "total", "total_increasing"]
assert self.controller is not None assert self.controller is not None
@ -206,7 +211,7 @@ class MqttConsumer(BaseConsumer):
res = { res = {
"~": f"{self.topic_prefix}", "~": f"{self.topic_prefix}",
"unique_id": f"{self.controller_id}_{id}", "unique_id": f"{self.controller_id}_{id}",
"object_id": f"{self.controller_id}_{id}", "object_id": f"{self.controller_id}_{id}", # Used for entity id
"availability_topic": "~/available", "availability_topic": "~/available",
"state_topic": f"~/{id}", "state_topic": f"~/{id}",
"name": name, "name": name,
@ -216,7 +221,7 @@ class MqttConsumer(BaseConsumer):
], ],
"manufacturer": self.controller.manufacturer, "manufacturer": self.controller.manufacturer,
"model": self.controller.model, "model": self.controller.model,
"hw_version": self.controller.version, "sw_version": self.controller.version,
"via_device": self.settings["device_id"], "via_device": self.settings["device_id"],
"suggested_area": "Solar panel", "suggested_area": "Solar panel",
"name": self.controller.name, "name": self.controller.name,
@ -231,7 +236,10 @@ class MqttConsumer(BaseConsumer):
res["dev_cla"] = type res["dev_cla"] = type
if state_class: if state_class:
res["state_class"] = state_class res["state_class"] = state_class
if platform == "switch":
res["command_topic"] = f"{res['state_topic']}/set"
res["payload_on"] = True
res["payload_off"] = False
return res return res
# The callback for when the client receives a CONNACK response from the server. # The callback for when the client receives a CONNACK response from the server.
@ -246,6 +254,26 @@ class MqttConsumer(BaseConsumer):
f"{userdata.topic_prefix}/available", payload="online", retain=True f"{userdata.topic_prefix}/available", payload="online", retain=True
) )
load_set_topic = f"{userdata.topic_prefix}/load_enabled/set"
client.message_callback_add(load_set_topic, userdata.on_load_switch)
client.subscribe(load_set_topic)
@staticmethod
def on_load_switch(
client: mqtt.Client, userdata: "MqttConsumer", message: mqtt.MQTTMessage
):
assert userdata.controller is not None
print(message)
print(message.info)
print(message.state)
print(message.payload)
payload = message.payload.decode().upper() in ("ON", "TRUE", "ENABLE", "YES")
res = userdata.controller.load_enabled = payload
client.publish(
f"{userdata.topic_prefix}/load_enabled", payload=res, retain=True
)
@staticmethod @staticmethod
def on_connect_fail(client: mqtt.Client, userdata: "MqttConsumer"): def on_connect_fail(client: mqtt.Client, userdata: "MqttConsumer"):
print(userdata.__class__.__name__, "on_connect_fail") print(userdata.__class__.__name__, "on_connect_fail")
@ -283,9 +311,10 @@ class MqttConsumer(BaseConsumer):
km = MAP_VALUES[DataName(dataname)] km = MAP_VALUES[DataName(dataname)]
pretty_name = dataname.replace("_", " ").capitalize() pretty_name = dataname.replace("_", " ").capitalize()
disc_prefix = self.settings["discovery_prefix"] disc_prefix = self.settings["discovery_prefix"]
platform = km.get("platform", "sensor")
self.client.publish( self.client.publish(
f"{disc_prefix}/sensor/{self.controller_id}/{dataname}/config", f"{disc_prefix}/{platform}/{self.controller_id}/{dataname}/config",
payload=json.dumps( payload=json.dumps(
self.get_ha_config(dataname, pretty_name, **km) self.get_ha_config(dataname, pretty_name, **km)
), ),

View file

@ -250,7 +250,7 @@ class ChargeController:
p2 = data[1] p2 = data[1]
p3 = (data[2] << 8) + data[3] p3 = (data[2] << 8) + data[3]
self._cached_serial = f"{p1}-{p2}-{p3}" self._cached_serial = f"{p1:02n}-{p2:02n}-{p3:04n}"
return self._cached_serial return self._cached_serial
_cached_model: str | None = None _cached_model: str | None = None