Skip to content

Commit 8dd1e9d

Browse files
parkerbxyzthecode
andauthored
Add threshold sensor to Aranet (#137291)
* Add threshold level sensor description to Aranet component * Use Color enum for status options * Add threshold level sensor tests for Aranet components * Rename `threshold_level` key to `status` * Update test to expect 7 sensors instead of 6 * Map sensor status to more human-friendly strings * Rename `threshold_level` key to `concentration_status` * Update docstring for function * Simplify `get_friendly_status()` * Rename `concentration_status` to `concentration_level` * Rename `concentration_status` to `concentration_level` in sensor tests * Refactor concentration level handling and tests * Normalize concentration level status values to lowercase * Add error to translations * Don't scale status string * Apply suggestions from code review Co-authored-by: Shay Levy <[email protected]> * Rename `concentration_level` to `threshold_indication` * Update threshold indication translations * `threshold_indication` → `threshold` * Capitalize sensor name Co-Authored-By: Shay Levy <[email protected]> --------- Co-authored-by: Shay Levy <[email protected]>
1 parent 096468b commit 8dd1e9d

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

homeassistant/components/aranet/sensor.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import dataclass
66
from typing import Any
77

8-
from aranet4.client import Aranet4Advertisement
8+
from aranet4.client import Aranet4Advertisement, Color
99
from bleak.backends.device import BLEDevice
1010

1111
from homeassistant.components.bluetooth.passive_update_processor import (
@@ -74,6 +74,13 @@ class AranetSensorEntityDescription(SensorEntityDescription):
7474
native_unit_of_measurement=UnitOfPressure.HPA,
7575
state_class=SensorStateClass.MEASUREMENT,
7676
),
77+
"status": AranetSensorEntityDescription(
78+
key="threshold",
79+
translation_key="threshold",
80+
name="Threshold",
81+
device_class=SensorDeviceClass.ENUM,
82+
options=[status.name.lower() for status in Color],
83+
),
7784
"co2": AranetSensorEntityDescription(
7885
key="co2",
7986
name="Carbon Dioxide",
@@ -161,7 +168,10 @@ def sensor_update_to_bluetooth_data_update(
161168
val = getattr(adv.readings, key)
162169
if val == -1:
163170
continue
164-
val *= desc.scale
171+
if key == "status":
172+
val = val.name.lower()
173+
else:
174+
val *= desc.scale
165175
data[tag] = val
166176
names[tag] = desc.name
167177
descs[tag] = desc

homeassistant/components/aranet/strings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,17 @@
2121
"no_devices_found": "No unconfigured Aranet devices found.",
2222
"outdated_version": "This device is using outdated firmware. Please update it to at least v1.2.0 and try again."
2323
}
24+
},
25+
"entity": {
26+
"sensor": {
27+
"threshold": {
28+
"state": {
29+
"error": "Error",
30+
"green": "Green",
31+
"yellow": "Yellow",
32+
"red": "Red"
33+
}
34+
}
35+
}
2436
}
2537
}

tests/components/aranet/test_sensor.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from homeassistant.components.aranet.const import DOMAIN
6-
from homeassistant.components.sensor import ATTR_STATE_CLASS
6+
from homeassistant.components.sensor import ATTR_OPTIONS, ATTR_STATE_CLASS
77
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT
88
from homeassistant.core import HomeAssistant
99
from homeassistant.helpers import device_registry as dr, entity_registry as er
@@ -170,7 +170,7 @@ async def test_sensors_aranet4(
170170
assert len(hass.states.async_all("sensor")) == 0
171171
inject_bluetooth_service_info(hass, VALID_DATA_SERVICE_INFO)
172172
await hass.async_block_till_done()
173-
assert len(hass.states.async_all("sensor")) == 6
173+
assert len(hass.states.async_all("sensor")) == 7
174174

175175
batt_sensor = hass.states.get("sensor.aranet4_12345_battery")
176176
batt_sensor_attrs = batt_sensor.attributes
@@ -214,6 +214,12 @@ async def test_sensors_aranet4(
214214
assert interval_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "s"
215215
assert interval_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
216216

217+
status_sensor = hass.states.get("sensor.aranet4_12345_threshold")
218+
status_sensor_attrs = status_sensor.attributes
219+
assert status_sensor.state == "green"
220+
assert status_sensor_attrs[ATTR_FRIENDLY_NAME] == "Aranet4 12345 Threshold"
221+
assert status_sensor_attrs[ATTR_OPTIONS] == ["error", "green", "yellow", "red"]
222+
217223
# Check device context for the battery sensor
218224
entity = entity_registry.async_get("sensor.aranet4_12345_battery")
219225
device = device_registry.async_get(entity.device_id)
@@ -245,7 +251,7 @@ async def test_sensors_aranetrn(
245251
assert len(hass.states.async_all("sensor")) == 0
246252
inject_bluetooth_service_info(hass, VALID_ARANET_RADON_DATA_SERVICE_INFO)
247253
await hass.async_block_till_done()
248-
assert len(hass.states.async_all("sensor")) == 6
254+
assert len(hass.states.async_all("sensor")) == 7
249255

250256
batt_sensor = hass.states.get("sensor.aranetrn_12345_battery")
251257
batt_sensor_attrs = batt_sensor.attributes
@@ -291,6 +297,12 @@ async def test_sensors_aranetrn(
291297
assert interval_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "s"
292298
assert interval_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
293299

300+
status_sensor = hass.states.get("sensor.aranetrn_12345_threshold")
301+
status_sensor_attrs = status_sensor.attributes
302+
assert status_sensor.state == "green"
303+
assert status_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Threshold"
304+
assert status_sensor_attrs[ATTR_OPTIONS] == ["error", "green", "yellow", "red"]
305+
294306
# Check device context for the battery sensor
295307
entity = entity_registry.async_get("sensor.aranetrn_12345_battery")
296308
device = device_registry.async_get(entity.device_id)

0 commit comments

Comments
 (0)