Skip to content

Commit

Permalink
More tests and some refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbkarlsson authored Dec 9, 2022
1 parent 3437f03 commit 7404247
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
3 changes: 1 addition & 2 deletions custom_components/ev_smart_charging/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
11 changes: 4 additions & 7 deletions custom_components/ev_smart_charging/helpers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"""
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion manage/pytest_all_ha_versions
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
homeassistant==2022.10.5
homeassistant==2022.11.4
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest-homeassistant-custom-component==0.12.10
pytest-homeassistant-custom-component==0.12.21
27 changes: 21 additions & 6 deletions tests/helpers/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()"""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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()"""
Expand Down

0 comments on commit 7404247

Please sign in to comment.