Skip to content

Commit

Permalink
Add ha_discovery support for serial stream
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkDvlp committed Jul 28, 2023
1 parent b31bd51 commit 37efced
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
34 changes: 33 additions & 1 deletion mqtt_io/home_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
from typing import Any, Dict

from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, SET_SUFFIX
from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, STREAM_TOPIC, SET_SUFFIX, SEND_SUFFIX
from .mqtt import MQTTClientOptions, MQTTMessageSend
from .types import ConfigType
from . import VERSION
Expand Down Expand Up @@ -148,3 +148,35 @@ def hass_announce_sensor_input(
json.dumps(sensor_config).encode("utf8"),
retain=True,
)

def hass_announce_stream(
in_conf: ConfigType, mqtt_conf: ConfigType, mqtt_options: MQTTClientOptions
) -> MQTTMessageSend:
"""
Create a message which announces stream as text to Home Assistant.
"""
disco_conf: ConfigType = mqtt_conf["ha_discovery"]
name: str = in_conf["name"]
disco_prefix: str = disco_conf["prefix"]
stream_config = get_common_config(in_conf, mqtt_conf, mqtt_options)
stream_config.update(
dict(
unique_id=f"{mqtt_options.client_id}_{in_conf['module']}_input_{name}",
state_topic="/".join((mqtt_conf["topic_prefix"], STREAM_TOPIC, name)),
command_topic="/".join((mqtt_conf["topic_prefix"], STREAM_TOPIC, name, SEND_SUFFIX))
)
)
return MQTTMessageSend(
"/".join(
(
disco_prefix,
stream_config.pop("component", "text"),
mqtt_options.client_id,
name,
"config",
)
),
json.dumps(stream_config).encode("utf8"),
retain=True,
)

4 changes: 4 additions & 0 deletions mqtt_io/modules/stream/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"required": False,
"empty": False,
"default": False
},
"ha_discovery": {
"type": "dict",
"allow_unknown": True
}
}

Expand Down
7 changes: 7 additions & 0 deletions mqtt_io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
hass_announce_digital_input,
hass_announce_digital_output,
hass_announce_sensor_input,
hass_announce_stream
)
from .modules import install_missing_module_requirements
from .modules.gpio import GenericGPIO, InterruptEdge, InterruptSupport, PinDirection
Expand Down Expand Up @@ -636,6 +637,12 @@ def _ha_discovery_announce(self) -> None:
sens_conf, mqtt_config, self.mqtt_client_options
)
)
for stream_conf in self.stream_configs.values():
messages.append(
hass_announce_stream(
stream_conf, mqtt_config, self.mqtt_client_options
)
)
for msg in messages:
self.mqtt_task_queue.put_nowait(
PriorityCoro(self._mqtt_publish(msg), MQTT_ANNOUNCE_PRIORITY)
Expand Down

0 comments on commit 37efced

Please sign in to comment.