Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update inverter.py #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 65 additions & 11 deletions solax/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ class DiscoveryError(Exception):
class Inverter:
"""Base wrapper around Inverter HTTP API"""

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

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

@classmethod
async def make_request(cls, host, port, pwd=''):
async def make_request(cls, host, port, pwd='', xffheader=False):
"""
Return instance of 'InverterResponse'
Raise exception if unable to get data
Expand Down Expand Up @@ -88,10 +89,17 @@ def map_response(cls, resp_data):
return result


async def discover(host, port, pwd='') -> Inverter:
async def discover(host, port, pwd='', xffheader=False) -> Inverter:
failures = []
for inverter in REGISTRY:
i = inverter(host, port, pwd)
i = inverter(host, port, pwd, xffheader)
try:
await i.get_data()
return i
except InverterError as ex:
failures.append(ex)
xffheader=True
i = inverter(host, port, pwd, xffheader)
try:
await i.get_data()
return i
Expand Down Expand Up @@ -171,12 +179,17 @@ class XHybrid(Inverter):
}

@classmethod
async def make_request(cls, host, port=80, pwd=''):
async def make_request(cls, host, port=80, pwd='', xffheader=False):
base = 'http://{}:{}/api/realTimeData.htm'
url = base.format(host, port)
headers = {'X-Forwarded-For' : '5.8.8.8'}
async with aiohttp.ClientSession() as session:
async with session.get(url) as req:
garbage = await req.read()
if xffheader:
async with session.get(url, headers=headers) as req:
garbage = await req.read()
else:
async with session.get(url) as req:
garbage = await req.read()
formatted = garbage.decode("utf-8")
formatted = formatted.replace(",,", ",0.0,").replace(",,", ",0.0,")
json_response = json.loads(formatted)
Expand All @@ -202,16 +215,21 @@ class InverterPost(Inverter):
# so we can disable the pylint warning
# pylint: disable=W0223
@classmethod
async def make_request(cls, host, port=80, pwd=''):
async def make_request(cls, host, port=80, pwd='', xffheader=False):
if not pwd:
base = 'http://{}:{}/?optType=ReadRealTimeData'
url = base.format(host, port)
else:
base = 'http://{}:{}/?optType=ReadRealTimeData&pwd={}&'
url = base.format(host, port, pwd)
headers = {'X-Forwarded-For' : '5.8.8.8'}
async with aiohttp.ClientSession() as session:
async with session.post(url) as req:
resp = await req.read()
if xffheader:
async with session.post(url, headers=headers) as req:
resp = await req.read()
else:
async with session.post(url) as req:
resp = await req.read()
raw_json = resp.decode("utf-8")
json_response = json.loads(raw_json)
response = {}
Expand Down Expand Up @@ -478,6 +496,42 @@ class X1(InverterPost):
'EPS Frequency': (56, 'Hz'),
}

@classmethod
async def make_request(cls, host, port=80, pwd='', xffheader=False):
headers = {'X-Forwarded-For': '5.8.8.8'}
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:
if xffheader:
async with session.post(url, headers=headers) as req:
resp = await req.read()
else:
async with session.post(url) as req:
resp = await req.read()
raw_json = resp.decode("utf-8")
json_response = json.loads(raw_json)
response = {}
try:
response = cls.schema()(json_response)
except (Invalid, MultipleInvalid) as ex:
_ = humanize_error(json_response, ex)
# print(_)
raise
if 'SN' in response:
serial_number = response['SN']
else:
serial_number = response['sn']
return InverterResponse(
data=cls.map_response(response['Data']),
serial_number=serial_number,
version=response['ver'],
type=response['type']
)

@classmethod
def sensor_map(cls):
return cls.__sensor_map
Expand Down