From 3632b5117f190f4fa7f102e48cefcb73c5d12423 Mon Sep 17 00:00:00 2001 From: 3735943886 <9522390+3735943886@users.noreply.github.com> Date: Mon, 8 Dec 2025 10:23:19 +0900 Subject: [PATCH 1/2] Add encoding support for mqtt_trigger --- custom_components/pyscript/eval.py | 5 ++++- custom_components/pyscript/mqtt.py | 4 ++-- custom_components/pyscript/trigger.py | 6 ++++-- docs/reference.rst | 5 ++++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index a20106e..c205dc0 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -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}}, diff --git a/custom_components/pyscript/mqtt.py b/custom_components/pyscript/mqtt.py index 8e15b3d..8ff413c 100644 --- a/custom_components/pyscript/mqtt.py +++ b/custom_components/pyscript/mqtt.py @@ -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) diff --git a/custom_components/pyscript/trigger.py b/custom_components/pyscript/trigger.py index f8ffe09..5378f70 100644 --- a/custom_components/pyscript/trigger.py +++ b/custom_components/pyscript/trigger.py @@ -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, @@ -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, mqtt_trigger_encoding) if webhook_trigger is not None: if isinstance(webhook_trigger, str): webhook_trigger = [webhook_trigger] @@ -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) @@ -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( diff --git a/docs/reference.rst b/docs/reference.rst index 005ba84..feb2868 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -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 @@ -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. From 7edbfa29a5f4f447abb8d29f4a42dd8fc264ac90 Mon Sep 17 00:00:00 2001 From: 3735943886 <9522390+3735943886@users.noreply.github.com> Date: Mon, 8 Dec 2025 11:10:42 +0900 Subject: [PATCH 2/2] Changes encoding to kwarg in Mqtt.notify_add call --- custom_components/pyscript/trigger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/pyscript/trigger.py b/custom_components/pyscript/trigger.py index 5378f70..9fb45ac 100644 --- a/custom_components/pyscript/trigger.py +++ b/custom_components/pyscript/trigger.py @@ -360,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, mqtt_trigger_encoding) + 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]