Keep flux_led device time in sync (#63259)

pull/63389/head
J. Nick Koston 2022-01-04 06:37:15 -10:00 committed by GitHub
parent f71d49230d
commit 13b262bcce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -16,7 +16,10 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.event import (
async_track_time_change,
async_track_time_interval,
)
from homeassistant.helpers.typing import ConfigType
from .const import (
@ -123,6 +126,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
platforms = PLATFORMS_BY_TYPE[device.device_type]
hass.config_entries.async_setup_platforms(entry, platforms)
async def _async_sync_time(*args: Any) -> None:
"""Set the time every morning at 02:40:30."""
await device.async_set_time()
await _async_sync_time() # set at startup
entry.async_on_unload(async_track_time_change(hass, _async_sync_time, 2, 4, 30)) # type: ignore[arg-type]
return True

View File

@ -73,6 +73,7 @@ def _mocked_bulb() -> AIOWifiLedBulb:
bulb.requires_turn_on = True
bulb.async_setup = AsyncMock(side_effect=_save_setup_callback)
bulb.effect_list = ["some_effect"]
bulb.async_set_time = AsyncMock()
bulb.async_set_music_mode = AsyncMock()
bulb.async_set_custom_pattern = AsyncMock()
bulb.async_set_preset_pattern = AsyncMock()
@ -131,6 +132,7 @@ def _mocked_switch() -> AIOWifiLedBulb:
channel4=PowerRestoreState.LAST_STATE,
)
switch.requires_turn_on = True
switch.async_set_time = AsyncMock()
switch.async_reboot = AsyncMock()
switch.async_setup = AsyncMock(side_effect=_save_setup_callback)
switch.async_set_power_restore = AsyncMock()

View File

@ -1,6 +1,7 @@
"""Tests for the flux_led component."""
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
@ -19,6 +20,7 @@ from . import (
FLUX_DISCOVERY_PARTIAL,
IP_ADDRESS,
MAC_ADDRESS,
_mocked_bulb,
_patch_discovery,
_patch_wifibulb,
)
@ -138,3 +140,19 @@ async def test_config_entry_fills_unique_id_with_directed_discovery(
assert config_entry.unique_id == MAC_ADDRESS
assert config_entry.data[CONF_NAME] == title
assert config_entry.title == title
async def test_time_sync_startup_and_next_day(hass: HomeAssistant) -> None:
"""Test that time is synced on startup and next day."""
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=MAC_ADDRESS)
config_entry.add_to_hass(hass)
bulb = _mocked_bulb()
with _patch_discovery(), _patch_wifibulb(device=bulb):
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
await hass.async_block_till_done()
assert config_entry.state == ConfigEntryState.LOADED
assert len(bulb.async_set_time.mock_calls) == 1
async_fire_time_changed(hass, utcnow() + timedelta(hours=24))
await hass.async_block_till_done()
assert len(bulb.async_set_time.mock_calls) == 2