Add time sync command (#951)

Allows setting the device time (on SMART devices) to the current time.
Fixes also setting the time which was previously broken.
This commit is contained in:
Teemu R
2024-06-17 10:37:08 +02:00
committed by GitHub
parent 6cdbbefb90
commit 867b7b8830
4 changed files with 75 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ import logging
import re
import sys
from contextlib import asynccontextmanager
from datetime import datetime
from functools import singledispatch, wraps
from pprint import pformat as pf
from typing import Any, cast
@@ -967,15 +968,43 @@ async def led(dev: Device, state):
return led.led
@cli.command()
@cli.group(invoke_without_command=True)
@click.pass_context
async def time(ctx: click.Context):
"""Get and set time."""
if ctx.invoked_subcommand is None:
await ctx.invoke(time_get)
@time.command(name="get")
@pass_dev
async def time(dev):
async def time_get(dev: Device):
"""Get the device time."""
res = dev.time
echo(f"Current time: {res}")
return res
@time.command(name="sync")
@pass_dev
async def time_sync(dev: SmartDevice):
"""Set the device time to current time."""
if not isinstance(dev, SmartDevice):
raise NotImplementedError("setting time currently only implemented on smart")
if (time := dev.modules.get(Module.Time)) is None:
echo("Device does not have time module")
return
echo("Old time: %s" % time.time)
local_tz = datetime.now().astimezone().tzinfo
await time.set_time(datetime.now(tz=local_tz))
await dev.update()
echo("New time: %s" % time.time)
@cli.command()
@click.option("--index", type=int, required=False)
@click.option("--name", type=str, required=False)