4747)
4848from pyhilo .graphql import GraphQlHelper
4949from pyhilo .util import from_utc_timestamp , time_diff
50- from pyhilo .websocket import WebsocketEvent
50+ from pyhilo .websocket import WebsocketEvent , websocket_event_from_payload
5151
5252from .config_flow import STEP_OPTION_SCHEMA , HiloFlowHandler
5353from .const import (
@@ -180,6 +180,20 @@ async def async_setup_entry( # noqa: C901
180180 hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
181181 )
182182
183+ async def handle_debug_event (event : Event ):
184+ """Handle an event."""
185+ LOG .debug ("HILO_DEBUG: Event received: %s" , event )
186+ log_traces = current_options .get (CONF_LOG_TRACES )
187+ LOG .debug ("HILO_DEBUG: log_traces is %s" , log_traces )
188+ websocket_event = websocket_event_from_payload (event .data )
189+ LOG .debug ("HILO_DEBUG: Websocket event parsed: %s" , websocket_event )
190+ await hilo .on_websocket_event (websocket_event )
191+
192+ log_traces = current_options .get (CONF_LOG_TRACES )
193+ if log_traces :
194+ LOG .debug ("HILO_DEBUG: log_traces is %s" , log_traces )
195+ hass .bus .async_listen ("hilo_debug" , handle_debug_event )
196+
183197 async def async_reload_entry (_ : HomeAssistant , updated_entry : ConfigEntry ) -> None :
184198 """Handle an options update.
185199 This method will get called in two scenarios:
@@ -317,36 +331,48 @@ def register_websocket_listener(self, listener):
317331 async def _handle_websocket_message (self , event ):
318332 """Process websocket messages and notify listeners."""
319333
334+ # TODO: ic-dev21: This needs to be cleaned up and optimized
320335 LOG .debug ("Received websocket message type: %s" , event )
321336 target = event .target
322337 LOG .debug ("handle_websocket_message_target %s" , target )
323338 msg_data = event
324339 LOG .debug ("handle_websocket_message_ msg_data %s" , msg_data )
325340
326- if target == "ChallengeListInitialValuesReceived" :
341+ if target in [
342+ "ChallengeListInitialValuesReceived" ,
343+ "EventListInitialValuesReceived" ,
344+ ]:
327345 msg_type = "challenge_list_initial"
328- elif target == "ChallengeAdded" :
346+ elif target in [ "ChallengeAdded" , "EventAdded" ] :
329347 msg_type = "challenge_added"
330348 elif target == "ChallengeDetailsUpdated" :
331349 msg_type = "challenge_details_update"
332350 elif target == "ChallengeConsumptionUpdatedValuesReceived" :
333351 msg_type = "challenge_details_update"
334- elif target == "ChallengeDetailsUpdatedValuesReceived" :
352+ elif target in [
353+ "ChallengeDetailsUpdatedValuesReceived" ,
354+ "EventCHDetailsUpdatedValuesReceived" ,
355+ "EventFlexDetailsUpdatedValuesReceived" ,
356+ ]:
335357 msg_type = "challenge_details_update"
336- elif target == "ChallengeDetailsInitialValuesReceived" :
358+ elif target in [
359+ "ChallengeDetailsInitialValuesReceived" ,
360+ "EventCHDetailsInitialValuesReceived" ,
361+ "EventFlexDetailsInitialValuesReceived" ,
362+ ]:
337363 msg_type = "challenge_details_update"
338- elif target == "ChallengeListUpdatedValuesReceived" :
364+ elif target in [
365+ "ChallengeListUpdatedValuesReceived" ,
366+ "EventListUpdatedValuesReceived" ,
367+ ]:
339368 msg_type = "challenge_details_update"
340- elif target == "EventCHConsumptionUpdatedValuesReceived" :
369+ elif target in [
370+ "EventCHConsumptionUpdatedValuesReceived" ,
371+ "EventFlexConsumptionUpdatedValuesReceived" ,
372+ ]:
341373 LOG .debug ("%s message received" , target )
342374 LOG .debug ("%s data: %s" , target , msg_data )
343375 return
344- elif target == "EventCHDetailsUpdatedValuesReceived" :
345- msg_type = "challenge_details_update"
346- elif target == "EventFlexDetailsUpdatedValuesReceived" :
347- msg_type = "challenge_details_update"
348- elif target == "EventListUpdatedValuesReceived" :
349- msg_type = "challenge_details_update"
350376
351377 # ic-dev21 Notify listeners
352378 for listener in self ._websocket_listeners :
@@ -478,13 +504,11 @@ async def on_websocket_event(self, event: WebsocketEvent) -> None:
478504 elif event .target == "Heartbeat" :
479505 self .validate_heartbeat (event )
480506
481- elif "Challenge" in event .target :
507+ elif "Challenge" in event .target or "Event" in event .target :
508+ LOG .debug ("HILO_DEBUG: Handling challenge/event websocket event: %s" , event )
482509 await self ._handle_challenge_events (event )
483510 await self ._handle_websocket_message (event )
484511
485- elif "Event" in event .target :
486- await self ._handle_websocket_message (event )
487-
488512 elif "Device" in event .target or event .target == "GatewayValuesReceived" :
489513 await self ._handle_device_events (event )
490514
@@ -504,36 +528,72 @@ async def subscribe_to_challenge(self, inv_id: int, event_id: int = 0) -> None:
504528 """Sends the json payload to receive updates from the challenge."""
505529 LOG .debug ("Subscribing to challenge : %s or %s" , event_id , self .challenge_id )
506530 event_id = event_id or self .challenge_id
507-
508- LOG .debug (
509- "Subscribing to challenge %s at location %s" ,
510- event_id ,
511- self .devices .location_id ,
512- )
531+ LOG .debug ("API URN is %s" , self ._api .urn )
532+ # Get plan name to connect to the correct challenge hub list
533+ tarif_config = self .hq_plan_name
534+ LOG .debug ("Event list needed is %s" , tarif_config )
535+
536+ # TODO: This is a fallback but will eventually need to be removed, I expect it to create
537+ # websocket disconnects once the split is complete.
538+ LOG .warning ("Not using plan name %s, falling back to default" , tarif_config )
513539 await self ._api .websocket_challenges .async_invoke (
514540 [{"locationId" : self .devices .location_id , "eventId" : event_id }],
515541 "SubscribeToChallenge" ,
516542 inv_id ,
517543 )
518544
545+ # Subscribe to the correct challenge hub
546+ if tarif_config == "rate d" :
547+ await self ._api .websocket_challenges .async_invoke (
548+ [{"locationHiloId" : self ._api .urn , "eventId" : event_id }],
549+ "SubscribeToEventCH" ,
550+ inv_id ,
551+ )
552+
553+ elif tarif_config == "flex d" :
554+ await self ._api .websocket_challenges .async_invoke (
555+ [{"locationHiloId" : self ._api .urn , "eventId" : event_id }],
556+ "SubscribeToEventFlex" ,
557+ inv_id ,
558+ )
559+ else :
560+ LOG .warning ("Unknown plan name %s, falling back to default" , tarif_config )
561+ await self ._api .websocket_challenges .async_invoke (
562+ [{"locationId" : self .devices .location_id , "eventId" : event_id }],
563+ "SubscribeToChallenge" ,
564+ inv_id ,
565+ )
566+
519567 @callback
520568 async def subscribe_to_challengelist (self , inv_id : int ) -> None :
521569 """Sends the json payload to receive updates from the challenge list."""
570+ # TODO : Rename challegenge functions to Event, fallback on challenge for now
522571 LOG .debug (
523572 "Subscribing to challenge list at location %s" , self .devices .location_id
524573 )
574+ LOG .debug ("API URN is %s" , self ._api .urn )
575+
525576 await self ._api .websocket_challenges .async_invoke (
526577 [{"locationId" : self .devices .location_id }],
527578 "SubscribeToChallengeList" ,
528579 inv_id ,
529580 )
530581
582+ LOG .debug ("Subscribing to event list at location %s" , self .devices .location_id )
583+ await self ._api .websocket_challenges .async_invoke (
584+ [{"locationHiloId" : self ._api .urn }],
585+ "SubscribeToEventList" ,
586+ inv_id ,
587+ )
588+
531589 @callback
532590 async def request_challenge_consumption_update (
533591 self , inv_id : int , event_id : int = 0
534592 ) -> None :
535593 """Sends the json payload to receive energy consumption updates from the challenge."""
536594 event_id = event_id or self .challenge_id
595+
596+ # TODO: Remove fallback once split is complete
537597 LOG .debug (
538598 "Requesting challenge %s consumption update at location %s" ,
539599 event_id ,
@@ -545,6 +605,41 @@ async def request_challenge_consumption_update(
545605 inv_id ,
546606 )
547607
608+ # Get plan name to request the correct consumption update
609+ tarif_config = self .hq_plan_name
610+ LOG .debug ("API URN is %s" , self ._api .urn )
611+ if tarif_config == "rate d" :
612+ LOG .debug (
613+ "Requesting event CH consumption update at location %s" ,
614+ self .devices .location_id ,
615+ )
616+ await self ._api .websocket_challenges .async_invoke (
617+ [{"locationHiloId" : self ._api .urn , "eventId" : event_id }],
618+ "RequestEventCHConsumptionUpdate" ,
619+ inv_id ,
620+ )
621+ elif tarif_config == "flex d" :
622+ LOG .debug (
623+ "Requesting event Flex consumption update at location %s" ,
624+ self .devices .location_id ,
625+ )
626+ await self ._api .websocket_challenges .async_invoke (
627+ [{"locationHiloId" : self ._api .urn , "eventId" : event_id }],
628+ "RequestEventFlexConsumptionUpdate" ,
629+ inv_id ,
630+ )
631+ else :
632+ LOG .debug (
633+ "Requesting challenge %s consumption update at location %s" ,
634+ event_id ,
635+ self .devices .location_id ,
636+ )
637+ await self ._api .websocket_challenges .async_invoke (
638+ [{"locationId" : self .devices .location_id , "eventId" : event_id }],
639+ "RequestChallengeConsumptionUpdate" ,
640+ inv_id ,
641+ )
642+
548643 @callback
549644 async def request_status_update (self ) -> None :
550645 await self ._api .websocket_devices .send_status ()
0 commit comments