@@ -19,7 +19,8 @@ def __init__(
1919 appID ,
2020 appSecret ,
2121 session : aiohttp .ClientSession | None = None ,
22- timeout : int = 30
22+ timeout : int = 30 ,
23+ ipaddress = None
2324 ) -> None :
2425 """Initialize."""
2526 self .appID = appID
@@ -31,6 +32,7 @@ def __init__(
3132 self .session = session or aiohttp .ClientSession ()
3233 self ._created_session = not session
3334 self .timeout = timeout
35+ self .ipaddress = ipaddress
3436
3537 async def close (self ) -> None :
3638 """Close the AlphaESS API client."""
@@ -312,6 +314,31 @@ async def updateDisChargeConfigInfo(self, sysSn, batUseCap, ctrDis, timeDise1, t
312314 except Exception as e :
313315 logger .error (f"Error: { e } when calling { resource } " )
314316
317+ async def getIPData (self ) -> Optional (dict ):
318+ ENDPOINTS = {
319+ "status" : "/config?command=status" ,
320+ "device_info" : "/config?command=devinfo"
321+ }
322+
323+ async with aiohttp .ClientSession () as session :
324+ tasks = []
325+ for name , path in ENDPOINTS .items ():
326+ url = f"http://{ self .ipaddress } { path } "
327+ tasks .append (self ._fetch (session , name , url ))
328+ results = await asyncio .gather (* tasks )
329+ return dict (results )
330+
331+ @staticmethod
332+ async def _fetch (session , name , url ):
333+ try :
334+ async with session .get (url ) as response :
335+ response .raise_for_status ()
336+ data = await response .json (content_type = None )
337+ return name , data
338+ except Exception as e :
339+ print (f"Failed to fetch { name } from { url } : { e } " )
340+ return name , None
341+
315342 async def api_get (self , path , json = None ) -> Optional (list ):
316343 """Retrieve ESS list by serial number from Alpha ESS"""
317344 if json is None :
@@ -379,6 +406,15 @@ async def getdata(self, get_power=False, get_ev=False, self_delay=0) -> Optional
379406 """Get All Data For All serial numbers from Alpha ESS"""
380407 try :
381408 alldata = []
409+ if self .ipaddress :
410+ ip_data = await self .getIPData ()
411+ if ip_data :
412+ # Wrap it like a unit and add to alldata
413+ alldata .append ({
414+ "type" : "local_ip_data" ,
415+ "ip" : self .ipaddress ,
416+ ** ip_data # merge status and device_info keys
417+ })
382418 units = await self .getESSList ()
383419 for unit in units :
384420 if "sysSn" in unit :
@@ -407,7 +443,6 @@ async def getdata(self, get_power=False, get_ev=False, self_delay=0) -> Optional
407443 unit ['EVCurrent' ] = await self .getEvChargerCurrentsBySn (serial )
408444 except Exception :
409445 pass
410-
411446 alldata .append (unit )
412447 logger .debug (alldata )
413448 return alldata
0 commit comments