1+ # Copyright (C) 2025 Drew Green (@agreenbhm)
2+ #
3+ # This program is free software: you can redistribute it and/or modify
4+ # it under the terms of the GNU General Public License as published by
5+ # the Free Software Foundation, either version 3 of the License, or
6+ # (at your option) any later version.
7+ #
8+ # This program is distributed in the hope that it will be useful,
9+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ # GNU General Public License for more details.
12+ #
13+ # You should have received a copy of the GNU General Public License
14+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+ import esphome .codegen as cg
17+ import esphome .config_validation as cv
18+ from esphome .automation import Condition
19+ from esphome .const import CONF_ID
20+ from esphome .core import CORE , coroutine_with_priority
21+ from esphome import automation
22+ from esphome .const import (
23+ CONF_TOPIC ,
24+ CONF_PAYLOAD ,
25+ CONF_QOS ,
26+ CONF_RETAIN
27+ )
28+
29+ rp2040_mqtt_ns = cg .esphome_ns .namespace ("rp2040_mqtt" )
30+ RP2040MQTTComponent = rp2040_mqtt_ns .class_ ("RP2040MQTT" , cg .Component )
31+ RP2040MQTTConnectedCondition = rp2040_mqtt_ns .class_ ("RP2040MQTTConnectedCondition" , Condition )
32+ RP2040MQTTEnableAction = rp2040_mqtt_ns .class_ ("RP2040MQTTEnableAction" , automation .Action )
33+ RP2040MQTTDisableAction = rp2040_mqtt_ns .class_ ("RP2040MQTTDisableAction" , automation .Action )
34+ RP2040MQTTPublishAction = rp2040_mqtt_ns .class_ ("RP2040MQTTPublishAction" , automation .Action )
35+
36+ MULTI_CONF = True
37+
38+ CONF_SECURE = "secure"
39+ CONF_MQTT_HOST = "mqtt_host"
40+ CONF_MQTT_PORT = "mqtt_port"
41+ CONF_AUTH = "auth"
42+ CONF_MQTT_USER = "mqtt_user"
43+ CONF_MQTT_PASS = "mqtt_pass"
44+
45+
46+ CONFIG_SCHEMA = (
47+ cv .Schema (
48+ {
49+ cv .GenerateID (): cv .declare_id (RP2040MQTTComponent ),
50+ cv .Optional (CONF_MQTT_HOST ): cv .string ,
51+ cv .Optional (CONF_MQTT_PORT ): cv .port ,
52+ cv .Optional (CONF_SECURE ): cv .boolean ,
53+ cv .Optional (CONF_AUTH ): cv .boolean ,
54+ cv .Optional (CONF_MQTT_USER ): cv .string ,
55+ cv .Optional (CONF_MQTT_PASS ): cv .string
56+
57+ }
58+ )
59+ .extend (cv .COMPONENT_SCHEMA )
60+ )
61+
62+ @automation .register_action (
63+ "rp2040_mqtt.enable" ,
64+ RP2040MQTTEnableAction ,
65+ cv .Schema (
66+ {
67+ cv .GenerateID (): cv .use_id (RP2040MQTTComponent ),
68+ }
69+ ),
70+ )
71+
72+ async def rp2040_mqtt_enable_to_code (config , action_id , template_arg , args ):
73+ paren = await cg .get_variable (config [CONF_ID ])
74+ return cg .new_Pvariable (action_id , template_arg , paren )
75+
76+ @automation .register_action (
77+ "rp2040_mqtt.disable" ,
78+ RP2040MQTTDisableAction ,
79+ cv .Schema (
80+ {
81+ cv .GenerateID (): cv .use_id (RP2040MQTTComponent ),
82+ }
83+ ),
84+ )
85+
86+ async def rp2040_mqtt_disable_to_code (config , action_id , template_arg , args ):
87+ paren = await cg .get_variable (config [CONF_ID ])
88+ return cg .new_Pvariable (action_id , template_arg , paren )
89+
90+ MQTT_PUBLISH_ACTION_SCHEMA = cv .Schema (
91+ {
92+ cv .GenerateID (): cv .use_id (RP2040MQTTComponent ),
93+ cv .Required (CONF_TOPIC ): cv .templatable (cv .publish_topic ),
94+ cv .Required (CONF_PAYLOAD ): cv .templatable (cv .mqtt_payload ),
95+ cv .Optional (CONF_QOS , default = 0 ): cv .templatable (cv .mqtt_qos ),
96+ cv .Optional (CONF_RETAIN , default = False ): cv .templatable (cv .boolean ),
97+ }
98+ )
99+
100+
101+ @automation .register_action (
102+ "rp2040_mqtt.publish" , RP2040MQTTPublishAction , MQTT_PUBLISH_ACTION_SCHEMA
103+ )
104+ async def rp2040_mqtt_publish_action_to_code (config , action_id , template_arg , args ):
105+ paren = await cg .get_variable (config [CONF_ID ])
106+ var = cg .new_Pvariable (action_id , template_arg , paren )
107+ template_ = await cg .templatable (config [CONF_TOPIC ], args , cg .std_string )
108+ cg .add (var .set_topic (template_ ))
109+
110+ template_ = await cg .templatable (config [CONF_PAYLOAD ], args , cg .std_string )
111+ cg .add (var .set_payload (template_ ))
112+ template_ = await cg .templatable (config [CONF_QOS ], args , cg .uint8 )
113+ cg .add (var .set_qos (template_ ))
114+ template_ = await cg .templatable (config [CONF_RETAIN ], args , bool )
115+ cg .add (var .set_retain (template_ ))
116+ return var
117+
118+
119+
120+ @automation .register_condition (
121+ "rp2040_mqtt.connected" ,
122+ RP2040MQTTConnectedCondition ,
123+ cv .Schema (
124+ {
125+ cv .GenerateID (): cv .use_id (RP2040MQTTComponent ),
126+ }
127+ ),
128+ )
129+ async def rp2040_mqtt_connected_to_code (config , condition_id , template_arg , args ):
130+ paren = await cg .get_variable (config [CONF_ID ])
131+ return cg .new_Pvariable (condition_id , template_arg , paren )
132+
133+ def to_code (config ):
134+ var = cg .new_Pvariable (config [CONF_ID ])
135+ if CONF_MQTT_HOST in config :
136+ cg .add (var .set_mqtt_host (config [CONF_MQTT_HOST ]))
137+ if CONF_MQTT_PORT in config :
138+ cg .add (var .set_mqtt_port (config [CONF_MQTT_PORT ]))
139+ if CONF_SECURE in config :
140+ cg .add (var .set_secure (config [CONF_SECURE ]))
141+ if CONF_AUTH in config :
142+ cg .add (var .set_authenticate (config [CONF_AUTH ]))
143+ if CONF_MQTT_USER in config :
144+ cg .add (var .set_mqtt_user (config [CONF_MQTT_USER ]))
145+ if CONF_MQTT_PASS in config :
146+ cg .add (var .set_mqtt_password (config [CONF_MQTT_PASS ]))
147+ # if CORE.is_rp2040:
148+ # cg.add_library("mobizt/ESP_SSLClient", None)
149+ cg .add_library ("mobizt/ESP_SSLClient" , None )
150+ cg .add_library ("arduino-libraries/ArduinoMqttClient" , None )
151+
152+
153+ yield cg .register_component (var , config )
0 commit comments