Skip to content

Commit

Permalink
Merge branch 'yozik04-master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	requirements.txt
  • Loading branch information
ehabets committed Jul 29, 2024
2 parents 184d424 + 957fdd7 commit d1ee5e0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Publish to Docker Hub

on:
workflow_dispatch:
push:
branches:
- master
Expand Down
2 changes: 1 addition & 1 deletion nibe_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cfg = Config()

__version__ = "0.4.0"
__version__ = "0.5.1"
3 changes: 2 additions & 1 deletion nibe_mqtt/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def publish_coil_state(self, coil_data: CoilData):
retain=self._conf["retain_state"],
)

def publish_discovery(self, coil: Coil, device_info: dict):
def publish_discovery(self, coil_data: CoilData, device_info: dict):
coil = coil_data.coil
component = "sensor"

device_id = device_info.get("id")
Expand Down
37 changes: 31 additions & 6 deletions nibe_mqtt/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import logging
from datetime import datetime, timedelta
from pathlib import Path
import re
from typing import Set, Union

from nibe.coil import Coil, CoilData
from nibe.connection import Connection
from nibe.exceptions import WriteException, CoilNotFoundException
from nibe.exceptions import WriteException, CoilNotFoundException, NoMappingException
from nibe.heatpump import HeatPump
from slugify import slugify

Expand All @@ -18,6 +19,7 @@

class Service(MqttHandler):
announced_coils: Set[Coil]
re_unknown_value = re.compile(r"UNKNOWN \((\d+)\)")

def __init__(self, conf: dict):
self.conf = conf
Expand Down Expand Up @@ -115,14 +117,37 @@ async def start(self):
self.mqtt_client.start()

def on_coil_update(self, coil_data: CoilData):
if coil_data.coil not in self.announced_coils:
self.mqtt_client.publish_discovery(coil_data.coil, self._get_device_info())
self.announced_coils.add(coil_data.coil)
coil = coil_data.coil
if coil.has_mappings and isinstance(coil_data.value, str):
self._update_coil_mappings(coil, coil_data.value)

self.mqtt_client.publish_coil_state(coil_data)
self._publish_coil_updates(coil_data)

if self.poller is not None:
self.poller.register_update(coil_data.coil)
self.poller.register_update(coil)

def _update_coil_mappings(self, coil, value):
try:
coil.get_reverse_mapping_for(value)
except NoMappingException:
logger.warning(f"No mapping found for {coil.name} value: {value}")

# If value in format `UNKNOWN (123)`, then extract the number
match = self.re_unknown_value.match(value)
if match:
current_mappings = coil.mappings.copy()
unknown_value = int(match.group(1))
current_mappings[str(unknown_value)] = value
coil.set_mappings(current_mappings)
self.announced_coils.discard(coil)

def _publish_coil_updates(self, coil_data):
coil = coil_data.coil
if coil not in self.announced_coils:
self.mqtt_client.publish_discovery(coil_data, self._get_device_info())
self.announced_coils.add(coil)

self.mqtt_client.publish_coil_state(coil_data)

def on_mqtt_connected(self):
self.announced_coils.clear()
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
nibe>=2.6.0,<3.0.0
nibe>=2.12.0,<3.0.0

paho_mqtt>=1.5.0
paho_mqtt>=1.5.0,<2.0.0
voluptuous>=0.13.0
pyyaml>=6.0
python-slugify>=6.0.0
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ include_package_data = True
packages = find:
python_requires = >=3.9
install_requires =
nibe >=2.1.0,<3.0.0
paho_mqtt>=1.5.0
nibe >=2.7.0,<3.0.0
paho_mqtt>=1.5.0,<2.0.0
voluptuous>=0.13.0
pyyaml>=6.0
python-slugify>=6.0.0
Expand Down

0 comments on commit d1ee5e0

Please sign in to comment.