Skip to content

Commit

Permalink
X1 Mini with password (#33)
Browse files Browse the repository at this point in the history
* X1 Mini with password

* fix linter

* fix Class method make_url should have 'cls' as first argument (bad-classmethod-argument)

* fix #32

support for pwd for InverterPost
fix #32

* pylint

* whitespace
  • Loading branch information
BoBiene authored Aug 3, 2021
1 parent f2650d1 commit 19ed75a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/dist
/solax.egg-info
.DS_Store
.eggs/**
/build/**
4 changes: 2 additions & 2 deletions solax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ async def rt_request(inv, retry, t_wait=0):
raise


async def real_time_api(ip_address, port=80):
i = await inverter.discover(ip_address, port)
async def real_time_api(ip_address, port=80, pwd=''):
i = await inverter.discover(ip_address, port, pwd)
return RealTimeAPI(i)


Expand Down
24 changes: 15 additions & 9 deletions solax/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class DiscoveryError(Exception):

class Inverter:
"""Base wrapper around Inverter HTTP API"""
def __init__(self, host, port):

def __init__(self, host, port, pwd=''):
self.host = host
self.port = port
self.pwd = pwd

async def get_data(self):
try:
data = await self.make_request(
self.host, self.port
self.host, self.port, self.pwd
)
except aiohttp.ClientError as ex:
msg = "Could not connect to inverter endpoint"
Expand All @@ -41,7 +43,7 @@ async def get_data(self):
return data

@classmethod
async def make_request(cls, host, port):
async def make_request(cls, host, port, pwd=''):
"""
Return instance of 'InverterResponse'
Raise exception if unable to get data
Expand Down Expand Up @@ -71,10 +73,10 @@ def map_response(resp_data, sensor_map):
}


async def discover(host, port) -> Inverter:
async def discover(host, port, pwd='') -> Inverter:
failures = []
for inverter in REGISTRY:
i = inverter(host, port)
i = inverter(host, port, pwd)
try:
await i.get_data()
return i
Expand Down Expand Up @@ -154,7 +156,7 @@ class XHybrid(Inverter):
}

@classmethod
async def make_request(cls, host, port=80):
async def make_request(cls, host, port=80, pwd=''):
base = 'http://{}:{}/api/realTimeData.htm'
url = base.format(host, port)
async with aiohttp.ClientSession() as session:
Expand Down Expand Up @@ -185,9 +187,13 @@ class InverterPost(Inverter):
# so we can disable the pylint warning
# pylint: disable=W0223
@classmethod
async def make_request(cls, host, port=80):
base = 'http://{}:{}/?optType=ReadRealTimeData'
url = base.format(host, port)
async def make_request(cls, host, port=80, pwd=''):
if not pwd:
base = 'http://{}:{}/?optType=ReadRealTimeData'
url = base.format(host, port)
else:
base = 'http://{}:{}/?optType=ReadRealTimeData&pwd={}&'
url = base.format(host, port, pwd)
async with aiohttp.ClientSession() as session:
async with session.post(url) as req:
resp = await req.read()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_base_inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ async def test_unimplemented_make_request():
await inverter.Inverter.make_request('localhost', 80)


@pytest.mark.asyncio
async def test_unimplemented_make_request_with_pwd():
with pytest.raises(NotImplementedError):
await inverter.Inverter.make_request('localhost', 80, 'pwd')


def test_unimplemented_sensor_map():
with pytest.raises(NotImplementedError):
inverter.Inverter.sensor_map()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ async def test_discovery_no_host():
await solax.real_time_api('localhost', 2)


@pytest.mark.asyncio
async def test_discovery_no_host_with_pwd():
with pytest.raises(inverter.DiscoveryError):
await solax.real_time_api('localhost', 2, 'pwd')


@pytest.mark.asyncio
async def test_discovery_unknown_webserver(simple_http_fixture):
with pytest.raises(inverter.DiscoveryError):
Expand Down

0 comments on commit 19ed75a

Please sign in to comment.