Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify battery model by extracting schedule and utils~ #106

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions custom_components/foxess_em/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import logging
from datetime import time

from custom_components.foxess_em.battery.schedule import Schedule
from custom_components.foxess_em.util.peak_period_util import PeakPeriodUtils
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config
from homeassistant.core import CoreState
Expand Down Expand Up @@ -82,10 +84,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
fox_client = FoxApiClient(session, fox_username, fox_password)

# Initialise controllers and services
peak_utils = PeakPeriodUtils(eco_start_time, eco_end_time)

forecast_controller = ForecastController(hass, solcast_client)
average_controller = AverageController(
hass, eco_start_time, eco_end_time, house_power, aux_power
)
schedule = Schedule(hass)
battery_controller = BatteryController(
hass,
forecast_controller,
Expand All @@ -95,8 +100,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
dawn_buffer,
day_buffer,
eco_start_time,
eco_end_time,
battery_soc,
schedule,
peak_utils,
)
fox_service = FoxCloudService(fox_client)
charge_service = ChargeService(
Expand All @@ -120,7 +126,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
}

await _refresh_controllers(
hass, average_controller, forecast_controller, battery_controller
hass, average_controller, forecast_controller, battery_controller, schedule
)

hass.services.async_register(
Expand All @@ -136,11 +142,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
return True


async def _refresh_controllers(hass: HomeAssistant, average, forecast, battery):
async def _refresh_controllers(
hass: HomeAssistant,
average: AverageController,
forecast: ForecastController,
battery: BatteryController,
schedule: Schedule,
):
"""Refresh all controllers"""

if hass.state is CoreState.running:
# Prime history for sensor creation
schedule.load()
await average.async_refresh()
await forecast.async_refresh()
await battery.async_refresh()
Expand All @@ -151,7 +164,7 @@ async def _refresh_controllers(hass: HomeAssistant, average, forecast, battery):
else:
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STARTED,
_refresh_controllers(hass, average, forecast, battery),
_refresh_controllers(hass, average, forecast, battery, schedule),
)


Expand Down
57 changes: 39 additions & 18 deletions custom_components/foxess_em/battery/battery_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from datetime import datetime
from datetime import time

from custom_components.foxess_em.battery.battery_util import BatteryUtils
from custom_components.foxess_em.battery.schedule import Schedule
from custom_components.foxess_em.util.peak_period_util import PeakPeriodUtils
from homeassistant.core import HomeAssistant
from homeassistant.helpers.event import async_track_state_change

Expand All @@ -29,21 +32,27 @@ def __init__(
dawn_buffer: float,
day_buffer: float,
eco_start_time: time,
eco_end_time: time,
battery_soc: str,
schedule: Schedule,
peak_utils: PeakPeriodUtils,
) -> None:
UnloadController.__init__(self)
CallbackController.__init__(self)
self._hass = hass
self._schedule = schedule
self._peak_utils = peak_utils
self._battery_utils = BatteryUtils(capacity, min_soc)
self._model = BatteryModel(
hass,
min_soc,
capacity,
dawn_buffer,
day_buffer,
eco_start_time,
eco_end_time,
battery_soc,
schedule,
peak_utils,
self._battery_utils,
)
self._forecast_controller = forecast_controller
self._average_controller = average_controller
Expand Down Expand Up @@ -87,11 +96,11 @@ def update_callback(self) -> None:

def charge_to_perc(self) -> int:
"""Calculate percentage target"""
return self._model.charge_to_perc(self.min_soc())
return self._battery_utils.charge_to_perc(self.min_soc())

def get_schedule(self):
"""Return charge schedule"""
return self._model.get_schedule()
return self._schedule.get_all()

def raw_data(self):
"""Return raw data in dictionary form"""
Expand All @@ -103,11 +112,23 @@ def state_at_eco_start(self) -> float:

def dawn_charge_needs(self) -> float:
"""Dawn charge needs"""
return self._model.dawn_charge()
return self._schedule_info()["dawn"]

def day_charge_needs(self) -> float:
"""Day charge needs"""
return self._model.day_charge()
return self._schedule_info()["day"]

def charge_total(self) -> float:
"""Total kWh required to charge"""
return self._schedule_info()["total"]

def min_soc(self) -> float:
"""Total kWh required to charge"""
return self._schedule_info()["min_soc"]

def _schedule_info(self) -> float:
"""Schedule info"""
return self._schedule.get(self._peak_utils.next_eco_start())

def next_dawn_time(self) -> datetime:
"""Day charge needs"""
Expand All @@ -117,14 +138,6 @@ def todays_dawn_time_str(self) -> datetime:
"""Day charge needs"""
return self._model.todays_dawn_time().isoformat()

def charge_total(self) -> float:
"""Total kWh required to charge"""
return self._model.total_charge()

def min_soc(self) -> float:
"""Total kWh required to charge"""
return self._model.min_soc()

def battery_last_update(self) -> datetime:
"""Battery last update"""
return self._last_update
Expand All @@ -143,21 +156,29 @@ def forecast_last_update_str(self) -> str:

def set_boost(self, status: bool) -> None:
"""Set boost on/off"""
self._model.set_boost_full_charge_status("boost_status", status)
self._schedule.set_boost(
self._peak_utils.next_eco_start(), "boost_status", status
)
self.refresh()

def boost_status(self) -> bool:
"""Boost status"""
return self._model.get_boost_full_charge_status("boost_status")
return self._schedule.get_boost(
self._peak_utils.next_eco_start(), "boost_status"
)

def set_full(self, status: bool) -> None:
"""Set full charge on/off"""
self._model.set_boost_full_charge_status("full_status", status)
self._schedule.set_boost(
self._peak_utils.next_eco_start(), "full_status", status
)
self.refresh()

def full_status(self) -> bool:
"""Full status"""
return self._model.get_boost_full_charge_status("full_status")
return self._schedule.get_boost(
self._peak_utils.next_eco_start(), "full_status"
)

def battery_depleted(self) -> datetime:
"""Time battery capacity is 0"""
Expand Down
Loading