mqtt: implement a "publish_mqtt_topic" remote method

Allow Klipper to publish MQTT topics from gcode macros.

Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-12-22 18:02:26 -05:00
parent 57b00cb33a
commit 92954245fa
1 changed files with 20 additions and 3 deletions

View File

@ -222,9 +222,15 @@ class MQTTClient(APITransport, Subscribable):
self.subscribe_topic(self.api_request_topic, self.subscribe_topic(self.api_request_topic,
self._process_api_request, self._process_api_request,
self.api_qos) self.api_qos)
logging.info(
f"Moonraker API topics - Request: {self.api_request_topic}, " self.server.register_remote_method("publish_mqtt_topic",
f"Response: {self.api_resp_topic}") self._publish_from_klipper)
logging.info(
f"\nReserved MQTT topics:\n"
f"API Request: {self.api_request_topic}\n"
f"API Response: {self.api_resp_topic}\n"
f"Moonraker Status: {self.moonraker_status_topic}\n"
f"Klipper Status: {self.klipper_status_topic}")
async def component_init(self) -> None: async def component_init(self) -> None:
# We must wait for the IOLoop (asyncio event loop) to start # We must wait for the IOLoop (asyncio event loop) to start
@ -645,6 +651,17 @@ class MQTTClient(APITransport, Subscribable):
fut.set_exception( fut.set_exception(
self.server.error("Moonraker Shutdown", 503)) self.server.error("Moonraker Shutdown", 503))
async def _publish_from_klipper(self,
topic: str,
payload: Any = None,
qos: Optional[int] = None,
retain: bool = False,
use_prefix: bool = False
) -> None:
if use_prefix:
topic = f"{self.instance_name}/{topic.lstrip('/')}"
await self.publish_topic(topic, payload, qos, retain)
def load_component(config: ConfigHelper) -> MQTTClient: def load_component(config: ConfigHelper) -> MQTTClient:
return MQTTClient(config) return MQTTClient(config)