Test sensor unit conversion (#59546)

pull/59559/head
Erik Montnemery 2021-11-11 19:36:53 +01:00 committed by GitHub
parent 6636287c59
commit d1ee041997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 1 deletions

View File

@ -1,8 +1,55 @@
"""The test for sensor device automation."""
import pytest
from pytest import approx
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
@pytest.mark.parametrize(
"unit_system,native_unit,state_unit,native_value,state_value",
[
(IMPERIAL_SYSTEM, TEMP_FAHRENHEIT, TEMP_FAHRENHEIT, 100, 100),
(IMPERIAL_SYSTEM, TEMP_CELSIUS, TEMP_FAHRENHEIT, 38, 100),
(METRIC_SYSTEM, TEMP_FAHRENHEIT, TEMP_CELSIUS, 100, 38),
(METRIC_SYSTEM, TEMP_CELSIUS, TEMP_CELSIUS, 38, 38),
],
)
async def test_temperature_conversion(
hass,
enable_custom_integrations,
unit_system,
native_unit,
state_unit,
native_value,
state_value,
):
"""Test temperature conversion."""
hass.config.units = unit_system
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test",
native_value=str(native_value),
native_unit_of_measurement=native_unit,
device_class=DEVICE_CLASS_TEMPERATURE,
)
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done()
state = hass.states.get(entity0.entity_id)
assert float(state.state) == approx(float(state_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == state_unit
async def test_deprecated_temperature_conversion(