# 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_serial.py
# class AsyncSerialGateway(BaseAsyncGateway, BaseSerialGateway):
#     """MySensors async serial gateway."""
# 
#     def __init__(self, *args, loop=None, **kwargs):
#         """Set up serial gateway."""
#         transport = AsyncTransport(self, async_connect, loop=loop, **kwargs)
#         super().__init__(transport, *args, loop=loop, **kwargs)
# 
#     async def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         serial_number = await self.tasks.loop.run_in_executor(
#             None, self._get_gateway_id
#         )
#         return serial_number
# 
# class SerialGateway(BaseSyncGateway, BaseSerialGateway):
#     """MySensors serial gateway."""
# 
#     def __init__(self, *args, **kwargs):
#         """Set up serial gateway."""
#         transport = SyncTransport(self, sync_connect, **kwargs)
#         super().__init__(transport, *args, **kwargs)
# 
#     def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()


import click
from mysensors.cli.helper import (
    common_gateway_options,
    handle_msg,
    run_async_gateway,
    run_gateway,
)
from mysensors.gateway_serial import AsyncSerialGateway, SerialGateway



def common_serial_options(func):
    """Supply common serial gateway options."""
    func = click.option(
        "-b",
        "--baud",
        default=115200,
        show_default=True,
        type=int,
        help="Baud rate of the serial connection.",
    )(func)
    func = click.option(
        "-p", "--port", required=True, help="Serial port of the gateway."
    )(func)
    return func


@click.command(options_metavar="<options>")
@common_serial_options
@common_gateway_options
def serial_gateway(**kwargs):
    """Start a serial gateway."""
    gateway = SerialGateway(event_callback=handle_msg, **kwargs)
    run_gateway(gateway)


@click.command(options_metavar="<options>")
@common_serial_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_serial.py
# class AsyncSerialGateway(BaseAsyncGateway, BaseSerialGateway):
#     """MySensors async serial gateway."""
# 
#     def __init__(self, *args, loop=None, **kwargs):
#         """Set up serial gateway."""
#         transport = AsyncTransport(self, async_connect, loop=loop, **kwargs)
#         super().__init__(transport, *args, loop=loop, **kwargs)
# 
#     async def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         serial_number = await self.tasks.loop.run_in_executor(
#             None, self._get_gateway_id
#         )
#         return serial_number
# 
# class SerialGateway(BaseSyncGateway, BaseSerialGateway):
#     """MySensors serial gateway."""
# 
#     def __init__(self, *args, **kwargs):
#         """Set up serial gateway."""
#         transport = SyncTransport(self, sync_connect, **kwargs)
#         super().__init__(transport, *args, **kwargs)
# 
#     def get_gateway_id(self):
#         """Return a unique id for the gateway."""
#         return self._get_gateway_id()


# Path: mysensors/cli/gateway_serial.py
import click
from mysensors.cli.helper import (
    common_gateway_options,
    handle_msg,
    run_async_gateway,
    run_gateway,
)
from mysensors.gateway_serial import AsyncSerialGateway, SerialGateway




def common_serial_options(func):
    """Supply common serial gateway options."""
    func = click.option(
        "-b",
        "--baud",
        default=115200,
        show_default=True,
        type=int,
        help="Baud rate of the serial connection.",
    )(func)
    func = click.option(
        "-p", "--port", required=True, help="Serial port of the gateway."
    )(func)
    return func


@click.command(options_metavar="<options>")
@common_serial_options
@common_gateway_options
def serial_gateway(**kwargs):
    """Start a serial gateway."""
    gateway = SerialGateway(event_callback=handle_msg, **kwargs)
    run_gateway(gateway)


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

@common_gateway_options