Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 22 additions & 20 deletions custom_components/kamstrup_403/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
For more details about this integration, please refer to
https://github.com/custom-components/kamstrup_403
"""
import asyncio
from datetime import timedelta
import logging
from typing import Any, List
Expand All @@ -27,7 +26,7 @@
PLATFORMS,
VERSION,
)
from .kamstrup import Kamstrup
from .pykamstrup.kamstrup import Kamstrup

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -55,7 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

try:
client = Kamstrup(port, DEFAULT_BAUDRATE, timeout_seconds)
except (Exception) as exception:
except Exception as exception:
_LOGGER.error("Can't establish a connection with %s", port)
raise ConfigEntryNotReady() from exception

Expand Down Expand Up @@ -136,36 +135,39 @@ async def _async_update_data(self) -> dict[int, Any]:
_LOGGER.debug("Start update")

data = {}
failed_counter = 0

try:
values = self.kamstrup.get_values(self._commands)
except serial.SerialException as exception:
_LOGGER.error(
"Device disconnected or multiple access on port? \nException: %e",
exception,
)
except Exception as exception:
_LOGGER.error(
"Error reading multiple %s \nException: %s", self._commands, exception
)
raise UpdateFailed() from exception

failed_counter = len(self._commands) - len(values)

for command in self._commands:
try:
value, unit = self.kamstrup.readvar(command)
if command in values:
value, unit = values[command]
data[command] = {"value": value, "unit": unit}
_LOGGER.debug(
"New value for sensor %s, value: %s %s", command, value, unit
)

if value is None and unit is None:
failed_counter += 1

await asyncio.sleep(1)
except (serial.SerialException) as exception:
_LOGGER.error(
"Device disconnected or multiple access on port? \nException: %e",
exception,
)
except (Exception) as exception:
_LOGGER.error("Error reading %s \nException: %s", command, exception)
raise UpdateFailed() from exception

if failed_counter == len(data):
_LOGGER.error(
"Finished update, No readings from the meter. Please check the IR connection"
)
else:
_LOGGER.debug(
"Finished update, %s/%s readings failed", failed_counter, len(data)
"Finished update, %s out of %s readings failed",
failed_counter,
len(data),
)

return data
2 changes: 1 addition & 1 deletion custom_components/kamstrup_403/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def async_step_user(self, user_input=None):
return self.async_create_entry(
title=user_input[CONF_PORT], data=user_input
)
except (serial.SerialException):
except serial.SerialException:
self._errors["base"] = "port"
else:
self._errors["base"] = "port"
Expand Down
225 changes: 0 additions & 225 deletions custom_components/kamstrup_403/kamstrup.py

This file was deleted.

24 changes: 24 additions & 0 deletions custom_components/kamstrup_403/pykamstrup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# PyKamstrup
This is an implementation of the Kamstrup Meter Protocol (KMP) based
on reverse engineering of a traffic dump.

## Contributors
This file has seen many modifications over the years, contributions have been made by:
| Author | Profile | Notes | Source |
|--|--|--|--|
| Poul-Henning Kamp | [@bsdphk](https://github.com/bsdphk) | Original author | https://github.com/bsdphk/PyKamstrup |
| Erik Jensen | | Provided units and exponents | |
| Frank Reijn | [@freijn](https://github.com/freijn) | | |
| Paul Bonnemaijers | | | |
| | [@adabrandt](https://github.com/adabrandt) | Support reading of multiple values at once | https://github.com/bsdphk/PyKamstrup/pull/6 |
| Sander Gols | [@golles](https://github.com/golles) | This component for Home Assistant | |

There might be other significant contributors that I'm not aware off, feel free to add them in a PR.

## License
`kamstrup.py` has it's own license, from the original author:
```
"THE BEER-WARE LICENSE" (Revision 42):

<phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
```
1 change: 1 addition & 0 deletions custom_components/kamstrup_403/pykamstrup/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""PyKamstrup module."""
Loading