From c272a596ae356a5e9df432226b11c90b3e51438d Mon Sep 17 00:00:00 2001 From: Parker Brown <17183625+parkerbxyz@users.noreply.github.com> Date: Thu, 6 Feb 2025 08:27:36 +0000 Subject: [PATCH] Map sensor status to more human-friendly strings --- homeassistant/components/aranet/sensor.py | 43 +++++++++++++++++++++-- tests/components/aranet/test_sensor.py | 8 ++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/aranet/sensor.py b/homeassistant/components/aranet/sensor.py index 778d6f7f220..6d12c044dd3 100644 --- a/homeassistant/components/aranet/sensor.py +++ b/homeassistant/components/aranet/sensor.py @@ -5,7 +5,7 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any -from aranet4.client import Aranet4Advertisement, Color +from aranet4.client import Aranet4Advertisement from bleak.backends.device import BLEDevice from homeassistant.components.bluetooth.passive_update_processor import ( @@ -78,7 +78,6 @@ SENSOR_DESCRIPTIONS = { key="threshold_level", name="Threshold Level", device_class=SensorDeviceClass.ENUM, - options=[status.name for status in Color], ), "co2": AranetSensorEntityDescription( key="co2", @@ -168,7 +167,7 @@ def sensor_update_to_bluetooth_data_update( if val == -1: continue if key == "status": - val = val.name # Use the name of the status + val = get_friendly_status(val.name, adv.readings.type.name) else: val *= desc.scale data[tag] = val @@ -222,3 +221,41 @@ 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: str, device_type: str) -> str: + """Convert device status to a human-readable string based on device type. + + Args: + status: Raw status string from device (e.g., "GREEN", "RED") + device_type: Aranet device type (e.g., "ARANET4", "ARANET_RADON") + + Returns: + Friendly status string (e.g., "Good", "Unhealthy") + + Example: + >>> get_friendly_status("GREEN", "ARANET4") + 'Good' + >>> get_friendly_status("GREEN", "ARANET_RADON") + 'Normal' + + """ + base_status_map = { + "ERROR": "Error", + "RED": "Unhealthy", + } + + device_status_map = { + "ARANET4": { + **base_status_map, + "GREEN": "Good", + "YELLOW": "Average", + }, + "ARANET_RADON": { + **base_status_map, + "GREEN": "Normal", + "YELLOW": "Elevated", + }, + } + + return device_status_map.get(device_type, {}).get(status, status) diff --git a/tests/components/aranet/test_sensor.py b/tests/components/aranet/test_sensor.py index 4d0cb700a6c..6f190f637bb 100644 --- a/tests/components/aranet/test_sensor.py +++ b/tests/components/aranet/test_sensor.py @@ -3,7 +3,7 @@ import pytest from homeassistant.components.aranet.const import DOMAIN -from homeassistant.components.sensor import ATTR_OPTIONS, ATTR_STATE_CLASS +from homeassistant.components.sensor import 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,9 +216,8 @@ async def test_sensors_aranet4( status_sensor = hass.states.get("sensor.aranet4_12345_threshold_level") status_sensor_attrs = status_sensor.attributes - assert status_sensor.state == "GREEN" + assert status_sensor.state == "Good" assert status_sensor_attrs[ATTR_FRIENDLY_NAME] == "Aranet4 12345 Threshold Level" - assert set(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") @@ -299,9 +298,8 @@ async def test_sensors_aranetrn( status_sensor = hass.states.get("sensor.aranetrn_12345_threshold_level") status_sensor_attrs = status_sensor.attributes - assert status_sensor.state == "GREEN" + assert status_sensor.state == "Normal" assert status_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Threshold Level" - assert set(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")