Skip to content

Commit

Permalink
smartmeter: fix type hinting for Python 3.9, minor fixes, add require…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
Morg42 committed Dec 2, 2024
1 parent c42bc35 commit a32f51c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions smartmeter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from lib.item.item import Item
from lib.shtime import Shtime
from collections.abc import Callable
from typing import Union

from . import dlms
from . import sml
Expand Down Expand Up @@ -89,6 +90,7 @@ def __init__(self, sh):

# load parameters from config
self._protocol = None
self._proto_detect = False
self.load_parameters()

# quit if errors on parameter read
Expand Down Expand Up @@ -143,7 +145,7 @@ def load_parameters(self):

# get mode (SML/DLMS) if set by user
# if not set, try to get at runtime
self._protocol = self.get_parameter_value('protocol')
self._protocol = self.get_parameter_value('protocol').upper()

# DLMS only
self._config['dlms'] = {}
Expand Down Expand Up @@ -203,12 +205,14 @@ def run(self):
# TODO: call DLMS/SML discovery routines to find protocol
if sml.discover(self._config):
self._protocol = 'SML'
self._proto_detect = True
elif dlms.discover(self._config):
self._protocol = 'DLMS'
self._proto_detect = True

self.alive = True
if self._protocol:
self.logger.info(f'set/detected protocol {self._protocol}')
self.logger.info(f'{"detected" if self._proto_detect else "set"} protocol {self._protocol}')
else:
self.logger.error('unable to auto-detect device protocol (SML/DLMS). Try manual disconvery via standalone mode or Web Interface.')
# skip cycle / crontab scheduler if no protocol set (only manual control from web interface)
Expand All @@ -235,7 +239,7 @@ def stop(self):
except Exception:
pass

def parse_item(self, item: Item) -> Callable | None:
def parse_item(self, item: Item) -> Union[Callable, None]:
"""
Default plugin parse_item method. Is called when the plugin is initialized.
Expand Down
1 change: 1 addition & 0 deletions smartmeter/dlms.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ def query(config) -> dict:
logger.error(f"Serial port '{serial_port}' could not be opened")
else:
logger.error(f"Serial port '{serial_port}' could be opened but somehow not accessed")
return {}
except OSError:
logger.error(f"Serial port '{serial_port}' does not exist, please check the spelling")
return {}
Expand Down
2 changes: 1 addition & 1 deletion smartmeter/dlms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,4 @@
1-1:0.2.0(B31)
1-1:0.2.1(005)
!
"""
"""
2 changes: 2 additions & 0 deletions smartmeter/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ parameters:
valid_list:
- DLMS
- SML
- dlms
- sml
- ''
default: ''
description:
Expand Down
1 change: 1 addition & 0 deletions smartmeter/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyserial

0 comments on commit a32f51c

Please sign in to comment.