# Path: mysensors/cli/helper.py
# def common_gateway_options(func):
#     """Supply common gateway options."""
#     func = click.option(
#         "-v",
#         "--protocol_version",
#         help="Protocol version of the gateway.",
#         default="2.2",
#         show_default=True,
#     )(func)
#     func = click.option(
#         "-s", "--persistence", help="Turn on persistence.", is_flag=True
#     )(func)
#     return func
# 
# def handle_msg(msg):
#     """Handle mysensors updates."""
#     _LOGGER.info("Received message: %s", msg.encode().strip())
# 
# def run_async_gateway(gateway, stop_task=None):
#     """Run an async gateway."""
#     try:
#         gateway.tasks.loop.run_until_complete(gateway.start_persistence())
#         gateway.tasks.loop.run_until_complete(gateway.start())
#         gateway.tasks.loop.run_forever()
#     except KeyboardInterrupt:
#         gateway.tasks.loop.run_until_complete(gateway.stop())
#         if stop_task:
#             gateway.tasks.loop.run_until_complete(stop_task)
#         gateway.tasks.loop.close()
# 
# def run_gateway(gateway):
#     """Run a sync gateway."""
#     gateway.start_persistence()
#     gateway.start()
#     try:
#         while True:
#             time.sleep(0.5)
#     except KeyboardInterrupt:
#         gateway.stop()
# 
# Path: mysensors/gateway_mqtt.py
# class AsyncMQTTGateway(BaseAsyncGateway, BaseMQTTGateway):
#     """MySensors async MQTT client gateway."""
# 
#     # pylint: disable=too-many-arguments
# 
#     def __init__(
#         self,
#         pub_callback,
#         sub_callback,
#         loop=None,
#         in_prefix="",
#         out_prefix="",
#         retain=True,
#         **kwargs,
#     ):
#         """Set up MQTT gateway."""
#         transport = MQTTAsyncTransport(
#             self,
#             pub_callback,
#             sub_callback,
#             in_prefix=in_prefix,
#             out_prefix=out_prefix,
#             retain=retain,
#         )
#         super().__init__(transport, loop=loop, **kwargs)
# 
#     async def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()
# 
# class MQTTGateway(BaseSyncGateway, BaseMQTTGateway):
#     """MySensors MQTT client gateway."""
# 
#     # pylint: disable=too-many-arguments
# 
#     def __init__(
#         self,
#         pub_callback,
#         sub_callback,
#         in_prefix="",
#         out_prefix="",
#         retain=True,
#         **kwargs,
#     ):
#         """Set up MQTT gateway."""
#         transport = MQTTSyncTransport(
#             self,
#             pub_callback,
#             sub_callback,
#             in_prefix=in_prefix,
#             out_prefix=out_prefix,
#             retain=retain,
#         )
#         super().__init__(transport, **kwargs)
# 
#     def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()


import asyncio
import logging
import socket
import sys
import click
            import paho.mqtt.client as mqtt
        import paho.mqtt.client as mqtt
from contextlib import contextmanager
from mysensors.cli.helper import (
    common_gateway_options,
    handle_msg,
    run_async_gateway,
    run_gateway,
)
from mysensors.gateway_mqtt import AsyncMQTTGateway, MQTTGateway
        help="Topic prefix for incoming messages.",
    )(func)
    func = click.option(
        "-p",
        "--port",
        default=1883,
        show_default=True,
        type=int,
        help="MQTT port of the connection.",
    )(func)
    func = click.option("-b", "--broker", required=True, help="MQTT broker address.")(
        func
    )
    return func


@click.command(options_metavar="<options>")
@common_mqtt_options
@common_gateway_options
def mqtt_gateway(broker, port, **kwargs):
    """Start an mqtt gateway."""
    with run_mqtt_client(broker, port) as mqttc:
        gateway = MQTTGateway(
            mqttc.publish, mqttc.subscribe, event_callback=handle_msg, **kwargs
        )
        run_gateway(gateway)


@click.command(options_metavar="<options>")
@common_mqtt_options

