Refactor concentration level handling and tests

This commit is contained in:
Parker Brown 2025-02-07 07:21:38 +00:00 committed by GitHub
parent 92d8b1cb9f
commit ad7f512e45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 23 deletions

View File

@ -5,7 +5,7 @@ from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from aranet4.client import Aranet4Advertisement, AranetType, Color
from aranet4.client import Aranet4Advertisement, Color
from bleak.backends.device import BLEDevice
from homeassistant.components.bluetooth.passive_update_processor import (
@ -76,8 +76,10 @@ SENSOR_DESCRIPTIONS = {
),
"status": AranetSensorEntityDescription(
key="concentration_level",
translation_key="concentration_level",
name="Concentration Level",
device_class=SensorDeviceClass.ENUM,
options=[status.name for status in Color],
),
"co2": AranetSensorEntityDescription(
key="co2",
@ -166,11 +168,9 @@ def sensor_update_to_bluetooth_data_update(
val = getattr(adv.readings, key)
if val == -1:
continue
val = (
get_friendly_status(val.name, adv.readings.type)
if key == "status"
else val * desc.scale
)
if key == "status":
val = val.name
val = val * desc.scale
data[tag] = val
names[tag] = desc.name
descs[tag] = desc
@ -222,17 +222,3 @@ class Aranet4BluetoothSensorEntity(
def native_value(self) -> int | float | None:
"""Return the native value."""
return self.processor.entity_data.get(self.entity_key)
def get_friendly_status(status: Color, device_type: AranetType) -> str:
"""Map status code to human-readable status based on device type."""
status_map = {
"ERROR": "Error",
"RED": "Unhealthy",
(device_type.ARANET4, "GREEN"): "Good",
(device_type.ARANET4, "YELLOW"): "Average",
(device_type.ARANET_RADON, "GREEN"): "Normal",
(device_type.ARANET_RADON, "YELLOW"): "Elevated",
}
return status_map.get((device_type, status), status_map.get(status, status))

View File

@ -21,5 +21,16 @@
"no_devices_found": "No unconfigured Aranet devices found.",
"outdated_version": "This device is using outdated firmware. Please update it to at least v1.2.0 and try again."
}
},
"entity": {
"sensor": {
"concentration_level": {
"state": {
"green": "Normal",
"yellow": "Elevated",
"red": "Unhealthy"
}
}
}
}
}

View File

@ -3,7 +3,7 @@
import pytest
from homeassistant.components.aranet.const import DOMAIN
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.components.sensor import ATTR_OPTIONS, ATTR_STATE_CLASS
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
@ -216,10 +216,11 @@ async def test_sensors_aranet4(
status_sensor = hass.states.get("sensor.aranet4_12345_concentration_level")
status_sensor_attrs = status_sensor.attributes
assert status_sensor.state == "Good"
assert status_sensor.state == "GREEN"
assert (
status_sensor_attrs[ATTR_FRIENDLY_NAME] == "Aranet4 12345 Concentration Level"
)
assert status_sensor_attrs[ATTR_OPTIONS] == ["ERROR", "GREEN", "YELLOW", "RED"]
# Check device context for the battery sensor
entity = entity_registry.async_get("sensor.aranet4_12345_battery")
@ -300,10 +301,11 @@ async def test_sensors_aranetrn(
status_sensor = hass.states.get("sensor.aranetrn_12345_concentration_level")
status_sensor_attrs = status_sensor.attributes
assert status_sensor.state == "Normal"
assert status_sensor.state == "GREEN"
assert (
status_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Concentration Level"
)
assert status_sensor_attrs[ATTR_OPTIONS] == ["ERROR", "GREEN", "YELLOW", "RED"]
# Check device context for the battery sensor
entity = entity_registry.async_get("sensor.aranetrn_12345_battery")