Skip to content

Commit 6ca3009

Browse files
2.2 (#28)
1 parent bc72aad commit 6ca3009

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ from energy meters and report the values through mqtt.
1818

1919

2020
# Changelog
21+
#### 2.2 (2023-03-31)
22+
- Small config improvements
23+
2124
#### 2.1 (2023-03-27)
2225
- Additional obis id for serial number matching
2326
- Improved serial reading a bit

src/sml2mqtt/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.1'
1+
__version__ = '2.2'

src/sml2mqtt/device/sml_device.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,33 @@ def _setup_sml_values(self, device_config: Optional[SmlDeviceConfig], frame_valu
147147
log_level = logging.DEBUG if not CMD_ARGS.analyze else logging.INFO
148148
values_config: Dict[str, SmlValueConfig] = device_config.values if device_config is not None else {}
149149

150-
for obis_id in frame_values:
150+
from_frame = frozenset(frame_values) - self.skip_values
151+
from_config = frozenset(values_config)
152+
default_cfg = from_frame - from_config
153+
154+
# create entries where we don't have a user config
155+
for obis_id in sorted(default_cfg):
156+
self.log.log(log_level, f'Creating default value handler for {obis_id}')
157+
self.sml_values[obis_id] = SmlValue(
158+
self.device_id, obis_id, self.mqtt_device.create_child(obis_id),
159+
workarounds=[], transformations=[], filters=sml2mqtt.sml_value.filter_from_config(None)
160+
)
161+
162+
# create entries which are defined by the user
163+
for obis_id, value_config in sorted(values_config.items()):
164+
# little helper to catch config errors
151165
if obis_id in self.skip_values:
152-
continue
153-
154-
value_config = values_config.get(obis_id)
155-
156-
if device_config is None or value_config is None:
157-
self.log.log(log_level, f'Creating default value handler for {obis_id}')
158-
value = SmlValue(
159-
self.device_id, obis_id, self.mqtt_device.create_child(obis_id),
160-
workarounds=[], transformations=[], filters=sml2mqtt.sml_value.filter_from_config(None)
161-
)
162-
else:
163-
self.log.log(log_level, f'Creating value handler from config for {obis_id}')
164-
value = SmlValue(
165-
self.device_id, obis_id, self.mqtt_device.create_child(obis_id).set_config(value_config.mqtt),
166-
workarounds=sml2mqtt.sml_value.workaround_from_config(value_config.workarounds),
167-
transformations=sml2mqtt.sml_value.transform_from_config(value_config.transformations),
168-
filters=sml2mqtt.sml_value.filter_from_config(value_config.filters),
169-
)
170-
171-
self.sml_values[obis_id] = value
166+
self.log.warning(f'Config for {obis_id:s} found but {obis_id:s} is also marked to be skipped')
167+
if obis_id not in frame_values:
168+
self.log.warning(f'Config for {obis_id:s} found but {obis_id:s} was not reported by the frame')
169+
170+
self.log.log(log_level, f'Creating value handler from config for {obis_id}')
171+
self.sml_values[obis_id] = SmlValue(
172+
self.device_id, obis_id, self.mqtt_device.create_child(obis_id).set_config(value_config.mqtt),
173+
workarounds=sml2mqtt.sml_value.workaround_from_config(value_config.workarounds),
174+
transformations=sml2mqtt.sml_value.transform_from_config(value_config.transformations),
175+
filters=sml2mqtt.sml_value.filter_from_config(value_config.filters),
176+
)
172177

173178
def serial_data_timeout(self):
174179
if self.set_status(DeviceStatus.MSG_TIMEOUT):
@@ -264,7 +269,16 @@ def process_frame(self, frame: SmlFrame):
264269
frame_values.pop(drop_obis, None)
265270

266271
for obis_id, frame_value in frame_values.items():
267-
self.sml_values[obis_id].set_value(frame_value, frame_values)
272+
if (sml_value := self.sml_values.get(obis_id)) is not None:
273+
sml_value.set_value(frame_value, frame_values)
274+
else:
275+
# This can only happen if the device does not report all values with the initial frame
276+
# The user then has to skip the obis ids or manually add them to the configuration
277+
self.log.error('Unexpected obis id received!')
278+
for line in frame_value.format_msg().splitlines():
279+
self.log.error(line)
280+
self.set_status(DeviceStatus.ERROR)
281+
return None
268282

269283
# There was no Error -> OK
270284
self.set_status(DeviceStatus.OK)

tests/device/frames/test_frame_1.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,15 @@ async def test_frame_no_config(device: TestingDevice, no_serial, caplog, monkeyp
3939
assert msg == sml_frame_1_analyze + """
4040
Found obis id 0100600100ff in the sml frame
4141
No configuration found for 0a0149534b0005020de2
42-
Creating default value handler for 010060320101
4342
Creating default value handler for 0100010800ff
4443
Creating default value handler for 0100100700ff
44+
Creating default value handler for 010060320101
4545
testing/0a0149534b0005020de2/010060320101: ISK (QOS: 0, retain: False)
4646
testing/0a0149534b0005020de2/0100010800ff: 253.9177 (QOS: 0, retain: False)
4747
testing/0a0149534b0005020de2/0100100700ff: 272 (QOS: 0, retain: False)
4848
OK
4949
testing/0a0149534b0005020de2/status: OK (QOS: 0, retain: False)
5050
51-
testing/0a0149534b0005020de2/010060320101 (010060320101):
52-
raw value: ISK
53-
pub value: ISK
54-
filters:
55-
- <Every: 120>
56-
- <OnChange>
57-
5851
testing/0a0149534b0005020de2/0100010800ff (0100010800ff):
5952
raw value: 253.9177
6053
pub value: 253.9177
@@ -69,6 +62,13 @@ async def test_frame_no_config(device: TestingDevice, no_serial, caplog, monkeyp
6962
- <Every: 120>
7063
- <OnChange>
7164
65+
testing/0a0149534b0005020de2/010060320101 (010060320101):
66+
raw value: ISK
67+
pub value: ISK
68+
filters:
69+
- <Every: 120>
70+
- <OnChange>
71+
7272
SHUTDOWN
7373
testing/0a0149534b0005020de2/status: SHUTDOWN (QOS: 0, retain: False)"""
7474

0 commit comments

Comments
 (0)