@@ -147,28 +147,33 @@ def _setup_sml_values(self, device_config: Optional[SmlDeviceConfig], frame_valu
147
147
log_level = logging .DEBUG if not CMD_ARGS .analyze else logging .INFO
148
148
values_config : Dict [str , SmlValueConfig ] = device_config .values if device_config is not None else {}
149
149
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
151
165
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
+ )
172
177
173
178
def serial_data_timeout (self ):
174
179
if self .set_status (DeviceStatus .MSG_TIMEOUT ):
@@ -264,7 +269,16 @@ def process_frame(self, frame: SmlFrame):
264
269
frame_values .pop (drop_obis , None )
265
270
266
271
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
268
282
269
283
# There was no Error -> OK
270
284
self .set_status (DeviceStatus .OK )
0 commit comments