Skip to content

Commit

Permalink
decouple observer wip
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Jul 4, 2024
1 parent ad44083 commit 4c3398e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
7 changes: 3 additions & 4 deletions custom_components/peaqev/peaqservice/hub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
HubOptions
from custom_components.peaqev.peaqservice.hub.models.initializer_types import \
InitializerTypes
from custom_components.peaqev.peaqservice.hub.observer.observer_coordinator import \
Observer
from custom_components.peaqev.peaqservice.hub.observer.iobserver_coordinator import IObserver
from custom_components.peaqev.peaqservice.hub.sensors.hub_sensors_base import \
HubSensorsBase
from custom_components.peaqev.peaqservice.hub.servicecalls import ServiceCalls
Expand Down Expand Up @@ -72,13 +71,13 @@ class HomeAssistantHub:
not_ready_list_old_state = 0
_initialized: bool = False

def __init__(self, hass: HomeAssistant, options: HubOptions, domain: str):
def __init__(self, hass: HomeAssistant, options: HubOptions, domain: str, observer: IObserver):
self.model = HubModel(domain, hass)
self.hubname = domain.capitalize()
self.state_machine = hass
self.options: HubOptions = options
self._is_initialized = False
self.observer = Observer(self)
self.observer = observer
self._set_observers()

async def async_setup(self):
Expand Down
15 changes: 9 additions & 6 deletions custom_components/peaqev/peaqservice/hub/hub_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from custom_components.peaqev.peaqservice.hub.hub_events import HubEvents
from custom_components.peaqev.peaqservice.hub.models.hub_options import \
HubOptions
from custom_components.peaqev.peaqservice.hub.observer.iobserver_coordinator import IObserver
from custom_components.peaqev.peaqservice.hub.observer.observer_coordinator import Observer
from custom_components.peaqev.peaqservice.hub.price_aware_hub import \
PriceAwareHub
from custom_components.peaqev.peaqservice.hub.sensors.hubsensors_factory import \
Expand All @@ -29,14 +31,15 @@
class HubFactory:
@staticmethod
async def async_create(hass: HomeAssistant, options: HubOptions, domain: str) -> HomeAssistantHub:
observer = Observer(hass)
if options.price.price_aware:
hub = PriceAwareHub
else:
hub = HomeAssistantHub
return await HubFactory.async_setup(hub(hass, options, domain))
return await HubFactory.async_setup(hub(hass, options, domain, observer), observer)

