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

Beta (2.8.x) #385

Open
wants to merge 18 commits into
base: Beta-(2.8.x)
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Corrected a bug in the generation of configuration.
Thx @Maes152 for reporting.
- Added unit tests
egguy authored and ChimpDW committed Oct 7, 2023
commit 0f3999d842224e26a346891308649d599cc06dd5
54 changes: 49 additions & 5 deletions examples/Home Assistent/grott_ha.py
Original file line number Diff line number Diff line change
@@ -8,13 +8,16 @@

from grottconf import Conf

__version__ = "0.0.7-rc2"
__version__ = "0.0.7-rc3"

"""A pluging for grott
This plugin allow to have autodiscovery of the device in HA

Should be able to support multiples inverters

Version 0.0.7
- Corrected a bug when creating the configuration

Config:
- ha_mqtt_host (required): The host of the MQTT broker user by HA (often the IP of HA)
- ha_mqtt_port (required): The port (the default is oftent 1883)
@@ -514,14 +517,13 @@ def make_payload(conf: Conf, device: str, name: str, key: str, unit: str = None)
layout = conf.recorddict[conf.layout]
if "value_template" not in payload and key in layout:
# From grottdata:207, default type is num, also process numx
if layout[key].get("type", "num") in ("num", "numx") and layout[key].get(
"divide", "1"
):
if layout[key].get("type", "num") in ("num", "numx"):
divider = layout[key].get("divide", "1")
payload[
"value_template"
] = "{{{{ value_json.{key} | float / {divide} }}}}".format(
key=key,
divide=layout[key].get("divide"),
divide=divider,
)

if "value_template" not in payload:
@@ -682,3 +684,45 @@ def grottext(conf: Conf, data: str, jsonmsg: str):
# Reset connection state in case of problem
return 2
return 0


def test_generate_payload():
"Test that an auto generated payload for MQTT configuration"

class TestConf():
recorddict = {
"test": {
"pvpowerout": {"value" :122, "length" : 4, "type" : "num", "divide" : 10}
}
}
layout = "test"

payload = make_payload(TestConf(), "NCO7410", "pvpowerout", "pvpowerout")
print(payload)
# The default divider for pvpowerout is 10
assert payload["value_template"] == "{{ value_json.pvpowerout | float / 10 }}"
assert payload["name"] == "NCO7410 PV Output (Actual)"
assert payload["unique_id"] == "grott_NCO7410_pvpowerout"
assert payload["state_class"] == "measurement"
assert payload["device_class"] == "power"
assert payload["unit_of_measurement"] == "W"

def test_generate_payload_without_divider():
"Test that an auto generated payload for MQTT configuration"
class TestConf():
recorddict = {
"test": {
"pvpowerout": {"value" :122, "length" : 4, "type" : "num", }
}
}
layout = "test"

payload = make_payload(TestConf(), "NCO7410", "pvpowerout", "pvpowerout")
print(payload)
# The default divider for pvpowerout is 10
assert payload["value_template"] == "{{ value_json.pvpowerout | float / 1 }}"
assert payload["name"] == "NCO7410 PV Output (Actual)"
assert payload["unique_id"] == "grott_NCO7410_pvpowerout"
assert payload["state_class"] == "measurement"
assert payload["device_class"] == "power"
assert payload["unit_of_measurement"] == "W"