|
3 | 3 |
|
4 | 4 | local st_utils = require "st.utils" |
5 | 5 | local capabilities = require "st.capabilities" |
6 | | -local fields = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.fields" |
| 6 | +local aqs_utils = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.utils" |
| 7 | +local aqs_fields = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.fields" |
7 | 8 |
|
8 | | -local AirQualityServerAttributeHandlers = {} |
| 9 | +local AirQualitySensorAttributeHandlers = {} |
9 | 10 |
|
10 | 11 |
|
11 | 12 | -- [[ GENERIC CONCENTRATION MEASUREMENT CLUSTER ATTRIBUTES ]] |
12 | 13 |
|
13 | | -function AirQualityServerAttributeHandlers.measurement_unit_factory(capability_name) |
| 14 | +function AirQualitySensorAttributeHandlers.measurement_unit_factory(capability_name) |
14 | 15 | return function(driver, device, ib, response) |
15 | 16 | device:set_field(capability_name.."_unit", ib.data.value, {persist = true}) |
16 | 17 | end |
17 | 18 | end |
18 | 19 |
|
19 | | -function AirQualityServerAttributeHandlers.level_value_factory(attribute) |
| 20 | +function AirQualitySensorAttributeHandlers.level_value_factory(attribute) |
20 | 21 | return function(driver, device, ib, response) |
21 | | - device:emit_event_for_endpoint(ib.endpoint_id, attribute(fields.level_strings[ib.data.value])) |
| 22 | + device:emit_event_for_endpoint(ib.endpoint_id, attribute(aqs_fields.level_strings[ib.data.value])) |
22 | 23 | end |
23 | 24 | end |
24 | 25 |
|
25 | | -local function unit_conversion(device, value, from_unit, to_unit) |
26 | | - local conversion_function = fields.conversion_tables[from_unit][to_unit] |
27 | | - if conversion_function == nil then |
28 | | - device.log.info_with( {hub_logs = true} , string.format("Unsupported unit conversion from %s to %s", fields.unit_strings[from_unit], fields.unit_strings[to_unit])) |
29 | | - return 1 |
30 | | - end |
31 | | - |
32 | | - if value == nil then |
33 | | - device.log.info_with( {hub_logs = true} , "unit conversion value is nil") |
34 | | - return 1 |
35 | | - end |
36 | | - return conversion_function(value) |
37 | | -end |
38 | | - |
39 | | -function AirQualityServerAttributeHandlers.measured_value_factory(capability_name, attribute, target_unit) |
| 26 | +function AirQualitySensorAttributeHandlers.measured_value_factory(capability_name, attribute, target_unit) |
40 | 27 | return function(driver, device, ib, response) |
41 | 28 | local reporting_unit = device:get_field(capability_name.."_unit") |
42 | 29 |
|
43 | 30 | if reporting_unit == nil then |
44 | | - reporting_unit = fields.unit_default[capability_name] |
| 31 | + reporting_unit = aqs_fields.unit_default[capability_name] |
45 | 32 | device:set_field(capability_name.."_unit", reporting_unit, {persist = true}) |
46 | 33 | end |
47 | 34 |
|
48 | 35 | if reporting_unit then |
49 | | - local value = unit_conversion(device, ib.data.value, reporting_unit, target_unit) |
50 | | - device:emit_event_for_endpoint(ib.endpoint_id, attribute({value = value, unit = fields.unit_strings[target_unit]})) |
| 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]})) |
51 | 38 |
|
52 | 39 | -- handle case where device profile supports both fineDustLevel and dustLevel |
53 | 40 | if capability_name == capabilities.fineDustSensor.NAME and device:supports_capability(capabilities.dustSensor) then |
54 | | - device:emit_event_for_endpoint(ib.endpoint_id, capabilities.dustSensor.fineDustLevel({value = value, unit = fields.unit_strings[target_unit]})) |
| 41 | + device:emit_event_for_endpoint(ib.endpoint_id, capabilities.dustSensor.fineDustLevel({value = value, unit = aqs_fields.unit_strings[target_unit]})) |
55 | 42 | end |
56 | 43 | end |
57 | 44 | end |
|
60 | 47 |
|
61 | 48 | -- [[ AIR QUALITY CLUSTER ATTRIBUTES ]] -- |
62 | 49 |
|
63 | | -function AirQualityServerAttributeHandlers.air_quality_handler(driver, device, ib, response) |
| 50 | +function AirQualitySensorAttributeHandlers.air_quality_handler(driver, device, ib, response) |
64 | 51 | local state = ib.data.value |
65 | 52 | if state == 0 then -- Unknown |
66 | 53 | device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.unknown()) |
|
82 | 69 |
|
83 | 70 | -- [[ PRESSURE MEASUREMENT CLUSTER ATTRIBUTES ]] -- |
84 | 71 |
|
85 | | -function AirQualityServerAttributeHandlers.pressure_measured_value_handler(driver, device, ib, response) |
| 72 | +function AirQualitySensorAttributeHandlers.pressure_measured_value_handler(driver, device, ib, response) |
86 | 73 | local pressure = st_utils.round(ib.data.value / 10.0) |
87 | 74 | device:emit_event_for_endpoint(ib.endpoint_id, capabilities.atmosphericPressureMeasurement.atmosphericPressure(pressure)) |
88 | 75 | end |
89 | 76 |
|
90 | | -return AirQualityServerAttributeHandlers |
| 77 | +return AirQualitySensorAttributeHandlers |
0 commit comments