Fix RFLink unit conversions (#79436)

* Fix unit conversions

* Fix tests

* Address requests changes

* Address requests changes

* Address requests changes

* Address requests changes

* migrate to SensorEntityDescription

* forgotten test

* forgotten test

* chango to WIND_SPEED

* manage UOM

* fix test

* address comments

* fix implementation

* prevent from mutate config
This commit is contained in:
javicalle
2022-10-29 00:20:26 +02:00
committed by GitHub
parent b7a651519b
commit 4dbb265590
4 changed files with 222 additions and 21 deletions

View File

@@ -12,11 +12,13 @@ from homeassistant.components.rflink import (
EVENT_KEY_SENSOR,
TMP_ENTITY,
)
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import (
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
STATE_UNKNOWN,
TEMP_CELSIUS,
UnitOfTemperature,
)
from .test_init import mock_rflink
@@ -47,11 +49,18 @@ async def test_default_setup(hass, monkeypatch):
config_sensor = hass.states.get("sensor.test")
assert config_sensor
assert config_sensor.state == "unknown"
assert config_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
assert (
config_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
)
# test event for config sensor
event_callback(
{"id": "test", "sensor": "temperature", "value": 1, "unit": TEMP_CELSIUS}
{
"id": "test",
"sensor": "temperature",
"value": 1,
"unit": UnitOfTemperature.CELSIUS,
}
)
await hass.async_block_till_done()
@@ -59,16 +68,36 @@ async def test_default_setup(hass, monkeypatch):
# test event for new unconfigured sensor
event_callback(
{"id": "test2", "sensor": "temperature", "value": 0, "unit": TEMP_CELSIUS}
{
"id": "test2",
"sensor": "temperature",
"value": 0,
"unit": UnitOfTemperature.CELSIUS,
}
)
await hass.async_block_till_done()
# test state of new sensor
new_sensor = hass.states.get("sensor.test2")
assert new_sensor
assert new_sensor.state == "0"
assert new_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
assert new_sensor.attributes["icon"] == "mdi:thermometer"
# test state of temp sensor
temp_sensor = hass.states.get("sensor.test2")
assert temp_sensor
assert temp_sensor.state == "0"
assert temp_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
assert (
ATTR_ICON not in temp_sensor.attributes
) # temperature uses SensorEntityDescription
# test event for new unconfigured sensor
event_callback(
{"id": "test3", "sensor": "humidity", "value": 43, "unit": PERCENTAGE}
)
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"
async def test_disable_automatic_add(hass, monkeypatch):
@@ -83,7 +112,12 @@ async def test_disable_automatic_add(hass, monkeypatch):
# test event for new unconfigured sensor
event_callback(
{"id": "test2", "sensor": "temperature", "value": 0, "unit": TEMP_CELSIUS}
{
"id": "test2",
"sensor": "temperature",
"value": 0,
"unit": UnitOfTemperature.CELSIUS,
}
)
await hass.async_block_till_done()
@@ -205,3 +239,53 @@ async def test_race_condition(hass, monkeypatch):
new_sensor = hass.states.get(f"{DOMAIN}.test3")
assert new_sensor
assert new_sensor.state == "ko"
async def test_sensor_attributes(hass, monkeypatch):
"""Validate the sensor attributes."""
config = {
"rflink": {"port": "/dev/ttyABC0"},
DOMAIN: {
"platform": "rflink",
"devices": {
"my_humidity_device_unique_id": {
"name": "humidity_device",
"sensor_type": "humidity",
},
"my_temperature_device_unique_id": {
"name": "temperature_device",
"sensor_type": "temperature",
},
"another_temperature_device_unique_id": {
"name": "fahrenheit_device",
"sensor_type": "temperature",
"unit_of_measurement": "F",
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)
# test sensor loaded from config
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["unit_of_measurement"] == PERCENTAGE
temperature_state = hass.states.get("sensor.temperature_device")
assert temperature_state
assert temperature_state.attributes["device_class"] == SensorDeviceClass.TEMPERATURE
assert temperature_state.attributes["state_class"] == SensorStateClass.MEASUREMENT
assert (
temperature_state.attributes["unit_of_measurement"] == UnitOfTemperature.CELSIUS
)
fahrenheit_state = hass.states.get("sensor.fahrenheit_device")
assert fahrenheit_state
assert fahrenheit_state.attributes["device_class"] == SensorDeviceClass.TEMPERATURE
assert fahrenheit_state.attributes["state_class"] == SensorStateClass.MEASUREMENT
assert fahrenheit_state.attributes["unit_of_measurement"] == "F"