mirror of
https://github.com/home-assistant/core.git
synced 2025-07-31 09:17:10 +00:00
Map sensor status to more human-friendly strings
This commit is contained in:
parent
c522db9138
commit
c272a596ae
@ -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)
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user