From 7404247fb6888d3e0b3c5f4bffd38d4d26f3d089 Mon Sep 17 00:00:00 2001 From: Jonas Karlsson Date: Fri, 9 Dec 2022 22:44:37 +0100 Subject: [PATCH] More tests and some refactoring. --- .../ev_smart_charging/coordinator.py | 3 +-- .../ev_smart_charging/helpers/coordinator.py | 11 +++----- manage/pytest_all_ha_versions | 11 +++++++- requirements_dev.txt | 2 +- requirements_test.txt | 2 +- tests/helpers/test_coordinator.py | 27 ++++++++++++++----- tests/test_coordinator.py | 17 ++++++++++++ 7 files changed, 55 insertions(+), 18 deletions(-) diff --git a/custom_components/ev_smart_charging/coordinator.py b/custom_components/ev_smart_charging/coordinator.py index 049d29a..adef26b 100644 --- a/custom_components/ev_smart_charging/coordinator.py +++ b/custom_components/ev_smart_charging/coordinator.py @@ -283,8 +283,7 @@ async def update_sensors( scheduling_params.update( {"value_in_graph": self.raw_two_days.max_value() * 0.75} ) - self.scheduler.calc_schedule(scheduling_params) - new_charging = self.scheduler.get_schedule() + new_charging = self.scheduler.get_schedule(scheduling_params) if new_charging is not None: self._charging_schedule = new_charging self.sensor.charging_schedule = ( diff --git a/custom_components/ev_smart_charging/helpers/coordinator.py b/custom_components/ev_smart_charging/helpers/coordinator.py index 9b80101..ed433e6 100644 --- a/custom_components/ev_smart_charging/helpers/coordinator.py +++ b/custom_components/ev_smart_charging/helpers/coordinator.py @@ -274,13 +274,13 @@ def base_schedule_exists(self) -> bool: """Return true if base schedule exists""" return len(self.schedule_base) > 0 - def calc_schedule(self, params: dict[str, Any]): + def get_schedule(self, params: dict[str, Any]) -> list: """Calculate the schedule""" if "switch_active" not in params or "switch_apply_limit" not in params: self.schedule = None self.calc_schedule_summary() - return + return self.schedule schedule = get_charging_update( self.schedule_base, @@ -310,11 +310,12 @@ def calc_schedule(self, params: dict[str, Any]): _LOGGER.debug("Use schedule_min_soc") self.schedule = schedule_min_soc self.calc_schedule_summary() - return + return self.schedule _LOGGER.debug("Use schedule") self.schedule = schedule self.calc_schedule_summary() + return self.schedule def calc_schedule_summary(self): """Calculate summary of schedule""" @@ -335,10 +336,6 @@ def calc_schedule_summary(self): self.charging_start_time = first_start self.charging_stop_time = last_stop - def get_schedule(self) -> list: - """Get the schedule""" - return self.schedule - def get_charging_is_planned(self): """Get charging_is_planned""" return self.charging_is_planned diff --git a/manage/pytest_all_ha_versions b/manage/pytest_all_ha_versions index efb208e..972fb1b 100644 --- a/manage/pytest_all_ha_versions +++ b/manage/pytest_all_ha_versions @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# https://github.com/home-assistant/core +# https://github.com/MatthewFlamm/pytest-homeassistant-custom-component + # echo # pip3 install homeassistant~=2022.6.0 pytest-homeassistant-custom-component~=0.9.0 # pip3 list 2>/dev/null | grep homeassistant | grep -v "\-home" @@ -38,7 +41,13 @@ pip3 list 2>/dev/null | grep pytest-homeassistant-custom-component pytest -q --no-summary tests echo -pip3 install -qq homeassistant~=2022.11.0 pytest-homeassistant-custom-component~=0.12.0 +pip3 install -qq homeassistant==2022.11.4 pytest-homeassistant-custom-component==0.12.21 +pip3 list 2>/dev/null | grep homeassistant | grep -v "\-home" +pip3 list 2>/dev/null | grep pytest-homeassistant-custom-component +pytest -q --no-summary tests + +echo +pip3 install -qq homeassistant~=2022.12.0 pytest-homeassistant-custom-component~=0.12.0 pip3 list 2>/dev/null | grep homeassistant | grep -v "\-home" pip3 list 2>/dev/null | grep pytest-homeassistant-custom-component pytest -q --no-summary tests diff --git a/requirements_dev.txt b/requirements_dev.txt index 2e0ee0c..dda58f4 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1 +1 @@ -homeassistant==2022.10.5 +homeassistant==2022.11.4 diff --git a/requirements_test.txt b/requirements_test.txt index 38bf2e0..9b1028f 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1 +1 @@ -pytest-homeassistant-custom-component==0.12.10 +pytest-homeassistant-custom-component==0.12.21 diff --git a/tests/helpers/test_coordinator.py b/tests/helpers/test_coordinator.py index cf14537..6da4dfe 100644 --- a/tests/helpers/test_coordinator.py +++ b/tests/helpers/test_coordinator.py @@ -233,26 +233,41 @@ async def test_scheduler(hass, set_cet_timezone, freezer): scheduling_params.update({"value_in_graph": 300}) - scheduler.calc_schedule(scheduling_params) - new_charging: list = scheduler.get_schedule() + new_charging: list = scheduler.get_schedule(scheduling_params) assert not new_charging scheduling_params.update({"switch_apply_limit": True}) - scheduler.calc_schedule(scheduling_params) - new_charging: list = scheduler.get_schedule() + new_charging: list = scheduler.get_schedule(scheduling_params) assert new_charging assert new_charging[26]["value"] == 0 assert new_charging[27]["value"] == 300 + assert scheduler.get_charging_is_planned() is True + assert scheduler.get_charging_start_time() == datetime( + 2022, 10, 1, 3, 0, tzinfo=dt_util.get_time_zone("Europe/Stockholm") + ) + assert scheduler.get_charging_stop_time() == datetime( + 2022, 10, 1, 7, 0, tzinfo=dt_util.get_time_zone("Europe/Stockholm") + ) + assert scheduler.get_charging_number_of_hours() == 4 + scheduling_params.update({"min_soc": 80}) scheduler.create_base_schedule(scheduling_params, raw_two_days) - scheduler.calc_schedule(scheduling_params) - new_charging: list = scheduler.get_schedule() + new_charging: list = scheduler.get_schedule(scheduling_params) assert new_charging assert new_charging[22]["value"] == 0 assert new_charging[23]["value"] == 300 + assert scheduler.get_charging_is_planned() is True + assert scheduler.get_charging_start_time() == datetime( + 2022, 9, 30, 23, 0, tzinfo=dt_util.get_time_zone("Europe/Stockholm") + ) + assert scheduler.get_charging_stop_time() == datetime( + 2022, 10, 1, 7, 0, tzinfo=dt_util.get_time_zone("Europe/Stockholm") + ) + assert scheduler.get_charging_number_of_hours() == 8 + async def test_get_empty_schedule(hass, set_cet_timezone, freezer): """Test Scheduler.get_empty_schedule()""" diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index cdbeab2..b610a9d 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -1,10 +1,13 @@ """Test ev_smart_charging coordinator.""" +from datetime import datetime + from pytest_homeassistant_custom_component.common import MockConfigEntry from homeassistant.core import HomeAssistant from homeassistant.const import STATE_ON, STATE_OFF from homeassistant.helpers.entity_registry import async_get as async_entity_registry_get from homeassistant.helpers.entity_registry import EntityRegistry +from homeassistant.util import dt as dt_util from custom_components.ev_smart_charging.coordinator import ( EVSmartChargingCoordinator, @@ -218,6 +221,15 @@ async def test_coordinator_min_soc2( assert coordinator.auto_charging_state == STATE_OFF assert coordinator.sensor.state == STATE_OFF + assert coordinator.sensor.charging_is_planned is True + assert coordinator.sensor.charging_start_time == datetime( + 2022, 10, 1, 3, 0, tzinfo=dt_util.get_time_zone("Europe/Stockholm") + ) + assert coordinator.sensor.charging_stop_time == datetime( + 2022, 10, 1, 8, 0, tzinfo=dt_util.get_time_zone("Europe/Stockholm") + ) + assert coordinator.sensor.charging_number_of_hours == 5 + # Move time to scheduled charging time freezer.move_to("2022-10-01T03:00:00+02:00") MockPriceEntity.set_state(hass, PRICE_20221001, None) @@ -233,6 +245,11 @@ async def test_coordinator_min_soc2( assert coordinator.auto_charging_state == STATE_OFF assert coordinator.sensor.state == STATE_OFF + assert coordinator.sensor.charging_is_planned is False + assert coordinator.sensor.charging_start_time is None + assert coordinator.sensor.charging_stop_time is None + assert coordinator.sensor.charging_number_of_hours == 0 + async def test_validate_input_sensors(hass: HomeAssistant): """Test validate_input_sensors()"""