Skip to content

Commit

Permalink
hassd tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jblance committed Jan 10, 2023
1 parent 670bc0e commit 1b02a15
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 61 deletions.
11 changes: 11 additions & 0 deletions daly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# sample yaml file for testing daly protocol
port:
path: /dev/ttyUSB0
type: test
baud: 2400
protocol: DALY
commands:
- command: cellVoltages
outputs:
- name: screen
loop: 10
3 changes: 2 additions & 1 deletion mppsolar/outputs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_outputs(output_list):
return ops


def output_results(results, command, mqtt_broker):
def output_results(results, command, mqtt_broker, fullconfig={}):
# "normal command definition"
# - command: QPIGS
# outputs:
Expand Down Expand Up @@ -94,6 +94,7 @@ def output_results(results, command, mqtt_broker):
data=dict(results),
config=op,
mqtt_broker=mqtt_broker,
fullconfig=fullconfig,
)


Expand Down
149 changes: 104 additions & 45 deletions mppsolar/outputs/hassd_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,122 @@ def __init__(self, *args, **kwargs) -> None:
log.debug(f"__init__: kwargs {kwargs}")

def build_msgs(self, *args, **kwargs):
log.debug(f"kwargs {kwargs}")

data = get_kwargs(kwargs, "data")
tag = get_kwargs(kwargs, "tag")
keep_case = get_kwargs(kwargs, "keep_case")
# Clean data
if "_command" in data:
command = data.pop("_command")
if "_command_description" in data:
data.pop("_command_description")
if "raw_response" in data:
data.pop("raw_response")

# check if config supplied
config = get_kwargs(kwargs, "config")
if config is not None:
log.debug(f"config: {config}")
# get results topic
# results_topic = config.get("results_topic", None)
# get formatting info
remove_spaces = config.get("remove_spaces", True)
keep_case = config.get("keep_case", False)
filter = config.get("filter", None)
excl_filter = config.get("excl_filter", None)
tag = config.get("tag", None)
# device_name =
else:
# results_topic = None
# get formatting info
remove_spaces = True
keep_case = get_kwargs(kwargs, "keep_case")
filter = get_kwargs(kwargs, "filter")
excl_filter = get_kwargs(kwargs, "excl_filter")
tag = get_kwargs(kwargs, "tag")
device_name = get_kwargs(kwargs, "name", "mppsolar")

filter = get_kwargs(kwargs, "filter")
if filter is not None:
filter = re.compile(filter)
excl_filter = get_kwargs(kwargs, "excl_filter")
if excl_filter is not None:
excl_filter = re.compile(excl_filter)
if tag is None:
tag = command

# Build array of mqtt messages with hass update format
# assumes hass_config has been run
# or hass updated manually
msgs = []
# Remove command and _command_description
data.pop("_command", None)
data.pop("_command_description", None)
data.pop("raw_response", None)
# build topic prefix
# if results_topic is not None:
# topic_prefix = results_topic
# else:
# topic_prefix = f"{tag}/status"

# Loop through responses
for _key in data:
value = data[_key][0]
unit = data[_key][1]
# build data to output
_data = {}
for key in data:
_values = data[key]
# remove spaces
key = _key.replace(" ", "_")
if remove_spaces:
key = key.replace(" ", "_")
if not keep_case:
# make lowercase
key = key.lower()
if key_wanted(key, filter, excl_filter):
#
# CONFIG / AUTODISCOVER
#
# <discovery_prefix>/<component>/[<node_id>/]<object_id>/config
# topic "homeassistant/binary_sensor/garden/config"
# msg '{"name": "garden", "device_class": "motion", "state_topic": "homeassistant/binary_sensor/garden/state", "unit_of_measurement": "°C"}'
topic = f"homeassistant/sensor/mpp_{tag}_{key}/config"
topic = topic.replace(" ", "_")
name = f"{tag} {_key}"
payload = {"name": f"{name}", "state_topic": f"homeassistant/sensor/mpp_{tag}_{key}/state", "unit_of_measurement": f"{unit}", "unique_id": f"mpp_{tag}_{key}", "force_update": "true"}
# payload["device"] = {"name": f"{device_name}", "identifiers": ["mppsolar"], "model": "PIP6048MAX", "manufacturer": "MPP-Solar"}
payload["device"] = {"name": f"{device_name}", "identifiers": [f"{device_name}"], "model": f"{device_name}", "manufacturer": "MPP-Solar"}
if unit == "W":
payload.update({"state_class": "measurement", "device_class": "power"})
# msg = {"topic": topic, "payload": payload, "retain": True}
payloads = js.dumps(payload)
# print(payloads)
msg = {"topic": topic, "payload": payloads}
msgs.append(msg)
#
# VALUE SETTING
#
# unit = data[key][1]
# 'tag'/status/total_output_active_power/value 1250
# 'tag'/status/total_output_active_power/unit W
topic = f"homeassistant/sensor/mpp_{tag}_{key}/state"
msg = {"topic": topic, "payload": value}
msgs.append(msg)
_data[key] = _values
log.debug(f"output data: {_data}")