# Path: mysensors/cli/helper.py
# def common_gateway_options(func):
#     """Supply common gateway options."""
#     func = click.option(
#         "-v",
#         "--protocol_version",
#         help="Protocol version of the gateway.",
#         default="2.2",
#         show_default=True,
#     )(func)
#     func = click.option(
#         "-s", "--persistence", help="Turn on persistence.", is_flag=True
#     )(func)
#     return func
# 
# def handle_msg(msg):
#     """Handle mysensors updates."""
#     _LOGGER.info("Received message: %s", msg.encode().strip())
# 
# def run_async_gateway(gateway, stop_task=None):
#     """Run an async gateway."""
#     try:
#         gateway.tasks.loop.run_until_complete(gateway.start_persistence())
#         gateway.tasks.loop.run_until_complete(gateway.start())
#         gateway.tasks.loop.run_forever()
#     except KeyboardInterrupt:
#         gateway.tasks.loop.run_until_complete(gateway.stop())
#         if stop_task:
#             gateway.tasks.loop.run_until_complete(stop_task)
#         gateway.tasks.loop.close()
# 
# def run_gateway(gateway):
#     """Run a sync gateway."""
#     gateway.start_persistence()
#     gateway.start()
#     try:
#         while True:
#             time.sleep(0.5)
#     except KeyboardInterrupt:
#         gateway.stop()
# 
# Path: mysensors/gateway_mqtt.py
# class AsyncMQTTGateway(BaseAsyncGateway, BaseMQTTGateway):
#     """MySensors async MQTT client gateway."""
# 
#     # pylint: disable=too-many-arguments
# 
#     def __init__(
#         self,
#         pub_callback,
#         sub_callback,
#         loop=None,
#         in_prefix="",
#         out_prefix="",
#         retain=True,
#         **kwargs,
#     ):
#         """Set up MQTT gateway."""
#         transport = MQTTAsyncTransport(
#             self,
#             pub_callback,
#             sub_callback,
#             in_prefix=in_prefix,
#             out_prefix=out_prefix,
#             retain=retain,
#         )
#         super().__init__(transport, loop=loop, **kwargs)
# 
#     async def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()
# 
# class MQTTGateway(BaseSyncGateway, BaseMQTTGateway):
#     """MySensors MQTT client gateway."""
# 
#     # pylint: disable=too-many-arguments
# 
#     def __init__(
#         self,
#         pub_callback,
#         sub_callback,
#         in_prefix="",
#         out_prefix="",
#         retain=True,
#         **kwargs,
#     ):
#         """Set up MQTT gateway."""
#         transport = MQTTSyncTransport(
#             self,
#             pub_callback,
#             sub_callback,
#             in_prefix=in_prefix,
#             out_prefix=out_prefix,
#             retain=retain,
#         )
#         super().__init__(transport, **kwargs)
# 
#     def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()


# Path: mysensors/cli/gateway_mqtt.py
import asyncio
import logging
import socket
import sys
import click
            import paho.mqtt.client as mqtt
        import paho.mqtt.client as mqtt
from contextlib import contextmanager
from mysensors.cli.helper import (
    common_gateway_options,
    handle_msg,
    run_async_gateway,
    run_gateway,
)
from mysensors.gateway_mqtt import AsyncMQTTGateway, MQTTGateway

        help="Topic prefix for incoming messages.",
    )(func)
    func = click.option(
        "-p",
        "--port",
        default=1883,
        show_default=True,
        type=int,
        help="MQTT port of the connection.",
    )(func)
    func = click.option("-b", "--broker", required=True, help="MQTT broker address.")(
        func
    )
    return func


@click.command(options_metavar="<options>")
@common_mqtt_options
@common_gateway_options
def mqtt_gateway(broker, port, **kwargs):
    """Start an mqtt gateway."""
    with run_mqtt_client(broker, port) as mqttc:
        gateway = MQTTGateway(
            mqttc.publish, mqttc.subscribe, event_callback=handle_msg, **kwargs
        )
        run_gateway(gateway)


@click.command(options_metavar="<options>")
@common_mqtt_options

@common_gateway_options