Skip to content

Commit

Permalink
1.2.1:
Browse files Browse the repository at this point in the history
- Add possibility to specify additional serial number obis
  • Loading branch information
spacemanspiff2007 committed Oct 14, 2022
1 parent b76ba70 commit 9f3bf4f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ devices:


### Example devices configuration
One energy meter is connected to the serial port. The serial meter reports OSIB ``0100000009ff``
One energy meter is connected to the serial port. The serial meter reports OBIS ``0100000009ff``
as ``11111111111111111111``.

For this device
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Testing
pytest >= 7.1, < 8
pre-commit >= 2, < 3
pytest-asyncio >= 0.18.2, < 0.19
pytest-asyncio >= 0.19, < 0.20
4 changes: 2 additions & 2 deletions requirements_setup.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
asyncio-mqtt == 0.12.1
pyserial-asyncio == 0.6
easyconfig == 0.2.4
pydantic >= 1.9, <2.0
smllib == 1.1
pydantic >= 1.10, <2.0
smllib == 1.2
2 changes: 1 addition & 1 deletion src/sml2mqtt/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.0'
__version__ = '1.2.1'
4 changes: 4 additions & 0 deletions src/sml2mqtt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class GeneralSettings(BaseModel):
False, description='Report the device id even though it does never change',
alias='report device id', in_file=False
)
device_id_obis: str = Field(
'', description='Additional OBIS field for the serial number, default is 0100000009ff',
alias='device id obis', in_file=False
)


class Settings(AppBaseModel):
Expand Down
35 changes: 23 additions & 12 deletions src/sml2mqtt/device/sml_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def set_status(self, new_status: DeviceStatus) -> bool:
shutdown(AllDevicesFailed)
return True

def process_device_id(self, serial: str):
def set_device_id(self, serial: str):
assert isinstance(serial, str)
assert not self.device_id_set
self.device_id = serial
self.device_id_set = True

Expand All @@ -94,6 +95,26 @@ def process_device_id(self, serial: str):
if cfg.skip is not None:
self.skip_values.update(cfg.skip)

def select_device_id(self, frame_values: Dict[str, SmlListEntry]):
obis_ids = ['0100000009ff']
if CONFIG.general.device_id_obis:
obis_ids.append(CONFIG.general.device_id_obis)

for obis_id in obis_ids:
entry = frame_values.pop(obis_id, None)
if entry is not None:
if not CONFIG.general.report_device_id:
self.skip_values.add(obis_id)
self.set_device_id(entry.get_value())
break
else:
self.set_device_id(self.device_url)

# remove additionally skipped frames
# this gets filled in set_device_id, so we have to do it afterwards!
for name in self.skip_values:
frame_values.pop(name, None)

def serial_data_timeout(self):
if self.set_status(DeviceStatus.MSG_TIMEOUT):
self.stream.clear()
Expand Down Expand Up @@ -181,17 +202,7 @@ async def process_frame(self, frame: SmlFrame):
# We overwrite the device_id url (default) with the serial number if the device reports it
# Otherwise we still do the config lookup so the user can configure the mqtt topics
if not self.device_id_set:
entry = frame_values.pop('0100000009ff', None)
if entry is not None:
if not CONFIG.general.report_device_id:
self.skip_values.add('0100000009ff')
self.process_device_id(entry.get_value())
else:
self.process_device_id(self.device_url)

# remove additionally skipped frames
for name in self.skip_values:
frame_values.pop(name, None)
self.select_device_id(frame_values)

# Process all values
for obis_value in frame_values.values():
Expand Down
4 changes: 2 additions & 2 deletions tests/device/frames/test_frame_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def test_frame_no_id(device: Device, no_serial, caplog, sml_frame_1: SmlFr
abort_on_error: 0
message_body <SmlGetListResponse>
client_id : None
sever_id : 0a0149534b0005020de2
server_id : 0a0149534b0005020de2
list_name : 0100620affff
act_sensor_time : 1815342
val_list:
Expand Down Expand Up @@ -81,7 +81,7 @@ async def test_frame_no_id(device: Device, no_serial, caplog, sml_frame_1: SmlFr
scaler : -1
value : 0
value_signature: None
-> 0.0Wh
-> 0.0Wh (Wirkenergie Total)
<SmlListEntry>
obis : 0100100700ff (1-0:16.7.0*255)
status : None
Expand Down
4 changes: 2 additions & 2 deletions tests/device/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def test_serial_data(device: Device, no_serial, caplog, sml_data_1: bytes,
abort_on_error: 0
message_body <SmlGetListResponse>
client_id : None
sever_id : 00000000000000000000
server_id : 00000000000000000000
list_name : None
act_sensor_time : 3064820
val_list:
Expand Down Expand Up @@ -109,7 +109,7 @@ async def test_serial_data(device: Device, no_serial, caplog, sml_data_1: bytes,
scaler : -5
value : 219800000
value_signature: None
-> 2198.0Wh
-> 2198.0Wh (Wirkenergie Total)
<SmlListEntry>
obis : 0100100700ff (1-0:16.7.0*255)
status : None
Expand Down

0 comments on commit 9f3bf4f

Please sign in to comment.