Skip to content

Commit

Permalink
Updates for using nibe 2.0.0 library
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 committed Feb 26, 2023
1 parent d810dc0 commit 0874799
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
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.3.3"
__version__ = "1.0.0"
10 changes: 5 additions & 5 deletions nibe_mqtt/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import ABC, abstractmethod
from typing import Union

from nibe.coil import Coil
from nibe.coil import Coil, CoilData
from paho.mqtt.client import Client, MQTTMessage

logger = logging.getLogger("nibe").getChild(__name__)
Expand Down Expand Up @@ -75,10 +75,10 @@ def stop(self):
def _get_coil_state_topic(self, coil: Coil):
return f"{self._conf['prefix']}/coils/{coil.name}"

def publish_coil_state(self, coil: Coil):
def publish_coil_state(self, coil_data: CoilData):
self._client.publish(
self._get_coil_state_topic(coil),
coil.value,
self._get_coil_state_topic(coil_data.coil),
coil_data.value,
retain=self._conf["retain_state"],
)

Expand Down Expand Up @@ -124,7 +124,7 @@ def publish_discovery(self, coil: Coil, device_info: dict):
else: # binary_sensor
component = "binary_sensor"
elif coil.is_writable: # switch
if coil.mappings:
if coil.has_mappings:
component = "select"
config["command_topic"] = f"{config['state_topic']}/set"
config["options"] = list(coil.reverse_mappings.keys())
Expand Down
37 changes: 15 additions & 22 deletions nibe_mqtt/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from pathlib import Path
from typing import Set, Union

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

Expand Down Expand Up @@ -77,9 +77,9 @@ def _get_device_info(self) -> dict:
def handle_coil_set(self, name, value: str):
try:
coil = self.heatpump.get_coil_by_name(name)
coil.value = value
coil_data = CoilData(coil, value)

asyncio.create_task(self.write_coil(coil))
asyncio.create_task(self.write_coil(coil_data))
except (AssertionError, CoilNotFoundException) as e:
logger.error(e)
except Exception as e:
Expand All @@ -88,26 +88,19 @@ def handle_coil_set(self, name, value: str):
async def read_coil(self, coil: Coil):
return await self.connection.read_coil(coil)

async def write_coil(self, coil: Coil) -> None:
async def write_coil(self, coil_data: CoilData) -> None:
refresh_required = True
try:
coil_value = coil.value
await self.connection.write_coil(coil)
if coil_value == coil.value: # if coil value did not change while we were writing, just publish to MQTT
self.on_coil_update(coil)
refresh_required = False
else: # if value has changed we do not know what is the current state
logger.info(
f"{coil.name} value has changed while we were writing: {coil_value} -> {coil.value}"
)
except CoilWriteException as e:
logger.info(f"Writing {coil_data}")
await self.connection.write_coil(coil_data)
except WriteException as e:
logger.error(e)
except Exception as e:
logger.exception("Unhandled exception during write", e)

if refresh_required:
try:
await self.read_coil(coil)
await self.read_coil(coil_data.coil)
except Exception as e:
logger.exception("Unhandled exception during read", e)

Expand All @@ -122,15 +115,15 @@ async def start(self):

self.mqtt_client.start()

def on_coil_update(self, coil: Coil):
if coil not in self.announced_coils:
self.mqtt_client.publish_discovery(coil, self._get_device_info())
self.announced_coils.add(coil)
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)

self.mqtt_client.publish_coil_state(coil)
self.mqtt_client.publish_coil_state(coil_data)

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


class PollService:
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools>=46.4.0"]
build-backend = "setuptools.build_meta"
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
asyncio_mode = "auto"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nibe>=1.0.0,<2.0.0
nibe>=2.0.0,<3.0.0

paho_mqtt>=1.5.0
voluptuous>=0.13.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include_package_data = True
packages = find:
python_requires = >=3.9
install_requires =
nibe >=1.3.1,<2.0.0
nibe >=2.0.0,<3.0.0
paho_mqtt>=1.5.0
voluptuous>=0.13.0
pyyaml>=6.0
Expand Down
12 changes: 6 additions & 6 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

from nibe.coil import CoilData

from nibe_mqtt.config import schema
from nibe_mqtt.service import Service

Expand Down Expand Up @@ -45,7 +47,6 @@ def mqtt_connection():
return mock.Mock(spec=MqttConnection)


@pytest.mark.asyncio
async def test_service_nibegw(nibegw_config):
service = Service(nibegw_config)

Expand All @@ -54,11 +55,10 @@ async def test_service_nibegw(nibegw_config):
assert len(service.heatpump.get_coils()) > 0

outdoor_temperature = service.heatpump.get_coil_by_address(40004)
outdoor_temperature.value = 10
service.on_coil_update(outdoor_temperature)
coil_data = CoilData(outdoor_temperature, 10)
service.on_coil_update(coil_data)


@pytest.mark.asyncio
async def test_service_modbus(modbus_config):
service = Service(modbus_config)

Expand All @@ -67,5 +67,5 @@ async def test_service_modbus(modbus_config):
assert len(service.heatpump.get_coils()) > 0

outdoor_temperature = service.heatpump.get_coil_by_address(30002)
outdoor_temperature.value = 10
service.on_coil_update(outdoor_temperature)
coil_data = CoilData(outdoor_temperature, 10)
service.on_coil_update(coil_data)

0 comments on commit 0874799

Please sign in to comment.