From f8030761d7f1e2c641829e86414fd6caae061d00 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Tue, 12 Nov 2024 21:58:50 +0000 Subject: [PATCH] Not usable cannot be forced in offpeak --- .../solar_optimizer/managed_device.py | 3 + tests/test_min_in_time.py | 86 ++++++++++--------- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/custom_components/solar_optimizer/managed_device.py b/custom_components/solar_optimizer/managed_device.py index 5711e25..4c49988 100644 --- a/custom_components/solar_optimizer/managed_device.py +++ b/custom_components/solar_optimizer/managed_device.py @@ -416,6 +416,9 @@ def is_usable(self) -> bool: @property def should_be_forced_offpeak(self) -> bool: """True is we are offpeak and the max_on_time is not exceeded""" + if not self.is_usable: + return False + if self._offpeak_time >= self._coordinator.raz_time: return ( self.now.time() >= self._offpeak_time diff --git a/tests/test_min_in_time.py b/tests/test_min_in_time.py index f61b0b9..612df36 100644 --- a/tests/test_min_in_time.py +++ b/tests/test_min_in_time.py @@ -183,7 +183,11 @@ async def test_min_on_time_config_ko_3( device._set_now(current_datetime.replace(tzinfo=get_tz(hass))) - assert device.should_be_forced_offpeak is should_be_forced_offpeak + with patch( + "custom_components.solar_optimizer.managed_device.ManagedDevice.is_usable", + return_value=True, + ): + assert device.should_be_forced_offpeak is should_be_forced_offpeak async def test_nominal_use_min_on_time( @@ -268,41 +272,45 @@ async def test_nominal_use_min_on_time( # # 5. at 01:00 it should be possible to force offpeak # - now = datetime(2024, 11, 10, 1, 00, 00).replace(tzinfo=get_tz(hass)) - device_a._set_now(now) - assert device_a.should_be_forced_offpeak is True - - # - # 6. at 04:59 it should be possible to force offpeak - # - now = datetime(2024, 11, 10, 4, 59, 00).replace(tzinfo=get_tz(hass)) - device_a._set_now(now) - assert device_a.should_be_forced_offpeak is True - - # - # 6. at 05:01 it should be not possible to force offpeak - # - now = datetime(2024, 11, 10, 5, 1, 00).replace(tzinfo=get_tz(hass)) - device_a._set_now(now) - assert device_a.should_be_forced_offpeak is False - - # - # 7. when on_time is > max_on_time it should be not possible to force off_peak - # - # Come back in offpeak - now = datetime(2024, 11, 10, 0, 0, 00).replace(tzinfo=get_tz(hass)) - device_a._set_now(now) - assert device_a.should_be_forced_offpeak is True - - await fake_input_bool.async_turn_on() - await hass.async_block_till_done() - - now = now + timedelta(minutes=6) # 6 minutes + 4 minutes already done - device_a._set_now(now) - await fake_input_bool.async_turn_off() - await hass.async_block_till_done() - - assert device_a_on_time_sensor.state == 10 * 60 - assert device_a._on_time_sec == 10 * 60 - - assert device_a.should_be_forced_offpeak is False + with patch( + "custom_components.solar_optimizer.managed_device.ManagedDevice.is_usable", + return_value=True, + ): + now = datetime(2024, 11, 10, 1, 00, 00).replace(tzinfo=get_tz(hass)) + device_a._set_now(now) + assert device_a.should_be_forced_offpeak is True + + # + # 6. at 04:59 it should be possible to force offpeak + # + now = datetime(2024, 11, 10, 4, 59, 00).replace(tzinfo=get_tz(hass)) + device_a._set_now(now) + assert device_a.should_be_forced_offpeak is True + + # + # 6. at 05:01 it should be not possible to force offpeak + # + now = datetime(2024, 11, 10, 5, 1, 00).replace(tzinfo=get_tz(hass)) + device_a._set_now(now) + assert device_a.should_be_forced_offpeak is False + + # + # 7. when on_time is > max_on_time it should be not possible to force off_peak + # + # Come back in offpeak + now = datetime(2024, 11, 10, 0, 0, 00).replace(tzinfo=get_tz(hass)) + device_a._set_now(now) + assert device_a.should_be_forced_offpeak is True + + await fake_input_bool.async_turn_on() + await hass.async_block_till_done() + + now = now + timedelta(minutes=6) # 6 minutes + 4 minutes already done + device_a._set_now(now) + await fake_input_bool.async_turn_off() + await hass.async_block_till_done() + + assert device_a_on_time_sensor.state == 10 * 60 + assert device_a._on_time_sec == 10 * 60 + + assert device_a.should_be_forced_offpeak is False