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

Return none on empty values #30

Merged
merged 3 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ You need at least:
- Python 3.8+
- [Poetry][poetry-install]

Create a virtualenv and enter it:
To install all packages, including all development requirements:

```bash
python3 -m venv venv
source venv/bin/activate
poetry install
pre-commit install
```

To install all packages, including all development requirements:
Poetry creates by default an virtual environment where it installs all necessary pip packages, to enter or exit the venv run the following commands:

```bash
poetry install
pre-commit install
poetry shell
exit
```

As this repository uses the [pre-commit][pre-commit] framework, all changes
Expand Down
70 changes: 38 additions & 32 deletions omnikinverter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ def from_json(data: dict[str, Any]) -> Inverter:
An Inverter object.
"""

def get_value(search):
if data[search] != "":
return data[search]
return None

data = json.loads(data)
return Inverter(
serial_number=data["i_sn"],
model=data["i_modle"],
firmware=data["i_ver_m"],
firmware_slave=data["i_ver_s"],
solar_rated_power=data["i_pow"],
solar_current_power=int(data["i_pow_n"]),
solar_energy_today=float(data["i_eday"]),
solar_energy_total=float(data["i_eall"]),
serial_number=get_value("i_sn"),
model=get_value("i_modle"),
firmware=get_value("i_ver_m"),
firmware_slave=get_value("i_ver_s"),
solar_rated_power=get_value("i_pow"),
solar_current_power=int(get_value("i_pow_n")),
solar_energy_today=float(get_value("i_eday")),
solar_energy_total=float(get_value("i_eall")),
)

@staticmethod
Expand All @@ -56,39 +61,40 @@ def from_js(data: dict[str, Any]) -> Inverter:
An Inverter object.
"""

def get_values(position):
def get_value(position):
if data.find("webData") != -1:
matches = re.search(r'(?<=webData=").*?(?=";)', data)
else:
matches = re.search(r'(?<=myDeviceArray\[0\]=").*?(?=";)', data)

try:
data_list = matches.group(0).split(",")
if position in [4, 5, 6, 7]:
if position in [4, 5]:
return int(data_list[position])

if position == 6:
energy_value = float(data_list[position]) / 100
if position == 7:
energy_value = float(data_list[position]) / 10
return energy_value

return data_list[position].replace(" ", "")
if data_list[position] != "":
if position in [4, 5, 6, 7]:
if position in [4, 5]:
return int(data_list[position])

if position == 6:
energy_value = float(data_list[position]) / 100
if position == 7:
energy_value = float(data_list[position]) / 10
return energy_value
return data_list[position].replace(" ", "")
return None
except AttributeError as exception:
raise OmnikInverterWrongSourceError(
"Your inverter has no data source from a javascript file."
) from exception

return Inverter(
serial_number=get_values(0),
model=get_values(3),
firmware=get_values(1),
firmware_slave=get_values(2),
solar_rated_power=get_values(4),
solar_current_power=get_values(5),
solar_energy_today=get_values(6),
solar_energy_total=get_values(7),
serial_number=get_value(0),
model=get_value(3),
firmware=get_value(1),
firmware_slave=get_value(2),
solar_rated_power=get_value(4),
solar_current_power=get_value(5),
solar_energy_today=get_value(6),
solar_energy_total=get_value(7),
)


Expand Down Expand Up @@ -129,7 +135,7 @@ def from_js(data: dict[str, Any]) -> Device:
An Device object.
"""

def get_values(value_type):
def get_value(value_type):
if value_type == "ip" and data.find("wanIp") != -1:
match = re.search(r'(?<=wanIp=").*?(?=";)', data.replace(" ", ""))
value = match.group(0)
Expand All @@ -143,7 +149,7 @@ def get_values(value_type):
return value

return Device(
signal_quality=get_values("signal"),
firmware=get_values("version"),
ip_address=get_values("ip"),
signal_quality=get_value("signal"),
firmware=get_value("version"),
ip_address=get_value("ip"),
)
2 changes: 1 addition & 1 deletion omnikinverter/omnikinverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(

Args:
host: Hostname or IP address of the Omnik Inverter.
use_json: Boolean to confirm you use a JSON input.
use_json: Boolean to confirm you use a JSON source.
request_timeout: An integer with the request timeout in seconds.
session: Optional, shared, aiohttp client session.
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/status.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"g_ver":"VER:ME-111001-V1.0.6(2015-10-16)",
"ip":"192.168.0.10",
"i_sn":"12345678910",
"i_sn":"",
"i_ver_m":"V1.25Build23261",
"i_ver_s":"V1.40Build52927",
"i_modle":"omnik2000tl2",
"i_pow":"2000",
"i_pow":"",
"i_pow_n":1225,
"i_eday":"10.90",
"i_eall":"8674.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/status_devicearray.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var version= "H4.01.51MW.2.01W1.0.64(2018-01-251-D)";var m2mRssi= "39%";var wanIp= "192.168.0.10";var myDeviceArray=new Array();myDeviceArray[0]="12345678910,V4.08Build215,V4.12Build246,Omnik1500tl ,1500,850,232,52002,,1,";
var version= "H4.01.51MW.2.01W1.0.64(2018-01-251-D)";var m2mRssi= "39%";var wanIp= "192.168.0.10";var myDeviceArray=new Array();myDeviceArray[0]="12345678910,V4.08Build215,V4.12Build246,Omnik1500tl ,,850,232,52002,,1,";
2 changes: 1 addition & 1 deletion tests/fixtures/status_webdata.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var version="H4.01.38Y1.0.09W1.0.08";var m2mRssi="96%";var wanIp="192.168.0.10";var webData="12345678910,NL2-V9.8-5931,V5.3-00157,omnik2000tl2,2000,1010,488,105319,,4,";
var version="H4.01.38Y1.0.09W1.0.08";var m2mRssi="96%";var wanIp="192.168.0.10";var webData="12345678910,NL2-V9.8-5931,V5.3-00157,omnik2000tl2,,1010,488,105319,,4,";
5 changes: 4 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async def test_inverter_js_webdata(aresponses):
assert inverter.firmware == "NL2-V9.8-5931"
assert inverter.firmware_slave == "V5.3-00157"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_rated_power is None
assert inverter.solar_current_power == 1010
assert inverter.solar_energy_today == 4.88
assert inverter.solar_energy_total == 10531.9
Expand Down Expand Up @@ -79,6 +80,7 @@ async def test_inverter_js_devicearray(aresponses):
assert inverter.firmware == "V4.08Build215"
assert inverter.firmware_slave == "V4.12Build246"
assert inverter.model == "Omnik1500tl"
assert inverter.solar_rated_power is None
assert inverter.solar_current_power == 850
assert inverter.solar_energy_today == 2.32
assert inverter.solar_energy_total == 5200.2
Expand Down Expand Up @@ -125,10 +127,11 @@ async def test_inverter_json(aresponses):
omnik = OmnikInverter(host="example.com", use_json=True, session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.serial_number == "12345678910"
assert inverter.serial_number is None
assert inverter.firmware == "V1.25Build23261"
assert inverter.firmware_slave == "V1.40Build52927"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_rated_power is None
assert inverter.solar_current_power == 1225
assert inverter.solar_energy_today == 10.90
assert inverter.solar_energy_total == 8674.0
Expand Down