Skip to content

Commit

Permalink
Add enabled attribute to check sensor availability
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericvl committed Apr 30, 2020
1 parent b531ebb commit 9af331d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
57 changes: 28 additions & 29 deletions pysaj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, key, csv_1_key, csv_2_key, factor, name, unit='',
self.per_day_basis = per_day_basis
self.per_total_basis = per_total_basis
self.date = date.today()
self.enabled = False


class Sensors(object):
Expand All @@ -57,13 +58,10 @@ def __init__(self, wifi=False):
Sensor("CO2", 21, 33, "/10", "total_co2_reduced", "kg", False,
True),
Sensor("temp", 20, 32, "/10", "temperature", "°C"),
Sensor("state", 22, 34, "", "state")
)
)
if not wifi:
self.add(
Sensor("state", 22, 34, "", "state"),
Sensor("maxPower", -1, -1, "", "today_max_current", "W", True)
)
)

def __len__(self):
"""Length."""
Expand Down Expand Up @@ -161,6 +159,7 @@ async def read(self, sensors):
current_url = self.url
async with session.get(current_url) as response:
data = await response.text()
at_least_one_enabled = False

if self.wifi:
csv_data = StringIO(data)
Expand All @@ -177,12 +176,18 @@ async def read(self, sensors):
for sen in sensors:
if ncol < 24:
if sen.csv_1_key != -1:
v = values[sen.csv_1_key]
try:
v = values[sen.csv_1_key]
except IndexError:
v = None
else:
v = None
else:
if sen.csv_2_key != -1:
v = values[sen.csv_2_key]
try:
v = values[sen.csv_2_key]
except IndexError:
v = None
else:
v = None

Expand All @@ -194,18 +199,28 @@ async def read(self, sensors):
"{0}{1}".format(v, sen.factor)
)
sen.date = date.today()
sen.enabled = True
at_least_one_enabled = True
else:
xml = ET.fromstring(data)

for sen in sensors:
find = xml.find(sen.key)
if find is None:
raise KeyError
sen.value = find.text
sen.date = date.today()
if find is not None:
sen.value = find.text
sen.date = date.today()
sen.enabled = True
at_least_one_enabled = True

if not at_least_one_enabled:
if self.wifi:
raise csv.Error
else:
raise ET.ParseError

_LOGGER.debug("Got new value for sensor %s: %s",
sen.name, sen.value)
if sen.enabled:
_LOGGER.debug("Got new value for sensor %s: %s",
sen.name, sen.value)

return True
except (aiohttp.client_exceptions.ClientConnectorError,
Expand Down Expand Up @@ -236,22 +251,6 @@ async def read(self, sensors):
str.format("No valid XML received from {0} at {1}", self.host,
current_url)
)
except KeyError:
# XML received does not have all the required elements
raise UnexpectedResponseException(
str.format("SAJ sensor key {0} not found, inverter not " +
"compatible?", sen.key)
)
except IndexError:
# CSV received does not have all the required elements
raise UnexpectedResponseException(
str.format(
"SAJ sensor name {0} at CSV position {1} not found, " +
"inverter not compatible?",
sen.name,
sen.csv_1_key if ncol < 24 else sen.csv_2_key
)
)


class UnauthorizedException(Exception):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pysaj",
version="0.0.14",
version="0.0.16",
author="fredericvl",
author_email="[email protected]",
description="Library to communicate with SAJ inverters",
Expand Down

0 comments on commit 9af331d

Please sign in to comment.