Skip to content

Commit

Permalink
refactor, core 19.10.12
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed May 25, 2024
1 parent 1afc696 commit 7d370a2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion custom_components/peaqev/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"iot_class": "calculated",
"issue_tracker": "https://github.com/elden1337/hass-peaq/issues",
"requirements": [
"peaqevcore==19.10.10"
"peaqevcore==19.10.12"
],
"version": "3.6.1b0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

_LOGGER = logging.getLogger(__name__)


class ChargeController(IChargeController):
def __init__(
self, hub: HomeAssistantHub, charger_states: dict, charger_type: ChargerType
self, hub: HomeAssistantHub, charger_states: dict, charger_type: ChargerType
):
self._aux_running_grace_timer = WaitTimer(timeout=300, init_now=True)
super().__init__(hub, charger_states, charger_type)
Expand All @@ -48,7 +49,11 @@ def _check_initialized(self) -> bool:
if float_state > 0:
return self._do_initialize()
except Exception as e:
_LOGGER.error('Could not convert state to float: %s', e)
_LOGGER.error(
'Could not convert state to float for sensor (%s). Exception: %s',
self.hub.options.powersensor,
e
)
return False
return False

Expand All @@ -70,12 +75,12 @@ async def async_get_energy_and_peak(self) -> Tuple[float, float]:
async def async_get_status_charging(self) -> ChargeControllerStates:
if any([not self.hub.power.power_canary.alive, self.hub.events.aux_stop,
all(
[
await self.async_above_stopthreshold(),
self.hub.sensors.totalhourlyenergy.value > 0,
not await self.hub.async_free_charge(),
]
)]):
[
await self.async_above_stopthreshold(),
self.hub.sensors.totalhourlyenergy.value > 0,
not await self.hub.async_free_charge(),
]
)]):
return ChargeControllerStates.Stop
return ChargeControllerStates.Start

Expand Down
12 changes: 6 additions & 6 deletions custom_components/peaqev/peaqservice/hub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ async def async_update_prices(self, prices: list) -> None:
async def async_is_caution_hour(self) -> bool:
return str(datetime.now().hour) in [str(h) for h in self.hours.caution_hours]

def _check_max_min_total_charge(self, ret: dict) -> None:
pass

async def async_request_sensor_data(self, *args) -> dict | any:
ret = {}
if not self.is_initialized:
Expand All @@ -192,10 +195,7 @@ async def async_request_sensor_data(self, *args) -> dict | any:
return val
return ret

def _check_max_min_total_charge(self, ret: dict) -> None:
pass

def _request_sensor_lookup(self) -> dict:
def _request_sensor_lookup(self) -> dict[LookupKeys, Callable]:
"""Proxies the request to the correct sensor."""
return {
LookupKeys.CHARGER_DONE: partial(getattr, self.sensors.charger_done, 'value'),
Expand Down Expand Up @@ -251,12 +251,12 @@ def _request_sensor_lookup(self) -> dict:

def now_is_non_hour(self) -> bool:
now = datetime.now().replace(minute=0, second=0, microsecond=0)
non_hours = self._request_sensor_lookup().get(LookupKeys.NON_HOURS, [])
non_hours = self._request_sensor_lookup().get(LookupKeys.NON_HOURS, [])[0]()
return now in non_hours

def now_is_caution_hour(self) -> bool:
now = datetime.now().replace(minute=0, second=0, microsecond=0)
caution_hours = self._request_sensor_lookup().get(LookupKeys.DYNAMIC_CAUTION_HOURS, {})
caution_hours = self._request_sensor_lookup().get(LookupKeys.DYNAMIC_CAUTION_HOURS, {})[0]()
return now in caution_hours.keys()

async def async_free_charge(self) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def scheduler_options_handler(self) -> SchedulerOptionsHandler:
@property
def current_peak_dynamic(self):
"""Dynamically calculated peak to adhere to caution-hours"""
dynamic_caution_hours = self._request_sensor_lookup().get(LookupKeys.DYNAMIC_CAUTION_HOURS, {})
dynamic_caution_hours: dict = self._request_sensor_lookup().get(LookupKeys.DYNAMIC_CAUTION_HOURS, {})()
if len(dynamic_caution_hours) > 0:
if self.now_is_caution_hour() and not getattr(self.hours.timer, 'is_override', False):
return self.sensors.current_peak.observed_peak * dynamic_caution_hours.get(datetime.now().replace(minute=0, second=0, microsecond=0), 1)
Expand Down
11 changes: 10 additions & 1 deletion custom_components/peaqev/peaqservice/hub/servicecalls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING

from peaqevcore.common.models.observer_types import ObserverTypes
from peaqevcore.services.scheduler.update_scheduler_dto import UpdateSchedulerDTO

if TYPE_CHECKING:
from custom_components.peaqev.peaqservice.hub.hub import HomeAssistantHub
Expand Down Expand Up @@ -63,7 +64,15 @@ async def async_call_schedule_needed_charge(
await self.hub.hours.scheduler.async_create_schedule(
charge_amount, dep_time, start_time, override_settings
)
await self.hub.hours.scheduler.async_update_facade()
dto = UpdateSchedulerDTO(
moving_avg24=self.hub.sensors.powersensormovingaverage24.value,
peak=self.hub.current_peak_dynamic,
charged_amount=self.hub.chargecontroller.session.session_energy,
prices=self.hub.hours.prices,
prices_tomorrow=self.hub.hours.prices_tomorrow,
chargecontroller_state = self.hub.chargecontroller.status_type
)
await self.hub.hours.scheduler.async_update_facade(dto)
await self.hub.observer.async_broadcast(ObserverTypes.SchedulerCreated)

async def async_call_scheduler_cancel(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ async def async_update_session_parameters(self, update_session: bool) -> None:
peak=self.hub.current_peak_dynamic,
charged_amount=self.hub.chargecontroller.session.session_energy,
prices=self.hub.hours.prices,
prices_tomorrow=self.hub.hours.prices_tomorrow
prices_tomorrow=self.hub.hours.prices_tomorrow,
chargecontroller_state=self.hub.chargecontroller.status_type
)
await self.hub.hours.scheduler.async_update_facade(dto)
except Exception as e:
Expand Down

0 comments on commit 7d370a2

Please sign in to comment.