Refactor the codeflow to avoid unassigned variable
@@ -11,15 +11,22 @@ from .helpers import (
_LOGGER = logging.getLogger(__name__)
-def parse_airmentor(self, data, source_mac, rssi):
- """Parser for Air Mentor"""
- data_length = len(data)
- if data_length == 12:
- device_type = "Air Mentor Pro 2"
- firmware = "Air Mentor"
- airmentor_mac = source_mac
- msg_type = data[2]
- xvalue = data[4:]
+def aqi_to_air_quality(aqi):
+ if aqi <= 50:
+ return "good"
+ elif aqi <= 100:
+ return "moderate"
+ elif aqi <= 150:
+ return "unhealthy for sensitive people"
+ elif aqi <= 200:
+ return "unhealthy"
+ elif aqi <= 250:
+ return "very unhealthy"
+ else:
+ return "hazardous"
+
+
+def parse_pro2(msg_type, xvalue):
if msg_type in [0x12, 0x22]:
(tvoc, temp, temp_cal, humi, aqi) = unpack(">HHBBH", xvalue)
temperature = (temp - 4000) * 0.01
@@ -29,19 +36,8 @@ def parse_airmentor(self, data, source_mac, rssi):
temperature_calibrated * 17.62 / (temperature_calibrated + 243.12)
), 2
)
- if aqi <= 50:
- air_quality = "good"
- elif aqi <= 100:
- air_quality = "moderate"
- elif aqi <= 150:
- air_quality = "unhealthy for sensitive people"
- elif aqi <= 200:
- air_quality = "unhealthy"
- elif aqi <= 250:
- air_quality = "very unhealthy"
- else:
- air_quality = "hazardous"
- result = {
+ air_quality = aqi_to_air_quality(aqi)
+ return {
"tvoc": tvoc,
"temperature": round(temperature, 2),
"temperature calibrated": round(temperature_calibrated, 2),
@@ -49,17 +45,33 @@ def parse_airmentor(self, data, source_mac, rssi):
"aqi": aqi,
"air quality": air_quality
}
- if msg_type in [0x11, 0x21]:
+ elif msg_type in [0x11, 0x21]:
(co2, pm25, pm10, co, o3) = unpack(">HHHBB", xvalue)
# CO and O3 are not used
- result = {
+ return {
"co2": co2,
"pm2.5": pm25,
"pm10": pm10,
}
else:
+ return None
+
+
+def parse_airmentor(self, data, source_mac, rssi):
+ """Parser for Air Mentor"""
+ data_length = len(data)
+ airmentor_mac = source_mac
+
device_type = None
- if device_type is None:
+ result = None
+ if data_length == 12:
+ device_type = "Air Mentor Pro 2"
+ firmware = "Air Mentor"
+ msg_type = data[2]
+ xvalue = data[4:]
+ result = parse_pro2(msg_type, xvalue)
+
+ if device_type is None or result is None:
if self.report_unknown == "Air Mentor":
_LOGGER.info(
"BLE ADV from UNKNOWN Air Mentor DEVICE: RSSI: %s, MAC: %s, ADV: %s",