# Build array of mqtt messages with hass update format
# assumes hass_config has been run
# or hass updated manually
msgs = []

# Loop through responses
for _key in _data:
value = _data[_key][0]
unit = _data[_key][1]

#
# CONFIG / AUTODISCOVER
#
# <discovery_prefix>/<component>/[<node_id>/]<object_id>/config
# topic "homeassistant/binary_sensor/garden/config"
# msg '{"name": "garden", "device_class": "motion", "state_topic": "homeassistant/binary_sensor/garden/state", "unit_of_measurement": "°C"}'

# For binary sensors
if unit == "bool":
sensor = "binary_sensor"
else:
sensor = "sensor"
topic = f"homeassistant/{sensor}/mpp_{tag}_{_key}/config"
topic = topic.replace(" ", "_")
name = f"{tag} {_key}"
payload = {
"name": f"{name}",
"state_topic": f"homeassistant/{sensor}/mpp_{tag}_{_key}/state",
"unique_id": f"mpp_{tag}_{_key}",
"force_update": "true",
}
if unit and unit != "bool":
payload["unit_of_measurement"] = f"{unit}"

# payload["device"] = {"name": f"{device_name}", "identifiers": ["mppsolar"], "model": "PIP6048MAX", "manufacturer": "MPP-Solar"}
payload["device"] = {
"name": f"{device_name}",
"identifiers": [f"{device_name}"],
"model": f"{device_name}",
"manufacturer": "MPP-Solar",
}
if unit == "W":
payload.update({"state_class": "measurement", "device_class": "power"})
# msg = {"topic": topic, "payload": payload, "retain": True}
payloads = js.dumps(payload)
# print(payloads)
msg = {"topic": topic, "payload": payloads}
msgs.append(msg)
#
# VALUE SETTING
#
# unit = data[key][1]
# 'tag'/status/total_output_active_power/value 1250
# 'tag'/status/total_output_active_power/unit W
topic = f"homeassistant/{sensor}/mpp_{tag}_{_key}/state"
msg = {"topic": topic, "payload": value}
msgs.append(msg)
return msgs
2 changes: 2 additions & 0 deletions mppsolar/powermon.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def main():
results=results,
command=config["mqttbroker"]["adhoc_commands"],
mqtt_broker=mqtt_broker,
fullconfig=config,
)
# process 'normal' commands
if not inDelay:
Expand All @@ -217,6 +218,7 @@ def main():
results=results,
command=command,
mqtt_broker=mqtt_broker,
fullconfig=config,
)
# pause
# pause_time = config["command_pause"]
Expand Down
32 changes: 17 additions & 15 deletions powermon.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
device:
name: Inverter_2
id: 123456789
model: 8048MAX
manufacturer: MPP-Solar
port:
path: /dev/ttyUSB0
type: test
baud: 2400
protocol: PI30
protocol: PI30MAX
mqttbroker:
name: null
name: screen
port: 1883
user: null
pass: null
adhoc_commands:
topic: test/command_topic
commands:
- command: QPIGS
repeat_period: 10s
outputs:
- name: screen
- name: mqtt
results_topic: results/qpigs
# - command: QPIGS
# outputs:
# - name: screen
# - name: mqtt
# results_topic: results/qpigs
- command: QED
f_command: f'QED{(date.today()-timedelta(days=1)).strftime("%Y%m%d")}'
repeat_period: 1h
outputs:
- name: screen
- command: QPIRI
repeat_period: 1m
outputs:
- name: screen
tag: testtag
filter: volt
- name: hassd_mqtt
# - command: QPIRI
# outputs:
# - name: screen
# filter: volt

0 comments on commit 1b02a15

Please sign in to comment.