Skip to content

Commit

Permalink
Avoid turning on charger during intialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbkarlsson authored Aug 13, 2024
1 parent b09b47b commit 9017d33
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions custom_components/ev_smart_charging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
EntityRegistry,
async_entries_for_config_entry,
)
from homeassistant.util import dt

from .coordinator import EVSmartChargingCoordinator
from .const import (
Expand Down Expand Up @@ -47,6 +48,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
raise ConfigEntryNotReady(validation_error)

hass.data[DOMAIN][entry.entry_id] = coordinator
coordinator.setup_timestamp = dt.now().timestamp()

for platform in PLATFORMS:
coordinator.platforms.append(platform)
Expand Down
29 changes: 28 additions & 1 deletion custom_components/ev_smart_charging/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class EventStateChangedData:
from homeassistant.helpers.device_registry import async_get as async_device_registry_get
from homeassistant.helpers.device_registry import DeviceRegistry
from homeassistant.helpers.event import (
async_call_later,
async_track_state_change,
async_track_time_change,
async_track_state_change_event,
Expand Down Expand Up @@ -101,6 +102,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
self.config_entry = config_entry
self.platforms = []
self.listeners = []
self.setup_timestamp = None

self.sensor = None
self.sensor_status = None
Expand Down Expand Up @@ -190,6 +192,10 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
self.listeners.append(
hass.bus.async_listen(EVENT_DEVICE_REGISTRY_UPDATED, self.device_updated)
)
# Update state once after intitialization
self.listeners.append(
async_call_later(hass, 10.0, self.update_initial)
)

def unsubscribe_listeners(self):
"""Unsubscribed to listeners"""
Expand Down Expand Up @@ -228,6 +234,21 @@ async def update_hourly(
_LOGGER.debug("EVSmartChargingCoordinator.update_hourly()")
await self.update_sensors()

@callback
async def update_initial(
self, date_time: datetime = None
): # pylint: disable=unused-argument
"""Called once"""
_LOGGER.debug("EVSmartChargingCoordinator.update_initial()")
await self.update_configuration()

def is_during_intialization(self) -> bool:
"""Checks if the integration is being intialized"""
# Assumes initialization takes less than 5 seconds.
now_timestamp = dt.now().timestamp()
time_since_start = now_timestamp - self.setup_timestamp
return time_since_start < 5

@callback
async def update_state(
self, date_time: datetime = None
Expand Down Expand Up @@ -309,7 +330,13 @@ async def update_state(
# Keep charger on.
turn_on_charging = True

# Handle conencted EV for EV controlled charging
# Don't turn on charging if initialization is not complete.
# Initialization is assumed to be finished in 5 seconds.
if self.is_during_intialization():
turn_on_charging = False
_LOGGER.debug("is_during_intialization() = True")

# Handle connected EV for EV controlled charging
if self.after_ev_connected:
self.after_ev_connected = False
# Make sure charging command is sent
Expand Down
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,22 @@ def skip_update_hourly_fixture():
"custom_components.ev_smart_charging.coordinator.EVSmartChargingCoordinator.update_hourly"
):
yield

# This fixture is used to prevent calls to update_initial().
@pytest.fixture(name="skip_update_initial", autouse=True)
def skip_update_initial_fixture():
"""Skip update_initial."""
with patch(
"custom_components.ev_smart_charging.coordinator.EVSmartChargingCoordinator.update_initial"
):
yield

# This fixture will result in calls to is_during_intialization to return False.
@pytest.fixture(name="bypass_is_during_intialization", autouse=True)
def bypass_is_during_intialization_fixture():
"""Skip calls to check if initialization is on-going."""
with patch(
"custom_components.ev_smart_charging.coordinator.EVSmartChargingCoordinator.is_during_intialization",
return_value=False,
):
yield

0 comments on commit 9017d33

Please sign in to comment.