Skip to content

cli

cli

CLI Interface.

Classes

EmberMugCli

Very simple CLI Interface to interact with a mug.

Source code in ember_mug/cli/commands.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
class EmberMugCli:
    """Very simple CLI Interface to interact with a mug."""

    _commands: ClassVar[dict[str, Callable[[Namespace], Awaitable]]] = {
        "find": find_device_cmd,
        "discover": discover_cmd,
        "info": fetch_info_cmd,
        "poll": poll_device_cmd,
        "get": get_device_value_cmd,
        "set": set_device_value_cmd,
    }

    def __init__(self) -> None:
        """Create parsers."""
        self.parser = ArgumentParser(prog="ember-mug", description="CLI to interact with an Ember Mug")
        shared_parser = ArgumentParser(add_help=False)
        shared_parser.add_argument(
            "-m",
            "--mac",
            action="store",
            type=validate_mac,
            help="Only look for this specific address",
        )
        shared_parser.add_argument(
            "-d",
            "--debug",
            action="store_true",
            help="Print extra information for development or debugging issues",
        )
        shared_parser.add_argument(
            "--log-file",
            type=FileType("w", encoding="utf-8"),
            nargs="?",
            default=sys.stdout,
            help="File to write logs too (Will be overwritten)",
        )
        shared_parser.add_argument("-r", "--raw", help="No formatting. One value per line.", action="store_true")
        if IS_LINUX is True:
            # Only works on Linux with BlueZ so don't add for others.
            shared_parser.add_argument(
                "-a",
                "--adapter",
                action="store",
                help="Use this Bluetooth adapter instead of the default one (for Bluez)",
            )
        subparsers = self.parser.add_subparsers(dest="command", required=True)
        subparsers.add_parser("find", description="Find the first paired device", parents=[shared_parser])
        subparsers.add_parser("discover", description="Discover devices in pairing mode", parents=[shared_parser])
        info_parsers = ArgumentParser(add_help=False)
        info_parsers.add_argument("-e", "--extra", help="Show extra info", action="store_true")
        info_parsers.add_argument("--imperial", help="Use Imperial units", action="store_true")
        subparsers.add_parser("info", description="Fetch all info from device", parents=[shared_parser, info_parsers])
        subparsers.add_parser("poll", description="Poll device for information", parents=[shared_parser, info_parsers])
        get_parser = subparsers.add_parser("get", description="Get mug value", parents=[shared_parser, info_parsers])
        get_parser.add_argument(dest="attributes", metavar="ATTRIBUTE", choices=get_attribute_names, nargs="+")
        set_parser = subparsers.add_parser("set", description="Set mug value", parents=[shared_parser, info_parsers])
        set_parser.add_argument("--name", help="Name", required=False)
        set_parser.add_argument("--target-temp", help="Target Temperature", type=float, required=False)
        set_parser.add_argument("--temperature-unit", help="Temperature Unit", choices=["C", "F"], required=False)
        set_parser.add_argument("--led-colour", help="LED Colour", type=colour_type, required=False)
        set_parser.add_argument(
            "--volume-level",
            help="Volume Level",
            choices=[v.value for v in VolumeLevel],
            required=False,
        )

    async def run(self) -> None:
        """Run the specified command based on subparser."""
        args = self.parser.parse_args()
        if IS_LINUX is False:
            args.adapter = None  # Set for other platforms
        if args.debug:
            logging.basicConfig(
                stream=args.log_file,
                level=logging.DEBUG,
                format="[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s",
            )
        await self._commands[args.command](args)
Attributes
parser instance-attribute
parser = ArgumentParser(
    prog="ember-mug",
    description="CLI to interact with an Ember Mug",
)
Functions
run async
run() -> None

Run the specified command based on subparser.

Source code in ember_mug/cli/commands.py
246
247
248
249
250
251
252
253
254
255
256
257
async def run(self) -> None:
    """Run the specified command based on subparser."""
    args = self.parser.parse_args()
    if IS_LINUX is False:
        args.adapter = None  # Set for other platforms
    if args.debug:
        logging.basicConfig(
            stream=args.log_file,
            level=logging.DEBUG,
            format="[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s",
        )
    await self._commands[args.command](args)

Functions

run_cli

run_cli() -> None

Run the command line interface.

Source code in ember_mug/cli/__init__.py
 9
10
11
12
13
14
15
def run_cli() -> None:
    """Run the command line interface."""
    cli = EmberMugCli()
    try:
        asyncio.run(cli.run())
    except KeyboardInterrupt:
        print("Exiting.")