mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-04-02 00:07:16 +01:00
The /os/info API endpoint has been using D-Bus property TimeUSec which got cached between requests, so the time returned was not always the same as current time on the host system at the time of the request. Since there's no reason to use D-Bus API for the time, as Supervisor runs on the same machine and time is global, simply format current datetime object with Python and return it in the response. Fixes #6581
106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
"""Test TimeDate dbus interface."""
|
|
|
|
# pylint: disable=import-error
|
|
from datetime import UTC, datetime
|
|
|
|
from dbus_fast.aio.message_bus import MessageBus
|
|
import pytest
|
|
|
|
from supervisor.dbus.timedate import TimeDate
|
|
from supervisor.exceptions import DBusNotConnectedError
|
|
|
|
from tests.common import mock_dbus_services
|
|
from tests.dbus_service_mocks.timedate import TimeDate as TimeDateService
|
|
|
|
|
|
@pytest.fixture(name="timedate_service")
|
|
async def fixture_timedate_service(dbus_session_bus: MessageBus) -> TimeDateService:
|
|
"""Mock timedate dbus service."""
|
|
yield (await mock_dbus_services({"timedate": None}, dbus_session_bus))["timedate"]
|
|
|
|
|
|
async def test_timedate_info(
|
|
timedate_service: TimeDateService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test timedate properties."""
|
|
timedate = TimeDate()
|
|
|
|
assert timedate.ntp is None
|
|
|
|
await timedate.connect(dbus_session_bus)
|
|
|
|
assert timedate.ntp is True
|
|
|
|
timedate_service.emit_properties_changed({"NTP": False})
|
|
await timedate_service.ping()
|
|
assert timedate.ntp is False
|
|
|
|
timedate_service.emit_properties_changed({}, ["NTP"])
|
|
await timedate_service.ping()
|
|
await timedate_service.ping() # To process the follow-up get all properties call
|
|
assert timedate.ntp is True
|
|
|
|
|
|
async def test_dbus_settime(
|
|
timedate_service: TimeDateService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Set timestamp on backend."""
|
|
timedate_service.SetTime.calls.clear()
|
|
timedate = TimeDate()
|
|
|
|
test_dt = datetime(2021, 5, 19, 8, 36, 54, 405718, tzinfo=UTC)
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await timedate.set_time(test_dt)
|
|
|
|
await timedate.connect(dbus_session_bus)
|
|
|
|
assert await timedate.set_time(test_dt) is None
|
|
assert timedate_service.SetTime.calls == [(1621413414405718, False, False)]
|
|
|
|
|
|
async def test_dbus_setntp(
|
|
timedate_service: TimeDateService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Disable NTP on backend."""
|
|
timedate_service.SetNTP.calls.clear()
|
|
timedate = TimeDate()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await timedate.set_ntp(False)
|
|
|
|
await timedate.connect(dbus_session_bus)
|
|
|
|
assert timedate.ntp is True
|
|
assert await timedate.set_ntp(False) is None
|
|
assert timedate_service.SetNTP.calls == [(False, False)]
|
|
await timedate_service.ping()
|
|
assert timedate.ntp is False
|
|
|
|
|
|
async def test_dbus_set_timezone(
|
|
timedate_service: TimeDateService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test setting of host timezone."""
|
|
timedate_service.SetTimezone.calls.clear()
|
|
timedate = TimeDate()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await timedate.set_timezone("Europe/Prague")
|
|
|
|
await timedate.connect(dbus_session_bus)
|
|
|
|
assert await timedate.set_timezone("Europe/Prague") is None
|
|
assert timedate_service.SetTimezone.calls == [("Europe/Prague", False)]
|
|
await timedate_service.ping()
|
|
assert timedate.timezone == "Europe/Prague"
|
|
|
|
|
|
async def test_dbus_timedate_connect_error(
|
|
dbus_session_bus: MessageBus, caplog: pytest.LogCaptureFixture
|
|
):
|
|
"""Test connecting to timedate error."""
|
|
timedate = TimeDate()
|
|
await timedate.connect(dbus_session_bus)
|
|
assert "No timedate support on the host" in caplog.text
|