Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra port open, and add ser2net example to README #124

Merged
merged 3 commits into from
Jul 13, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ custom_components/kamstrup_403/sensor.py
## Configuration

Configuration is done in the UI. It's recommended to use devices as `/dev/serial/by-id` and not `/dev/ttyUSB1` as the port. This is because the first example is a stable identifier, while the second can change when USB devices are added or removed, or even when you perform a system reboot.<br>
The port should look like this: `/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_D307PBVY-if00-port0`.
The port should look like this: `/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_D307PBVY-if00-port0`. If the port is a remote port (e.g. by using ser2net) it's possible to use a socket connection too, by using something similar to `socket://192.168.1.101:20019`.

Some meters contain a battery, and communicating with the meter does impact battery life. By default, this component updates every `3600` seconds (1 hour). This is configurable. Also, since version `2.0.1` you can also configure the serial timeout. The default value is `1.0` seconds, if you get the error `Finished update, No readings from the meter. Please check the IR connection` you can try to increase this value. Fractional numbers are allowed (eg. `0.5`).
You can do this by pressing `configure` on the Integrations page:
Expand Down
6 changes: 6 additions & 0 deletions custom_components/kamstrup_403/pykamstrup/kamstrup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ def _debug(self, msg: str, byte_array: bytearray):
log += f" {byte:02x}"
_LOGGER.debug(log)

def _make_sure_port_is_opened(self):
if not self.ser.is_open:
self.ser.open()

def _write(self, data: tuple[int]):
"""Write directly to the meter"""
self._make_sure_port_is_opened()
bytearray_data = bytearray(data)
self._debug("Write", bytearray_data)
self.ser.write(bytearray_data)

def _read(self) -> int | None:
"""Read directly from the meter"""
self._make_sure_port_is_opened()
data = self.ser.read(1)
if len(data) == 0:
_LOGGER.debug("Rx Timeout")
Expand Down