@staticmethod
async def async_setup(hub: HomeAssistantHub) -> HomeAssistantHub:
async def async_setup(hub: HomeAssistantHub, observer: IObserver) -> HomeAssistantHub:
hub.chargertype = await ChargerTypeFactory.async_create(hub.state_machine, hub.options)
hub.sensors = await HubSensorsFactory.async_create(hub=hub)
hub.chargecontroller = await ChargeControllerFactory.async_create(
Expand All @@ -46,12 +49,12 @@ async def async_setup(hub: HomeAssistantHub) -> HomeAssistantHub:
)
hub.hours = await HourselectionFactory.async_create(hub)
hub.threshold = await ThresholdFactory.async_create(hub)
hub.prediction = Prediction(hub) # threshold
hub.servicecalls = ServiceCalls(hub) # top level
hub.states = await StateChangesFactory.async_create(hub) # top level
hub.prediction = Prediction(hub)
hub.servicecalls = ServiceCalls(hub, observer)
hub.states = await StateChangesFactory.async_create(hub)
hub.spotprice = SpotPriceFactory.create(
hub=hub,
observer=hub.observer,
observer=observer,
system=PeaqSystem.PeaqEv,
test=False,
is_active=hub.options.price.price_aware,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@


class Observer(IObserver):
def __init__(self, hub):
def __init__(self, hass):
super().__init__()
self.hub = hub
self.hass = hass
async_track_time_interval(
self.hub.state_machine, self.async_dispatch, timedelta(seconds=1)
self.hass, self.async_dispatch, timedelta(seconds=1)
)

async def async_broadcast_separator(self, func, command: Command):
if await async_iscoroutine(func):
await self.async_call_func(func=func, command=command),
else:
await self.hub.state_machine.async_add_executor_job(
await self.hass.async_add_executor_job(
self._call_func, func, command
)
)
5 changes: 3 additions & 2 deletions custom_components/peaqev/peaqservice/hub/price_aware_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
MaxMinController
from custom_components.peaqev.peaqservice.hub.models.hub_options import \
HubOptions
from custom_components.peaqev.peaqservice.hub.observer.iobserver_coordinator import IObserver
from custom_components.peaqev.peaqservice.util.schedule_options_handler import \
SchedulerOptionsHandler

_LOGGER = logging.getLogger(__name__)

class PriceAwareHub(HomeAssistantHub):
def __init__(self, hass: HomeAssistant, options: HubOptions, domain: str):
super().__init__(hass, options, domain)
def __init__(self, hass: HomeAssistant, options: HubOptions, domain: str, observer: IObserver):
super().__init__(hass, options, domain, observer)
self.max_min_controller = MaxMinController(self)
self._scheduler_options_handler = SchedulerOptionsHandler(self)

Expand Down
19 changes: 11 additions & 8 deletions custom_components/peaqev/peaqservice/hub/servicecalls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from peaqevcore.services.scheduler.update_scheduler_dto import \
UpdateSchedulerDTO

from custom_components.peaqev.peaqservice.hub.observer.iobserver_coordinator import IObserver

if TYPE_CHECKING:
from custom_components.peaqev.peaqservice.hub.hub import HomeAssistantHub

Expand All @@ -16,24 +18,25 @@


class ServiceCalls:
def __init__(self, hub: HomeAssistantHub):
def __init__(self, hub: HomeAssistantHub, observer: IObserver):
self.hub = hub
self.observer = observer

async def async_call_enable_peaq(self):
"""peaqev.enable"""
await self.hub.observer.async_broadcast(ObserverTypes.UpdateChargerEnabled, True)
await self.hub.observer.async_broadcast(ObserverTypes.UpdateChargerDone, False)
await self.observer.async_broadcast(ObserverTypes.UpdateChargerEnabled, True)
await self.observer.async_broadcast(ObserverTypes.UpdateChargerDone, False)

async def async_call_disable_peaq(self):
"""peaqev.disable"""
await self.hub.observer.async_broadcast(ObserverTypes.UpdateChargerEnabled, False)
await self.hub.observer.async_broadcast(ObserverTypes.UpdateChargerDone, False)
await self.observer.async_broadcast(ObserverTypes.UpdateChargerEnabled, False)
await self.observer.async_broadcast(ObserverTypes.UpdateChargerDone, False)

async def async_call_override_nonhours(self, hours: int = 1):
"""peaqev.override_nonhours"""
if self.hub.hours.price_aware:
await self.hub.hours.timer.async_update(hours)
await self.hub.observer.async_broadcast(ObserverTypes.TimerActivated)
await self.observer.async_broadcast(ObserverTypes.TimerActivated)

async def async_call_schedule_needed_charge(
self,
Expand Down Expand Up @@ -74,9 +77,9 @@ async def async_call_schedule_needed_charge(
chargecontroller_state = self.hub.chargecontroller.status_type
)
await self.hub.hours.scheduler.async_update_facade(dto)
await self.hub.observer.async_broadcast(ObserverTypes.SchedulerCreated)
await self.observer.async_broadcast(ObserverTypes.SchedulerCreated)

async def async_call_scheduler_cancel(self):
if self.hub.hours.price_aware:
await self.hub.hours.scheduler.async_cancel_facade()
await self.hub.observer.async_broadcast(ObserverTypes.SchedulerCancelled)
await self.observer.async_broadcast(ObserverTypes.SchedulerCancelled)

0 comments on commit 4c3398e

Please sign in to comment.