From 13b262bcce538c5b18576bdefe3a0ceb44529b1d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 4 Jan 2022 06:37:15 -1000 Subject: [PATCH] Keep flux_led device time in sync (#63259) --- homeassistant/components/flux_led/__init__.py | 12 +++++++++++- tests/components/flux_led/__init__.py | 2 ++ tests/components/flux_led/test_init.py | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/flux_led/__init__.py b/homeassistant/components/flux_led/__init__.py index 81fb9a1ce24..63dcfe0271f 100644 --- a/homeassistant/components/flux_led/__init__.py +++ b/homeassistant/components/flux_led/__init__.py @@ -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 diff --git a/tests/components/flux_led/__init__.py b/tests/components/flux_led/__init__.py index ae03726fdfa..1a44dd949aa 100644 --- a/tests/components/flux_led/__init__.py +++ b/tests/components/flux_led/__init__.py @@ -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() diff --git a/tests/components/flux_led/test_init.py b/tests/components/flux_led/test_init.py index 1b4864de788..7981f2cef11 100644 --- a/tests/components/flux_led/test_init.py +++ b/tests/components/flux_led/test_init.py @@ -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