|
3 | 3 |
|
4 | 4 | local st_utils = require "st.utils" |
5 | 5 | local capabilities = require "st.capabilities" |
6 | | -local aqs_utils = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.utils" |
7 | 6 | local aqs_fields = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.fields" |
8 | 7 |
|
9 | 8 | local AirQualitySensorAttributeHandlers = {} |
|
25 | 24 |
|
26 | 25 | function AirQualitySensorAttributeHandlers.measured_value_factory(capability_name, attribute, target_unit) |
27 | 26 | return function(driver, device, ib, response) |
28 | | - local reporting_unit = device:get_field(capability_name.."_unit") |
29 | | - |
30 | | - if reporting_unit == nil then |
31 | | - reporting_unit = aqs_fields.unit_default[capability_name] |
32 | | - device:set_field(capability_name.."_unit", reporting_unit, {persist = true}) |
33 | | - end |
34 | | - |
35 | | - if reporting_unit then |
36 | | - local value = aqs_utils.unit_conversion(device, ib.data.value, reporting_unit, target_unit) |
37 | | - device:emit_event_for_endpoint(ib.endpoint_id, attribute({value = value, unit = aqs_fields.unit_strings[target_unit]})) |
38 | | - |
39 | | - -- handle case where device profile supports both fineDustLevel and dustLevel |
40 | | - if capability_name == capabilities.fineDustSensor.NAME and device:supports_capability(capabilities.dustSensor) then |
41 | | - device:emit_event_for_endpoint(ib.endpoint_id, capabilities.dustSensor.fineDustLevel({value = value, unit = aqs_fields.unit_strings[target_unit]})) |
| 27 | + if ib.data.value then |
| 28 | + local reporting_unit = device:get_field(capability_name.."_unit") or aqs_fields.unit_default[capability_name] |
| 29 | + local conversion_function = aqs_fields.conversion_tables[reporting_unit][target_unit] |
| 30 | + if conversion_function then |
| 31 | + local converted_value = conversion_function(ib.data.value) |
| 32 | + device:emit_event_for_endpoint(ib.endpoint_id, attribute({value = converted_value, unit = aqs_fields.unit_strings[target_unit]})) |
| 33 | + -- handle case where device profile supports both fineDustLevel and dustLevel |
| 34 | + if capability_name == capabilities.fineDustSensor.NAME and device:supports_capability(capabilities.dustSensor) then |
| 35 | + device:emit_event_for_endpoint(ib.endpoint_id, capabilities.dustSensor.fineDustLevel({value = converted_value, unit = aqs_fields.unit_strings[target_unit]})) |
| 36 | + end |
| 37 | + else |
| 38 | + device.log.info_with({hub_logs=true}, string.format("Unsupported unit conversion from %s to %s", aqs_fields.unit_strings[reporting_unit], aqs_fields.unit_strings[target_unit])) |
42 | 39 | end |
43 | 40 | end |
44 | 41 | end |
|
0 commit comments