Skip to content

Commit f03ef1e

Browse files
jtkDvlpJulian Knabenschuh
authored andcommitted
Add ha_discovery support for serial stream
1 parent 10abb89 commit f03ef1e

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

mqtt_io/home_assistant.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
from typing import Any, Dict
1010

11-
from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, SET_SUFFIX
11+
from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, STREAM_TOPIC, SET_SUFFIX, SEND_SUFFIX
1212
from .mqtt import MQTTClientOptions, MQTTMessageSend
1313
from .types import ConfigType
1414
from . import VERSION
@@ -148,3 +148,35 @@ def hass_announce_sensor_input(
148148
json.dumps(sensor_config).encode("utf8"),
149149
retain=True,
150150
)
151+
152+
def hass_announce_stream(
153+
in_conf: ConfigType, mqtt_conf: ConfigType, mqtt_options: MQTTClientOptions
154+
) -> MQTTMessageSend:
155+
"""
156+
Create a message which announces stream as text to Home Assistant.
157+
"""
158+
disco_conf: ConfigType = mqtt_conf["ha_discovery"]
159+
name: str = in_conf["name"]
160+
disco_prefix: str = disco_conf["prefix"]
161+
stream_config = get_common_config(in_conf, mqtt_conf, mqtt_options)
162+
stream_config.update(
163+
dict(
164+
unique_id=f"{mqtt_options.client_id}_{in_conf['module']}_input_{name}",
165+
state_topic="/".join((mqtt_conf["topic_prefix"], STREAM_TOPIC, name)),
166+
command_topic="/".join((mqtt_conf["topic_prefix"], STREAM_TOPIC, name, SEND_SUFFIX))
167+
)
168+
)
169+
return MQTTMessageSend(
170+
"/".join(
171+
(
172+
disco_prefix,
173+
stream_config.pop("component", "text"),
174+
mqtt_options.client_id,
175+
name,
176+
"config",
177+
)
178+
),
179+
json.dumps(stream_config).encode("utf8"),
180+
retain=True,
181+
)
182+

mqtt_io/modules/stream/serial.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
"required": False,
4848
"empty": False,
4949
"default": False
50+
},
51+
"ha_discovery": {
52+
"type": "dict",
53+
"allow_unknown": True
5054
}
5155
}
5256

mqtt_io/server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
hass_announce_digital_input,
5757
hass_announce_digital_output,
5858
hass_announce_sensor_input,
59+
hass_announce_stream
5960
)
6061
from .modules import install_missing_module_requirements
6162
from .modules.gpio import GenericGPIO, InterruptEdge, InterruptSupport, PinDirection
@@ -636,6 +637,12 @@ def _ha_discovery_announce(self) -> None:
636637
sens_conf, mqtt_config, self.mqtt_client_options
637638
)
638639
)
640+
for stream_conf in self.stream_configs.values():
641+
messages.append(
642+
hass_announce_stream(
643+
stream_conf, mqtt_config, self.mqtt_client_options
644+
)
645+
)
639646
for msg in messages:
640647
self.mqtt_task_queue.put_nowait(
641648
PriorityCoro(self._mqtt_publish(msg), MQTT_ANNOUNCE_PRIORITY)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy