Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ async def trigger_init(self, trig_ctx, func_name):
}
kwarg_check = {
"event_trigger": {"kwargs": {dict}},
"mqtt_trigger": {"kwargs": {dict}},
"mqtt_trigger": {
"kwargs": {dict},
"encoding": {str},
},
"time_trigger": {"kwargs": {dict}},
"task_unique": {"kill_me": {bool, int}},
"time_active": {"hold_off": {int, float}},
Expand Down
4 changes: 2 additions & 2 deletions custom_components/pyscript/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ async def mqtt_message_handler(mqttmsg):
return mqtt_message_handler

@classmethod
async def notify_add(cls, topic, queue):
async def notify_add(cls, topic, queue, encoding=None):
"""Register to notify for mqtt messages of given topic to be sent to queue."""

if topic not in cls.notify:
cls.notify[topic] = set()
_LOGGER.debug("mqtt.notify_add(%s) -> adding mqtt subscription", topic)
cls.notify_remove[topic] = await mqtt.async_subscribe(
cls.hass, topic, cls.mqtt_message_handler_maker(topic), encoding="utf-8", qos=0
cls.hass, topic, cls.mqtt_message_handler_maker(topic), encoding=encoding or "utf-8", qos=0
)
cls.notify[topic].add(queue)

Expand Down
6 changes: 4 additions & 2 deletions custom_components/pyscript/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ async def wait_until(
time_trigger=None,
event_trigger=None,
mqtt_trigger=None,
mqtt_trigger_encoding=None,
webhook_trigger=None,
webhook_local_only=True,
webhook_methods=None,
Expand Down Expand Up @@ -359,7 +360,7 @@ async def wait_until(
if len(state_trig_ident) > 0:
State.notify_del(state_trig_ident, notify_q)
raise exc
await Mqtt.notify_add(mqtt_trigger[0], notify_q)
await Mqtt.notify_add(mqtt_trigger[0], notify_q, encoding=mqtt_trigger_encoding)
if webhook_trigger is not None:
if isinstance(webhook_trigger, str):
webhook_trigger = [webhook_trigger]
Expand Down Expand Up @@ -892,6 +893,7 @@ def __init__(
self.event_trigger_kwargs = trig_cfg.get("event_trigger", {}).get("kwargs", {})
self.mqtt_trigger = trig_cfg.get("mqtt_trigger", {}).get("args", None)
self.mqtt_trigger_kwargs = trig_cfg.get("mqtt_trigger", {}).get("kwargs", {})
self.mqtt_trigger_encoding = self.mqtt_trigger_kwargs.get("encoding", None)
self.webhook_trigger = trig_cfg.get("webhook_trigger", {}).get("args", None)
self.webhook_trigger_kwargs = trig_cfg.get("webhook_trigger", {}).get("kwargs", {})
self.webhook_local_only = self.webhook_trigger_kwargs.get("local_only", True)
Expand Down Expand Up @@ -1082,7 +1084,7 @@ async def trigger_watch(self):
Event.notify_add(self.event_trigger[0], self.notify_q)
if self.mqtt_trigger is not None:
_LOGGER.debug("trigger %s adding mqtt_trigger %s", self.name, self.mqtt_trigger[0])
await Mqtt.notify_add(self.mqtt_trigger[0], self.notify_q)
await Mqtt.notify_add(self.mqtt_trigger[0], self.notify_q, encoding=self.mqtt_trigger_encoding)
if self.webhook_trigger is not None:
_LOGGER.debug("trigger %s adding webhook_trigger %s", self.name, self.webhook_trigger[0])
Webhook.notify_add(
Expand Down
5 changes: 4 additions & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ more examples of built-in and user events and how to create triggers for them.

.. code:: python

@mqtt_trigger(topic, str_expr=None, kwargs=None)
@mqtt_trigger(topic, str_expr=None, encoding="utf-8", kwargs=None)

``@mqtt_trigger`` subscribes to the given MQTT ``topic`` and triggers whenever a message is received
on that topic. Multiple ``@mqtt_trigger`` decorators can be applied to a single function if you want
Expand All @@ -816,6 +816,9 @@ An optional ``str_expr`` can be used to match the MQTT message data, and the tri
if that expression evaluates to ``True`` or non-zero. This expression has available these
variables:

An optional ``encoding`` argument specifies the character encoding used to decode the MQTT payload
(default is **"utf-8"**). It can be explicitly set to other encodings if necessary.

- ``trigger_type`` is set to "mqtt".
- ``topic`` is set to the topic the message was received on.
- ``qos`` is set to the message QoS.
Expand Down
Loading