Skip to content

Commit 3632b51

Browse files
committed
Add encoding support for mqtt_trigger
1 parent e71c012 commit 3632b51

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

custom_components/pyscript/eval.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ async def trigger_init(self, trig_ctx, func_name):
390390
}
391391
kwarg_check = {
392392
"event_trigger": {"kwargs": {dict}},
393-
"mqtt_trigger": {"kwargs": {dict}},
393+
"mqtt_trigger": {
394+
"kwargs": {dict},
395+
"encoding": {str},
396+
},
394397
"time_trigger": {"kwargs": {dict}},
395398
"task_unique": {"kill_me": {bool, int}},
396399
"time_active": {"hold_off": {int, float}},

custom_components/pyscript/mqtt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ async def mqtt_message_handler(mqttmsg):
5858
return mqtt_message_handler
5959

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

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

custom_components/pyscript/trigger.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ async def wait_until(
223223
time_trigger=None,
224224
event_trigger=None,
225225
mqtt_trigger=None,
226+
mqtt_trigger_encoding=None,
226227
webhook_trigger=None,
227228
webhook_local_only=True,
228229
webhook_methods=None,
@@ -359,7 +360,7 @@ async def wait_until(
359360
if len(state_trig_ident) > 0:
360361
State.notify_del(state_trig_ident, notify_q)
361362
raise exc
362-
await Mqtt.notify_add(mqtt_trigger[0], notify_q)
363+
await Mqtt.notify_add(mqtt_trigger[0], notify_q, mqtt_trigger_encoding)
363364
if webhook_trigger is not None:
364365
if isinstance(webhook_trigger, str):
365366
webhook_trigger = [webhook_trigger]
@@ -892,6 +893,7 @@ def __init__(
892893
self.event_trigger_kwargs = trig_cfg.get("event_trigger", {}).get("kwargs", {})
893894
self.mqtt_trigger = trig_cfg.get("mqtt_trigger", {}).get("args", None)
894895
self.mqtt_trigger_kwargs = trig_cfg.get("mqtt_trigger", {}).get("kwargs", {})
896+
self.mqtt_trigger_encoding = self.mqtt_trigger_kwargs.get("encoding", None)
895897
self.webhook_trigger = trig_cfg.get("webhook_trigger", {}).get("args", None)
896898
self.webhook_trigger_kwargs = trig_cfg.get("webhook_trigger", {}).get("kwargs", {})
897899
self.webhook_local_only = self.webhook_trigger_kwargs.get("local_only", True)
@@ -1082,7 +1084,7 @@ async def trigger_watch(self):
10821084
Event.notify_add(self.event_trigger[0], self.notify_q)
10831085
if self.mqtt_trigger is not None:
10841086
_LOGGER.debug("trigger %s adding mqtt_trigger %s", self.name, self.mqtt_trigger[0])
1085-
await Mqtt.notify_add(self.mqtt_trigger[0], self.notify_q)
1087+
await Mqtt.notify_add(self.mqtt_trigger[0], self.notify_q, encoding=self.mqtt_trigger_encoding)
10861088
if self.webhook_trigger is not None:
10871089
_LOGGER.debug("trigger %s adding webhook_trigger %s", self.name, self.webhook_trigger[0])
10881090
Webhook.notify_add(

docs/reference.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ more examples of built-in and user events and how to create triggers for them.
806806

807807
.. code:: python
808808
809-
@mqtt_trigger(topic, str_expr=None, kwargs=None)
809+
@mqtt_trigger(topic, str_expr=None, encoding="utf-8", kwargs=None)
810810
811811
``@mqtt_trigger`` subscribes to the given MQTT ``topic`` and triggers whenever a message is received
812812
on that topic. Multiple ``@mqtt_trigger`` decorators can be applied to a single function if you want
@@ -816,6 +816,9 @@ An optional ``str_expr`` can be used to match the MQTT message data, and the tri
816816
if that expression evaluates to ``True`` or non-zero. This expression has available these
817817
variables:
818818

819+
An optional ``encoding`` argument specifies the character encoding used to decode the MQTT payload
820+
(default is **"utf-8"**). It can be explicitly set to other encodings if necessary.
821+
819822
- ``trigger_type`` is set to "mqtt".
820823
- ``topic`` is set to the topic the message was received on.
821824
- ``qos`` is set to the message QoS.

0 commit comments

Comments
 (0)