Skip to content

Commit 0a52608

Browse files
Merge pull request #16 from Poshy163/local-api-support
2 parents 4eabe18 + 7a61b88 commit 0a52608

4 files changed

Lines changed: 46 additions & 6 deletions

File tree

alphaess/alphaess.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

apitest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
if len(sys.argv) != 3:
88
appID = input("AppID: ")
99
appSecret = input("AppSecret: ")
10+
IPAddress = input("IP Address (blank if not using): ") or None
1011
else:
1112
appID = sys.argv[1]
1213
appSecret = sys.argv[2]
14+
IPAddress = sys.argv[3]
1315

1416
logger = logging.getLogger('')
1517
logger.setLevel(logging.DEBUG)
@@ -19,11 +21,14 @@
1921
handler.setFormatter(formatter)
2022
logger.addHandler(handler)
2123

24+
if sys.platform.startswith("win"):
25+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
26+
2227

2328
async def main():
2429
logger.debug("instantiating Alpha ESS Client")
2530
try:
26-
client: alphaess = alphaess(appID, appSecret)
31+
client: alphaess = alphaess(appID, appSecret, ipaddress=IPAddress)
2732
ESSList = await client.getESSList()
2833
for unit in ESSList:
2934
if "sysSn" in unit:

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
aiohttp~=3.11.13
1+
aiohttp~=3.12.13
22
voluptuous~=0.15.2
3-
setuptools~=75.8.2
3+
setuptools~=80.9.0

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="alphaessopenapi",
8-
version="0.0.13",
8+
version="0.0.14",
99
author="Charles Gillanders",
1010
author_email="charles@charlesgillanders.com",
1111
description="A python library to retrieve energy statistics from your Alpha ESS inverter by polling the Official Alpha ESS Open API.",

0 commit comments

Comments
 (0)