11"""
22Data coordinator for pod point client
33"""
4+
45import logging
56from typing import Dict , List , Set , Tuple
67
8+ import pytz
79from homeassistant .core import HomeAssistant
810from homeassistant .exceptions import ConfigEntryAuthFailed
911from homeassistant .helpers import issue_registry as ir
1416from podpointclient .pod import Firmware , Pod
1517from podpointclient .user import User
1618
19+ from datetime import datetime , timedelta
20+
1721from .const import DOMAIN , LIMITED_POD_INCLUDES
1822
1923_LOGGER : logging .Logger = logging .getLogger (__package__ )
@@ -25,10 +29,7 @@ class PodPointDataUpdateCoordinator(DataUpdateCoordinator):
2529 _firmware_refresh_interval = 5 # How many refreshes between a firmware update call
2630
2731 def __init__ (
28- self ,
29- hass : HomeAssistant ,
30- client : PodPointClient ,
31- scan_interval : int
32+ self , hass : HomeAssistant , client : PodPointClient , scan_interval : timedelta
3233 ) -> None :
3334 """Initialize."""
3435 self .api : PodPointClient = client
@@ -45,6 +46,7 @@ def __init__(
4546 self .online = None
4647 self .firmware_refresh = 1 # Initial refresh will be a firmware refresh too, ensuring we pull firmware for all pods at startup
4748 self .user : User = None
49+ self .last_message_at = datetime (1970 , 1 , 1 , 0 , 0 , 0 , 0 , pytz .UTC )
4850
4951 super ().__init__ (hass , _LOGGER , name = DOMAIN , update_interval = scan_interval )
5052
@@ -56,6 +58,7 @@ async def _async_update_data(self):
5658 self .pod_dict : Dict [int , Pod ] = None
5759
5860 self .user = await self .api .async_get_user ()
61+
5962 new_pods = await self .__async_update_pods ()
6063
6164 _LOGGER .debug (
@@ -68,14 +71,23 @@ async def _async_update_data(self):
6871 # they were performed on
6972 new_pods_by_id = self .__group_pods_by_unit_id (pods = new_pods )
7073
71- (new_pods , new_pods_by_id ) = await self .__async_group_pods (new_pods , new_pods_by_id )
74+ (new_pods , new_pods_by_id ) = await self .__async_group_pods (
75+ new_pods , new_pods_by_id
76+ )
7277
7378 new_pods_by_id = self .__group_pods_by_unit_id (pods = new_pods )
7479
7580 # Fetch firmware data for pods, if it is needed
7681 self .firmware_refresh -= 1
7782 if self .firmware_refresh <= 0 :
78- new_pods_by_id = await self .__async_refresh_firmware (new_pods , new_pods_by_id )
83+ new_pods_by_id = await self .__async_refresh_firmware (
84+ new_pods , new_pods_by_id
85+ )
86+
87+ # Fetch connection status data for pods
88+ new_pods_by_id = await self .__async_update_pod_connection_status (
89+ new_pods_by_id
90+ )
7991
8092 # Determine if we should fetch for all charges, or just the most recent for a user.
8193 should_fetch_all_charges = self .__should_fetch_all_charges (
@@ -302,17 +314,13 @@ async def __async_update_pods(self) -> List[Pod]:
302314 # Should we get a limited set of data (subsiquent refreshes)
303315 if len (self .pods ) > 0 :
304316 _LOGGER .debug ("Existing pods found, performing a limited data pull" )
305- return await self .api .async_get_all_pods (
306- includes = LIMITED_POD_INCLUDES
307- )
317+ return await self .api .async_get_all_pods (includes = LIMITED_POD_INCLUDES )
308318 else :
309319 _LOGGER .debug ("No existing pods found, performing a full data pull" )
310320 return await self .api .async_get_all_pods ()
311321
312322 async def __async_group_pods (
313- self ,
314- new_pods ,
315- new_pods_by_id
323+ self , new_pods , new_pods_by_id
316324 ) -> Tuple [List [Pod ], Dict [str , Pod ]]:
317325 # Attempt to update our new pods with additional data from the existing pods.
318326 # This allows us to query less data each refresh, kinder on the Pod Point APIs.
@@ -332,16 +340,12 @@ async def __async_group_pods(
332340 return (new_pods , new_pods_by_id )
333341
334342 async def __async_refresh_firmware (
335- self ,
336- new_pods : List [Pod ],
337- new_pods_by_id : Dict [str , List [Pod ]]
343+ self , new_pods : List [Pod ], new_pods_by_id : Dict [str , List [Pod ]]
338344 ) -> Dict [str , List [Pod ]]:
339345 _LOGGER .debug ("=== FIRMWARE STATUS UPDATE ===" )
340346
341347 for pod in new_pods :
342- pod_firmwares : List [Firmware ] = await self .api .async_get_firmware (
343- pod = pod
344- )
348+ pod_firmwares : List [Firmware ] = await self .api .async_get_firmware (pod = pod )
345349
346350 if len (pod_firmwares ) <= 0 :
347351 _LOGGER .warning (
@@ -361,3 +365,33 @@ async def __async_refresh_firmware(
361365 self .firmware_refresh = self ._firmware_refresh_interval
362366
363367 return new_pods_by_id
368+
369+ async def __async_update_pod_connection_status (
370+ self , new_pods_by_id : Dict [str , List [Pod ]]
371+ ) -> Dict [str , List [Pod ]]:
372+ _LOGGER .debug ("=== POD CONNECTION STATUS UPDATE ===" )
373+
374+ # flat_pods = [item for row in new_pods_by_id.values() for item in row]
375+ # Fetch connection status for each pod
376+ for pod in new_pods_by_id .values ():
377+ connectivity_status = await self .api .async_get_connectivity_status (pod = pod )
378+
379+ if connectivity_status is not None :
380+ pod .connectivity_status = connectivity_status
381+ pod .last_message_at = connectivity_status .last_message_at
382+ pod .charging_state = connectivity_status .charging_state
383+
384+ pod .statuses .append (
385+ Pod .Status (
386+ 99 ,
387+ connectivity_status .charging_state ,
388+ connectivity_status .charging_state ,
389+ connectivity_status .charging_state ,
390+ "A" ,
391+ 1 ,
392+ )
393+ )
394+
395+ new_pods_by_id [pod .unit_id ] = pod
396+
397+ return new_pods_by_id
0 commit comments