Add more sensors to SensorEntityDescription for RFLink (#82036)

* Add more sensors to SensorEntityDescription

* changes from comments

* add device_class precipitation

* fix test

* change state_class for total_rain
This commit is contained in:
mbo18 2022-11-26 05:40:02 +01:00 committed by GitHub
parent fb132f8a26
commit 6f1208b07f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 209 additions and 47 deletions

View file

@ -17,6 +17,7 @@ from homeassistant.const import (
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
PRECIPITATION_MILLIMETERS,
STATE_UNKNOWN,
UnitOfTemperature,
)
@ -87,17 +88,15 @@ async def test_default_setup(hass, monkeypatch):
) # temperature uses SensorEntityDescription
# test event for new unconfigured sensor
event_callback(
{"id": "test3", "sensor": "humidity", "value": 43, "unit": PERCENTAGE}
)
event_callback({"id": "test3", "sensor": "battery", "value": "ok", "unit": None})
await hass.async_block_till_done()
# test state of hum sensor
hum_sensor = hass.states.get("sensor.test3")
assert hum_sensor
assert hum_sensor.state == "43"
assert hum_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE
assert hum_sensor.attributes[ATTR_ICON] == "mdi:water-percent"
# test state of battery sensor
bat_sensor = hass.states.get("sensor.test3")
assert bat_sensor
assert bat_sensor.state == "ok"
assert ATTR_UNIT_OF_MEASUREMENT not in bat_sensor.attributes
assert bat_sensor.attributes[ATTR_ICON] == "mdi:battery"
async def test_disable_automatic_add(hass, monkeypatch):
@ -195,7 +194,7 @@ async def test_aliases(hass, monkeypatch):
)
await hass.async_block_till_done()
# test state of new sensor
# test state of new sensor
updated_sensor = hass.states.get("sensor.test_02")
assert updated_sensor
assert updated_sensor.state == "65"
@ -221,11 +220,11 @@ async def test_race_condition(hass, monkeypatch):
await hass.async_block_till_done()
# test state of new sensor
# test state of new sensor
updated_sensor = hass.states.get("sensor.test3")
assert updated_sensor
# test state of new sensor
# test state of new sensor
new_sensor = hass.states.get(f"{DOMAIN}.test3")
assert new_sensor
assert new_sensor.state == "ok"
@ -235,7 +234,7 @@ async def test_race_condition(hass, monkeypatch):
# tmp_entity must be deleted from EVENT_KEY_COMMAND
assert tmp_entity not in hass.data[DATA_ENTITY_LOOKUP][EVENT_KEY_SENSOR]["test3"]
# test state of new sensor
# test state of new sensor
new_sensor = hass.states.get(f"{DOMAIN}.test3")
assert new_sensor
assert new_sensor.state == "ko"
@ -249,6 +248,14 @@ async def test_sensor_attributes(hass, monkeypatch):
DOMAIN: {
"platform": "rflink",
"devices": {
"my_meter_device_unique_id": {
"name": "meter_device",
"sensor_type": "meter_value",
},
"my_rain_device_unique_id": {
"name": "rain_device",
"sensor_type": "total_rain",
},
"my_humidity_device_unique_id": {
"name": "humidity_device",
"sensor_type": "humidity",
@ -270,10 +277,22 @@ async def test_sensor_attributes(hass, monkeypatch):
event_callback, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)
# test sensor loaded from config
meter_state = hass.states.get("sensor.meter_device")
assert meter_state
assert "device_class" not in meter_state.attributes
assert "state_class" not in meter_state.attributes
assert "unit_of_measurement" not in meter_state.attributes
rain_state = hass.states.get("sensor.rain_device")
assert rain_state
assert rain_state.attributes["device_class"] == SensorDeviceClass.PRECIPITATION
assert rain_state.attributes["state_class"] == SensorStateClass.TOTAL_INCREASING
assert rain_state.attributes["unit_of_measurement"] == PRECIPITATION_MILLIMETERS
humidity_state = hass.states.get("sensor.humidity_device")
assert humidity_state
assert "device_class" not in humidity_state.attributes
assert "state_class" not in humidity_state.attributes
assert humidity_state.attributes["device_class"] == SensorDeviceClass.HUMIDITY
assert humidity_state.attributes["state_class"] == SensorStateClass.MEASUREMENT
assert humidity_state.attributes["unit_of_measurement"] == PERCENTAGE
temperature_state = hass.states.get("sensor.temperature_device")