From 19ed75a49a14efa5e25fe4f8f98da9f59eb8cc2f Mon Sep 17 00:00:00 2001 From: Bo Biene <23037659+BoBiene@users.noreply.github.com> Date: Tue, 3 Aug 2021 04:58:02 +0200 Subject: [PATCH] X1 Mini with password (#33) * 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 --- .gitignore | 2 ++ solax/__init__.py | 4 ++-- solax/inverter.py | 24 +++++++++++++++--------- tests/test_base_inverter.py | 6 ++++++ tests/test_discovery.py | 6 ++++++ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 568683a..60f8519 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /dist /solax.egg-info .DS_Store +.eggs/** +/build/** diff --git a/solax/__init__.py b/solax/__init__.py index c376924..1d68fd1 100644 --- a/solax/__init__.py +++ b/solax/__init__.py @@ -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) diff --git a/solax/inverter.py b/solax/inverter.py index 40ab735..0a94f37 100644 --- a/solax/inverter.py +++ b/solax/inverter.py @@ -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" @@ -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 @@ -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 @@ -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: @@ -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() diff --git a/tests/test_base_inverter.py b/tests/test_base_inverter.py index 21607db..8d4a296 100644 --- a/tests/test_base_inverter.py +++ b/tests/test_base_inverter.py @@ -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() diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 5df3309..5c106ab 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -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):