Skip to content

Commit

Permalink
Fixed charging_is_planned. Added test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbkarlsson authored Dec 6, 2022
1 parent 9da4a0e commit 3437f03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
40 changes: 23 additions & 17 deletions custom_components/ev_smart_charging/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,29 @@ async def update_state(
# Turn off charging
self.auto_charging_state = STATE_OFF
await self.turn_off_charging()
if self.scheduler.get_charging_is_planned():
time_now = dt.now()
if time_now > self.scheduler.charging_stop_time:
self.sensor.charging_is_planned = False
self.sensor.charging_start_time = None
self.sensor.charging_stop_time = None
self.sensor.charging_number_of_hours = 0

time_now = dt.now()
if (
self.scheduler.charging_stop_time is not None
and time_now < self.scheduler.charging_stop_time
):
_LOGGER.debug("Charging summary shown")
self.sensor.charging_is_planned = (
self.scheduler.get_charging_is_planned()
)
self.sensor.charging_start_time = (
self.scheduler.get_charging_start_time()
)
self.sensor.charging_stop_time = self.scheduler.get_charging_stop_time()
self.sensor.charging_number_of_hours = (
self.scheduler.get_charging_number_of_hours()
)
else:
_LOGGER.debug("Charging summary removed")
self.sensor.charging_is_planned = False
self.sensor.charging_start_time = None
self.sensor.charging_stop_time = None
self.sensor.charging_number_of_hours = 0

async def turn_on_charging(self):
"""Turn on charging"""
Expand Down Expand Up @@ -274,16 +290,6 @@ async def update_sensors(
self.sensor.charging_schedule = (
Raw(self._charging_schedule).copy().to_local().get_raw()
)
self.sensor.charging_is_planned = (
self.scheduler.get_charging_is_planned()
)
self.sensor.charging_start_time = (
self.scheduler.get_charging_start_time()
)
self.sensor.charging_stop_time = self.scheduler.get_charging_stop_time()
self.sensor.charging_number_of_hours = (
self.scheduler.get_charging_number_of_hours()
)

_LOGGER.debug("self._max_price = %s", self.max_price)
_LOGGER.debug("Current price = %s", self.sensor.current_price)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ev_smart_charging/helpers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def __init__(self) -> None:
self.charging_is_planned = False
self.charging_start_time = None
self.charging_stop_time = None
self.charging_number_of_hours = None
self.charging_number_of_hours = 0

def create_base_schedule(
self,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Test ev_smart_charging sensor."""
from zoneinfo import ZoneInfo
from datetime import datetime

from homeassistant.const import STATE_OFF, STATE_ON
from pytest_homeassistant_custom_component.common import MockConfigEntry

Expand Down Expand Up @@ -65,10 +68,38 @@ async def test_sensor(hass, bypass_validate_input_sensors):
sensor.charging_schedule = one_list
assert sensor.charging_schedule == one_list

sensor.charging_is_planned = True
assert sensor.charging_is_planned is True

sensor.charging_start_time = datetime(
2022, 9, 30, 1, 0, tzinfo=ZoneInfo(key="Europe/Stockholm")
)
assert sensor.charging_start_time == datetime(
2022, 9, 30, 1, 0, tzinfo=ZoneInfo(key="Europe/Stockholm")
)

sensor.charging_stop_time = datetime(
2022, 9, 30, 5, 0, tzinfo=ZoneInfo(key="Europe/Stockholm")
)
assert sensor.charging_stop_time == datetime(
2022, 9, 30, 5, 0, tzinfo=ZoneInfo(key="Europe/Stockholm")
)

sensor.charging_number_of_hours = 4
assert sensor.charging_number_of_hours == 4

extra = sensor.extra_state_attributes
assert extra["current_price"] == 12.1
assert extra["EV SOC"] == 56
assert extra["EV target SOC"] == 80
assert extra["Charging is planned"] is True
assert extra["Charging start time"] == datetime(
2022, 9, 30, 1, 0, tzinfo=ZoneInfo(key="Europe/Stockholm")
)
assert extra["Charging stop time"] == datetime(
2022, 9, 30, 5, 0, tzinfo=ZoneInfo(key="Europe/Stockholm")
)
assert extra["Charging number of hours"] == 4

# Unload the entry and verify that the data has been removed
assert await async_unload_entry(hass, config_entry)
Expand Down

0 comments on commit 3437f03

Please sign in to comment.