Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concentration level sensor to Aranet #137291

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions homeassistant/components/aranet/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass
from typing import Any

from aranet4.client import Aranet4Advertisement
from aranet4.client import Aranet4Advertisement, Color
from bleak.backends.device import BLEDevice

from homeassistant.components.bluetooth.passive_update_processor import (
Expand Down Expand Up @@ -74,6 +74,13 @@ class AranetSensorEntityDescription(SensorEntityDescription):
native_unit_of_measurement=UnitOfPressure.HPA,
state_class=SensorStateClass.MEASUREMENT,
),
"status": AranetSensorEntityDescription(
key="concentration_level",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for all the other sensors here we use the device key as the sensor key and translation_key.
should we use status_level for this one too, to keep it consistent?

translation_key="concentration_level",
name="Concentration Level",
device_class=SensorDeviceClass.ENUM,
options=[status.name.lower() for status in Color],
),
"co2": AranetSensorEntityDescription(
key="co2",
name="Carbon Dioxide",
Expand Down Expand Up @@ -161,7 +168,9 @@ def sensor_update_to_bluetooth_data_update(
val = getattr(adv.readings, key)
if val == -1:
continue
val *= desc.scale
if key == "status":
val = val.name.lower()
val = val * desc.scale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should scaling be applied for the string?

data[tag] = val
names[tag] = desc.name
descs[tag] = desc
Expand Down
11 changes: 11 additions & 0 deletions homeassistant/components/aranet/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also add error since it is one of the enum values?

"yellow": "Elevated",
"red": "Unhealthy"
}
}
}
}
}
22 changes: 19 additions & 3 deletions tests/components/aranet/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,7 +170,7 @@ async def test_sensors_aranet4(
assert len(hass.states.async_all("sensor")) == 0
inject_bluetooth_service_info(hass, VALID_DATA_SERVICE_INFO)
await hass.async_block_till_done()
assert len(hass.states.async_all("sensor")) == 6
assert len(hass.states.async_all("sensor")) == 7

batt_sensor = hass.states.get("sensor.aranet4_12345_battery")
batt_sensor_attrs = batt_sensor.attributes
Expand Down Expand Up @@ -214,6 +214,14 @@ async def test_sensors_aranet4(
assert interval_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "s"
assert interval_sensor_attrs[ATTR_STATE_CLASS] == "measurement"

status_sensor = hass.states.get("sensor.aranet4_12345_concentration_level")
status_sensor_attrs = status_sensor.attributes
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")
device = device_registry.async_get(entity.device_id)
Expand Down Expand Up @@ -245,7 +253,7 @@ async def test_sensors_aranetrn(
assert len(hass.states.async_all("sensor")) == 0
inject_bluetooth_service_info(hass, VALID_ARANET_RADON_DATA_SERVICE_INFO)
await hass.async_block_till_done()
assert len(hass.states.async_all("sensor")) == 6
assert len(hass.states.async_all("sensor")) == 7

batt_sensor = hass.states.get("sensor.aranetrn_12345_battery")
batt_sensor_attrs = batt_sensor.attributes
Expand Down Expand Up @@ -291,6 +299,14 @@ async def test_sensors_aranetrn(
assert interval_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "s"
assert interval_sensor_attrs[ATTR_STATE_CLASS] == "measurement"

status_sensor = hass.states.get("sensor.aranetrn_12345_concentration_level")
status_sensor_attrs = status_sensor.attributes
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")
device = device_registry.async_get(entity.device_id)
Expand Down