Skip to content

Commit

Permalink
Merge pull request #58 from kpetremann/hubitat_support
Browse files Browse the repository at this point in the history
Hubitat support
  • Loading branch information
kpetremann authored Jun 30, 2023
2 parents 86a6dee + 1e0240c commit 24250ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
26 changes: 26 additions & 0 deletions mqtt_exporter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ def _normalize_esphome_format(topic, payload):
return topic, payload_dict


def _normalize_hubitat_format(topic, payload):
"""Normalize hubitat format.
Example:
hubitat/hub1/some room/temperature/value
"""
info = topic.split("/")

if len(info) < 3:
return topic, payload

topic = f"{info[0].lower()}_{info[1].lower()}_{info[2].lower()}"
payload_dict = {info[-2]: payload}
return topic, payload_dict


def _is_esphome_topic(topic):
for prefix in settings.ESPHOME_TOPIC_PREFIXES:
if prefix and topic.startswith(prefix):
Expand All @@ -259,6 +275,14 @@ def _is_esphome_topic(topic):
return False


def _is_hubitat_topic(topic):
for prefix in settings.HUBITAT_TOPIC_PREFIXES:
if prefix and topic.startswith(prefix):
return True

return False


def _parse_message(raw_topic, raw_payload):
"""Parse topic and payload to have exposable information."""
# parse MQTT payload
Expand All @@ -273,6 +297,8 @@ def _parse_message(raw_topic, raw_payload):

if raw_topic.startswith(settings.ZWAVE_TOPIC_PREFIX):
topic, payload = _normalize_zwave2mqtt_format(raw_topic, payload)
elif _is_hubitat_topic(raw_topic):
topic, payload = _normalize_hubitat_format(raw_topic, payload)
elif _is_esphome_topic(raw_topic):
topic, payload = _normalize_esphome_format(raw_topic, payload)
elif not isinstance(payload, dict):
Expand Down
1 change: 1 addition & 0 deletions mqtt_exporter/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
IGNORED_TOPICS = os.getenv("MQTT_IGNORED_TOPICS", "").split(",")
ZWAVE_TOPIC_PREFIX = os.getenv("ZWAVE_TOPIC_PREFIX", "zwave/")
ESPHOME_TOPIC_PREFIXES = os.getenv("ESPHOME_TOPIC_PREFIXES", "").split(",")
HUBITAT_TOPIC_PREFIXES = os.getenv("HUBITAT_TOPIC_PREFIXES", "hubitat/").split(",")

ZIGBEE2MQTT_AVAILABILITY = os.getenv("ZIGBEE2MQTT_AVAILABILITY", "False") == "True"
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
Expand Down
16 changes: 15 additions & 1 deletion tests/functional/test_parse_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_parse_message__zwave_js__payload_not_dict():


def test__parse_message__esphome_style():
"""Test message parsing with espthome style.
"""Test message parsing with esphome style.
Same format for SONOFF sensors.
"""
Expand All @@ -163,3 +163,17 @@ def test__parse_message__esphome_style():

assert parsed_topic == "esphome_indoor"
assert parsed_payload == {"temperature": 22.0}


def test__parse_message__hubitat_style():
"""Test message parsing with Hubitat style.
It looks like: hubitat/[hubname]/[device name]/attributes/[attribute name]/value
"""
topic = "hubitat/hub1/some_room/attributes/temperature/value"
payload = "20.0"

parsed_topic, parsed_payload = _parse_message(topic, payload)

assert parsed_topic == "hubitat_hub1_some_room"
assert parsed_payload == {"temperature": 20.0}

0 comments on commit 24250ba

Please sign in to comment.