2020-12-02 12:50:48 +00:00
|
|
|
"""The tests for the Number component."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2024-07-01 10:09:11 +00:00
|
|
|
from collections.abc import Generator
|
2023-02-15 09:50:02 +00:00
|
|
|
from typing import Any
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import MagicMock
|
2020-12-02 12:50:48 +00:00
|
|
|
|
2021-08-04 09:57:26 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.number import (
|
2022-06-14 07:40:57 +00:00
|
|
|
ATTR_MAX,
|
|
|
|
ATTR_MIN,
|
2023-10-22 21:55:13 +00:00
|
|
|
ATTR_MODE,
|
2021-08-04 09:57:26 +00:00
|
|
|
ATTR_STEP,
|
|
|
|
ATTR_VALUE,
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
2022-06-14 07:40:57 +00:00
|
|
|
NumberDeviceClass,
|
2021-08-04 09:57:26 +00:00
|
|
|
NumberEntity,
|
2022-06-14 07:40:57 +00:00
|
|
|
NumberEntityDescription,
|
2023-06-22 21:48:49 +00:00
|
|
|
NumberMode,
|
2022-06-14 07:40:57 +00:00
|
|
|
)
|
2023-01-12 08:34:10 +00:00
|
|
|
from homeassistant.components.number.const import (
|
|
|
|
DEVICE_CLASS_UNITS as NUMBER_DEVICE_CLASS_UNITS,
|
|
|
|
)
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_UNITS as SENSOR_DEVICE_CLASS_UNITS,
|
2023-07-20 09:10:03 +00:00
|
|
|
NON_NUMERIC_DEVICE_CLASSES,
|
2023-01-12 08:34:10 +00:00
|
|
|
SensorDeviceClass,
|
|
|
|
)
|
2023-06-22 21:48:49 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
2022-06-14 07:40:57 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
CONF_PLATFORM,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature,
|
2024-01-30 14:01:08 +00:00
|
|
|
UnitOfVolumeFlowRate,
|
2021-08-04 09:57:26 +00:00
|
|
|
)
|
2022-06-14 17:56:27 +00:00
|
|
|
from homeassistant.core import HomeAssistant, State
|
2024-04-02 10:22:00 +00:00
|
|
|
from homeassistant.exceptions import ServiceValidationError
|
2022-07-11 12:49:36 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2023-06-22 21:48:49 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-06-14 17:56:27 +00:00
|
|
|
from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY
|
2021-08-04 09:57:26 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2022-10-20 13:42:23 +00:00
|
|
|
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
|
2020-12-03 17:35:17 +00:00
|
|
|
|
2024-06-10 11:32:31 +00:00
|
|
|
from . import common
|
|
|
|
|
2023-05-31 01:48:17 +00:00
|
|
|
from tests.common import (
|
2023-06-22 21:48:49 +00:00
|
|
|
MockConfigEntry,
|
|
|
|
MockModule,
|
|
|
|
MockPlatform,
|
2023-05-31 01:48:17 +00:00
|
|
|
async_mock_restore_state_shutdown_restart,
|
2023-06-22 21:48:49 +00:00
|
|
|
mock_config_flow,
|
|
|
|
mock_integration,
|
|
|
|
mock_platform,
|
2023-05-31 01:48:17 +00:00
|
|
|
mock_restore_cache_with_extra_data,
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform,
|
2023-05-31 01:48:17 +00:00
|
|
|
)
|
2022-06-14 17:56:27 +00:00
|
|
|
|
2023-06-22 21:48:49 +00:00
|
|
|
TEST_DOMAIN = "test"
|
|
|
|
|
2020-12-03 17:35:17 +00:00
|
|
|
|
|
|
|
class MockDefaultNumberEntity(NumberEntity):
|
2022-06-14 07:40:57 +00:00
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class falls back on defaults for min_value, max_value, step.
|
|
|
|
"""
|
2020-12-03 17:35:17 +00:00
|
|
|
|
|
|
|
@property
|
2022-06-14 07:40:57 +00:00
|
|
|
def native_value(self):
|
2020-12-03 17:35:17 +00:00
|
|
|
"""Return the current value."""
|
|
|
|
return 0.5
|
|
|
|
|
2020-12-02 12:50:48 +00:00
|
|
|
|
|
|
|
class MockNumberEntity(NumberEntity):
|
2022-06-14 07:40:57 +00:00
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class customizes min_value, max_value as overridden methods.
|
|
|
|
Step is calculated based on the smaller max_value and min_value.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_max_value(self) -> float:
|
|
|
|
"""Return the max value."""
|
|
|
|
return 0.5
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_min_value(self) -> float:
|
|
|
|
"""Return the min value."""
|
|
|
|
return -0.5
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
"""Return the current value."""
|
|
|
|
return "native_cats"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the current value."""
|
|
|
|
return 0.5
|
|
|
|
|
|
|
|
|
|
|
|
class MockNumberEntityAttr(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class customizes min_value, max_value by setting _attr members.
|
|
|
|
Step is calculated based on the smaller max_value and min_value.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_attr_native_max_value = 1000.0
|
|
|
|
_attr_native_min_value = -1000.0
|
|
|
|
_attr_native_step = 100.0
|
|
|
|
_attr_native_unit_of_measurement = "native_dogs"
|
|
|
|
_attr_native_value = 500.0
|
|
|
|
|
|
|
|
|
|
|
|
class MockNumberEntityDescr(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class customizes min_value, max_value by entity description.
|
|
|
|
Step is calculated based on the smaller max_value and min_value.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the clas instance."""
|
|
|
|
self.entity_description = NumberEntityDescription(
|
|
|
|
"test",
|
|
|
|
native_max_value=10.0,
|
|
|
|
native_min_value=-10.0,
|
|
|
|
native_step=2.0,
|
|
|
|
native_unit_of_measurement="native_rabbits",
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the current value."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-12-23 23:20:24 +00:00
|
|
|
class MockNumberEntityAttrWithDescription(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class sets an entity description and overrides
|
|
|
|
all the values with _attr members to ensure the _attr
|
|
|
|
members take precedence over the entity description.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the clas instance."""
|
|
|
|
self.entity_description = NumberEntityDescription(
|
|
|
|
"test",
|
|
|
|
native_max_value=10.0,
|
|
|
|
native_min_value=-10.0,
|
|
|
|
native_step=2.0,
|
|
|
|
native_unit_of_measurement="native_rabbits",
|
|
|
|
)
|
|
|
|
|
|
|
|
_attr_native_max_value = 1000.0
|
|
|
|
_attr_native_min_value = -1000.0
|
|
|
|
_attr_native_step = 100.0
|
|
|
|
_attr_native_unit_of_measurement = "native_dogs"
|
|
|
|
_attr_native_value = 500.0
|
|
|
|
|
|
|
|
|
2022-06-14 07:40:57 +00:00
|
|
|
class MockDefaultNumberEntityDeprecated(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class falls back on defaults for min_value, max_value, step.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the current value."""
|
|
|
|
return 0.5
|
|
|
|
|
|
|
|
|
|
|
|
class MockNumberEntityDeprecated(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class customizes min_value, max_value as overridden methods.
|
|
|
|
Step is calculated based on the smaller max_value and min_value.
|
|
|
|
"""
|
2020-12-02 12:50:48 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_value(self) -> float:
|
|
|
|
"""Return the max value."""
|
2022-06-14 07:40:57 +00:00
|
|
|
return 0.5
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_value(self) -> float:
|
|
|
|
"""Return the min value."""
|
|
|
|
return -0.5
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the current value."""
|
|
|
|
return "cats"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
"""Return the current value."""
|
|
|
|
return 0.5
|
|
|
|
|
|
|
|
|
|
|
|
class MockNumberEntityAttrDeprecated(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class customizes min_value, max_value by setting _attr members.
|
|
|
|
Step is calculated based on the smaller max_value and min_value.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_attr_max_value = 1000.0
|
|
|
|
_attr_min_value = -1000.0
|
|
|
|
_attr_step = 100.0
|
|
|
|
_attr_unit_of_measurement = "dogs"
|
|
|
|
_attr_value = 500.0
|
|
|
|
|
|
|
|
|
|
|
|
class MockNumberEntityDescrDeprecated(NumberEntity):
|
|
|
|
"""Mock NumberEntity device to use in tests.
|
|
|
|
|
|
|
|
This class customizes min_value, max_value by entity description.
|
|
|
|
Step is calculated based on the smaller max_value and min_value.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the clas instance."""
|
|
|
|
self.entity_description = NumberEntityDescription(
|
|
|
|
"test",
|
|
|
|
max_value=10.0,
|
|
|
|
min_value=-10.0,
|
|
|
|
step=2.0,
|
|
|
|
unit_of_measurement="rabbits",
|
|
|
|
)
|
2020-12-02 12:50:48 +00:00
|
|
|
|
|
|
|
@property
|
2020-12-03 17:35:17 +00:00
|
|
|
def value(self):
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Return the current value."""
|
2020-12-03 17:35:17 +00:00
|
|
|
return 0.5
|
2020-12-02 12:50:48 +00:00
|
|
|
|
|
|
|
|
2021-08-04 09:57:26 +00:00
|
|
|
async def test_step(hass: HomeAssistant) -> None:
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Test the step calculation."""
|
2020-12-03 17:35:17 +00:00
|
|
|
number = MockDefaultNumberEntity()
|
2022-06-14 07:40:57 +00:00
|
|
|
number.hass = hass
|
|
|
|
assert number.step == 1.0
|
|
|
|
|
|
|
|
number_2 = MockNumberEntity()
|
|
|
|
number_2.hass = hass
|
|
|
|
assert number_2.step == 0.1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_attributes(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the attributes."""
|
|
|
|
number = MockDefaultNumberEntity()
|
|
|
|
number.hass = hass
|
|
|
|
assert number.max_value == 100.0
|
|
|
|
assert number.min_value == 0.0
|
2020-12-02 12:50:48 +00:00
|
|
|
assert number.step == 1.0
|
2022-06-14 07:40:57 +00:00
|
|
|
assert number.unit_of_measurement is None
|
|
|
|
assert number.value == 0.5
|
2023-10-22 21:55:13 +00:00
|
|
|
assert number.capability_attributes == {
|
|
|
|
ATTR_MAX: 100.0,
|
|
|
|
ATTR_MIN: 0.0,
|
|
|
|
ATTR_MODE: NumberMode.AUTO,
|
|
|
|
ATTR_STEP: 1.0,
|
|
|
|
}
|
2020-12-02 12:50:48 +00:00
|
|
|
|
|
|
|
number_2 = MockNumberEntity()
|
2022-06-14 07:40:57 +00:00
|
|
|
number_2.hass = hass
|
|
|
|
assert number_2.max_value == 0.5
|
|
|
|
assert number_2.min_value == -0.5
|
2020-12-02 12:50:48 +00:00
|
|
|
assert number_2.step == 0.1
|
2022-06-14 07:40:57 +00:00
|
|
|
assert number_2.unit_of_measurement == "native_cats"
|
|
|
|
assert number_2.value == 0.5
|
2023-10-22 21:55:13 +00:00
|
|
|
assert number_2.capability_attributes == {
|
|
|
|
ATTR_MAX: 0.5,
|
|
|
|
ATTR_MIN: -0.5,
|
|
|
|
ATTR_MODE: NumberMode.AUTO,
|
|
|
|
ATTR_STEP: 0.1,
|
|
|
|
}
|
2022-06-14 07:40:57 +00:00
|
|
|
|
|
|
|
number_3 = MockNumberEntityAttr()
|
|
|
|
number_3.hass = hass
|
|
|
|
assert number_3.max_value == 1000.0
|
|
|
|
assert number_3.min_value == -1000.0
|
|
|
|
assert number_3.step == 100.0
|
|
|
|
assert number_3.unit_of_measurement == "native_dogs"
|
|
|
|
assert number_3.value == 500.0
|
2023-10-22 21:55:13 +00:00
|
|
|
assert number_3.capability_attributes == {
|
|
|
|
ATTR_MAX: 1000.0,
|
|
|
|
ATTR_MIN: -1000.0,
|
|
|
|
ATTR_MODE: NumberMode.AUTO,
|
|
|
|
ATTR_STEP: 100.0,
|
|
|
|
}
|
2022-06-14 07:40:57 +00:00
|
|
|
|
|
|
|
number_4 = MockNumberEntityDescr()
|
|
|
|
number_4.hass = hass
|
|
|
|
assert number_4.max_value == 10.0
|
|
|
|
assert number_4.min_value == -10.0
|
|
|
|
assert number_4.step == 2.0
|
|
|
|
assert number_4.unit_of_measurement == "native_rabbits"
|
|
|
|
assert number_4.value is None
|
2023-10-22 21:55:13 +00:00
|
|
|
assert number_4.capability_attributes == {
|
|
|
|
ATTR_MAX: 10.0,
|
|
|
|
ATTR_MIN: -10.0,
|
|
|
|
ATTR_MODE: NumberMode.AUTO,
|
|
|
|
ATTR_STEP: 2.0,
|
|
|
|
}
|
2022-06-14 07:40:57 +00:00
|
|
|
|
2023-12-23 23:20:24 +00:00
|
|
|
number_5 = MockNumberEntityAttrWithDescription()
|
|
|
|
number_5.hass = hass
|
|
|
|
assert number_5.max_value == 1000.0
|
|
|
|
assert number_5.min_value == -1000.0
|
|
|
|
assert number_5.step == 100.0
|
|
|
|
assert number_5.native_step == 100.0
|
|
|
|
assert number_5.unit_of_measurement == "native_dogs"
|
|
|
|
assert number_5.value == 500.0
|
|
|
|
assert number_5.capability_attributes == {
|
|
|
|
ATTR_MAX: 1000.0,
|
|
|
|
ATTR_MIN: -1000.0,
|
|
|
|
ATTR_MODE: NumberMode.AUTO,
|
|
|
|
ATTR_STEP: 100.0,
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:40:57 +00:00
|
|
|
|
2021-08-04 09:57:26 +00:00
|
|
|
async def test_sync_set_value(hass: HomeAssistant) -> None:
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Test if async set_value calls sync set_value."""
|
2020-12-03 17:35:17 +00:00
|
|
|
number = MockDefaultNumberEntity()
|
2020-12-02 12:50:48 +00:00
|
|
|
number.hass = hass
|
|
|
|
|
|
|
|
number.set_value = MagicMock()
|
|
|
|
await number.async_set_value(42)
|
|
|
|
|
|
|
|
assert number.set_value.called
|
|
|
|
assert number.set_value.call_args[0][0] == 42
|
2021-08-04 09:57:26 +00:00
|
|
|
|
|
|
|
|
2024-03-26 20:56:38 +00:00
|
|
|
async def test_set_value(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_number_entities: list[MockNumberEntity],
|
|
|
|
) -> None:
|
2021-08-04 09:57:26 +00:00
|
|
|
"""Test we can only set valid values."""
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform(hass, DOMAIN, mock_number_entities)
|
2021-08-04 09:57:26 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("number.test")
|
|
|
|
assert state.state == "50.0"
|
|
|
|
assert state.attributes.get(ATTR_STEP) == 1.0
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
{ATTR_VALUE: 60.0, ATTR_ENTITY_ID: "number.test"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2022-06-14 07:40:57 +00:00
|
|
|
|
2021-08-04 09:57:26 +00:00
|
|
|
state = hass.states.get("number.test")
|
|
|
|
assert state.state == "60.0"
|
|
|
|
|
2024-04-02 10:22:00 +00:00
|
|
|
# test range validation
|
|
|
|
with pytest.raises(ServiceValidationError) as exc:
|
2021-08-04 09:57:26 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
{ATTR_VALUE: 110.0, ATTR_ENTITY_ID: "number.test"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2024-04-02 10:22:00 +00:00
|
|
|
assert exc.value.translation_domain == DOMAIN
|
|
|
|
assert exc.value.translation_key == "out_of_range"
|
|
|
|
assert (
|
|
|
|
str(exc.value)
|
|
|
|
== "Value 110.0 for number.test is outside valid range 0.0 - 100.0"
|
|
|
|
)
|
2021-08-04 09:57:26 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("number.test")
|
|
|
|
assert state.state == "60.0"
|
2022-06-14 07:40:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
(
|
|
|
|
"unit_system",
|
|
|
|
"native_unit",
|
|
|
|
"state_unit",
|
|
|
|
"initial_native_value",
|
|
|
|
"initial_state_value",
|
|
|
|
"updated_native_value",
|
|
|
|
"updated_state_value",
|
|
|
|
"native_max_value",
|
|
|
|
"state_max_value",
|
|
|
|
"native_min_value",
|
|
|
|
"state_min_value",
|
|
|
|
"native_step",
|
|
|
|
"state_step",
|
|
|
|
),
|
2022-06-14 07:40:57 +00:00
|
|
|
[
|
|
|
|
(
|
2022-10-20 13:42:23 +00:00
|
|
|
US_CUSTOMARY_SYSTEM,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
2022-06-14 07:40:57 +00:00
|
|
|
100,
|
|
|
|
100,
|
|
|
|
50,
|
|
|
|
50,
|
|
|
|
140,
|
|
|
|
140,
|
|
|
|
-9,
|
|
|
|
-9,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
),
|
|
|
|
(
|
2022-10-20 13:42:23 +00:00
|
|
|
US_CUSTOMARY_SYSTEM,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
2022-06-14 07:40:57 +00:00
|
|
|
38,
|
|
|
|
100,
|
|
|
|
10,
|
|
|
|
50,
|
|
|
|
60,
|
|
|
|
140,
|
|
|
|
-23,
|
|
|
|
-10,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
METRIC_SYSTEM,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-06-14 07:40:57 +00:00
|
|
|
100,
|
|
|
|
38,
|
|
|
|
50,
|
|
|
|
10,
|
|
|
|
140,
|
|
|
|
60,
|
|
|
|
-9,
|
|
|
|
-23,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
METRIC_SYSTEM,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-06-14 07:40:57 +00:00
|
|
|
38,
|
|
|
|
38,
|
|
|
|
10,
|
|
|
|
10,
|
|
|
|
60,
|
|
|
|
60,
|
|
|
|
-23,
|
|
|
|
-23,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_temperature_conversion(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
2022-06-14 07:40:57 +00:00
|
|
|
unit_system,
|
|
|
|
native_unit,
|
|
|
|
state_unit,
|
|
|
|
initial_native_value,
|
|
|
|
initial_state_value,
|
|
|
|
updated_native_value,
|
|
|
|
updated_state_value,
|
|
|
|
native_max_value,
|
|
|
|
state_max_value,
|
|
|
|
native_min_value,
|
|
|
|
state_min_value,
|
|
|
|
native_step,
|
|
|
|
state_step,
|
2023-02-15 09:50:02 +00:00
|
|
|
) -> None:
|
2022-06-14 07:40:57 +00:00
|
|
|
"""Test temperature conversion."""
|
|
|
|
hass.config.units = unit_system
|
2024-03-26 20:56:38 +00:00
|
|
|
entity0 = common.MockNumberEntity(
|
|
|
|
name="Test",
|
|
|
|
native_max_value=native_max_value,
|
|
|
|
native_min_value=native_min_value,
|
|
|
|
native_step=native_step,
|
|
|
|
native_unit_of_measurement=native_unit,
|
|
|
|
native_value=initial_native_value,
|
|
|
|
device_class=NumberDeviceClass.TEMPERATURE,
|
2022-06-14 07:40:57 +00:00
|
|
|
)
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
2022-06-14 07:40:57 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(initial_state_value))
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == state_unit
|
|
|
|
assert state.attributes[ATTR_MAX] == state_max_value
|
|
|
|
assert state.attributes[ATTR_MIN] == state_min_value
|
|
|
|
assert state.attributes[ATTR_STEP] == state_step
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
{ATTR_VALUE: updated_state_value, ATTR_ENTITY_ID: entity0.entity_id},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(updated_state_value))
|
|
|
|
assert entity0._values["native_value"] == updated_native_value
|
|
|
|
|
|
|
|
# Set to the minimum value
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
{ATTR_VALUE: state_min_value, ATTR_ENTITY_ID: entity0.entity_id},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(state_min_value), rel=0.1)
|
|
|
|
|
|
|
|
# Set to the maximum value
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_VALUE,
|
|
|
|
{ATTR_VALUE: state_max_value, ATTR_ENTITY_ID: entity0.entity_id},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(state_max_value), rel=0.1)
|
2022-06-14 17:56:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
RESTORE_DATA = {
|
|
|
|
"native_max_value": 200.0,
|
|
|
|
"native_min_value": -10.0,
|
|
|
|
"native_step": 2.0,
|
|
|
|
"native_unit_of_measurement": "°F",
|
|
|
|
"native_value": 123.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_restore_number_save_state(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_storage: dict[str, Any],
|
|
|
|
) -> None:
|
2022-06-14 17:56:27 +00:00
|
|
|
"""Test RestoreNumber."""
|
2024-03-26 20:56:38 +00:00
|
|
|
entity0 = common.MockRestoreNumber(
|
|
|
|
name="Test",
|
|
|
|
native_max_value=200.0,
|
|
|
|
native_min_value=-10.0,
|
|
|
|
native_step=2.0,
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
|
|
|
|
native_value=123.0,
|
|
|
|
device_class=NumberDeviceClass.TEMPERATURE,
|
2022-06-14 17:56:27 +00:00
|
|
|
)
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
2022-06-14 17:56:27 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(hass, "number", {"number": {"platform": "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Trigger saving state
|
2023-05-31 01:48:17 +00:00
|
|
|
await async_mock_restore_state_shutdown_restart(hass)
|
2022-06-14 17:56:27 +00:00
|
|
|
|
|
|
|
assert len(hass_storage[RESTORE_STATE_KEY]["data"]) == 1
|
|
|
|
state = hass_storage[RESTORE_STATE_KEY]["data"][0]["state"]
|
|
|
|
assert state["entity_id"] == entity0.entity_id
|
|
|
|
extra_data = hass_storage[RESTORE_STATE_KEY]["data"][0]["extra_data"]
|
|
|
|
assert extra_data == RESTORE_DATA
|
2023-08-19 12:17:17 +00:00
|
|
|
assert isinstance(extra_data["native_value"], float)
|
2022-06-14 17:56:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
(
|
|
|
|
"native_max_value",
|
|
|
|
"native_min_value",
|
|
|
|
"native_step",
|
|
|
|
"native_value",
|
|
|
|
"native_value_type",
|
|
|
|
"extra_data",
|
|
|
|
"device_class",
|
|
|
|
"uom",
|
|
|
|
),
|
2022-06-14 17:56:27 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
200.0,
|
|
|
|
-10.0,
|
|
|
|
2.0,
|
|
|
|
123.0,
|
|
|
|
float,
|
|
|
|
RESTORE_DATA,
|
|
|
|
NumberDeviceClass.TEMPERATURE,
|
|
|
|
"°F",
|
|
|
|
),
|
|
|
|
(100.0, 0.0, None, None, type(None), None, None, None),
|
|
|
|
(100.0, 0.0, None, None, type(None), {}, None, None),
|
|
|
|
(100.0, 0.0, None, None, type(None), {"beer": 123}, None, None),
|
|
|
|
(
|
|
|
|
100.0,
|
|
|
|
0.0,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
type(None),
|
|
|
|
{"native_unit_of_measurement": "°F", "native_value": {}},
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_restore_number_restore_state(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_storage: dict[str, Any],
|
2022-06-14 17:56:27 +00:00
|
|
|
native_max_value,
|
|
|
|
native_min_value,
|
|
|
|
native_step,
|
|
|
|
native_value,
|
|
|
|
native_value_type,
|
|
|
|
extra_data,
|
|
|
|
device_class,
|
|
|
|
uom,
|
2023-02-15 09:50:02 +00:00
|
|
|
) -> None:
|
2022-06-14 17:56:27 +00:00
|
|
|
"""Test RestoreNumber."""
|
|
|
|
mock_restore_cache_with_extra_data(hass, ((State("number.test", ""), extra_data),))
|
|
|
|
|
2024-03-26 20:56:38 +00:00
|
|
|
entity0 = common.MockRestoreNumber(
|
|
|
|
device_class=device_class,
|
|
|
|
name="Test",
|
|
|
|
native_value=None,
|
2022-06-14 17:56:27 +00:00
|
|
|
)
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
2022-06-14 17:56:27 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(hass, "number", {"number": {"platform": "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(entity0.entity_id)
|
|
|
|
|
|
|
|
assert entity0.native_max_value == native_max_value
|
|
|
|
assert entity0.native_min_value == native_min_value
|
|
|
|
assert entity0.native_step == native_step
|
|
|
|
assert entity0.native_value == native_value
|
2024-06-28 09:56:49 +00:00
|
|
|
assert type(entity0.native_value) is native_value_type
|
2022-06-14 17:56:27 +00:00
|
|
|
assert entity0.native_unit_of_measurement == uom
|
2022-07-11 12:49:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
(
|
|
|
|
"device_class",
|
|
|
|
"native_unit",
|
|
|
|
"custom_unit",
|
|
|
|
"state_unit",
|
|
|
|
"native_value",
|
|
|
|
"custom_value",
|
|
|
|
),
|
2022-07-11 12:49:36 +00:00
|
|
|
[
|
|
|
|
# Not a supported temperature unit
|
|
|
|
(
|
|
|
|
NumberDeviceClass.TEMPERATURE,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-07-11 12:49:36 +00:00
|
|
|
"my_temperature_unit",
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-07-11 12:49:36 +00:00
|
|
|
1000,
|
|
|
|
1000,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
NumberDeviceClass.TEMPERATURE,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
2022-07-11 12:49:36 +00:00
|
|
|
37.5,
|
|
|
|
99.5,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
NumberDeviceClass.TEMPERATURE,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-07-11 12:49:36 +00:00
|
|
|
100,
|
|
|
|
38.0,
|
|
|
|
),
|
2024-01-30 14:01:08 +00:00
|
|
|
(
|
2024-02-04 14:01:06 +00:00
|
|
|
NumberDeviceClass.VOLUME_FLOW_RATE,
|
2024-01-30 14:01:08 +00:00
|
|
|
UnitOfVolumeFlowRate.LITERS_PER_MINUTE,
|
|
|
|
UnitOfVolumeFlowRate.GALLONS_PER_MINUTE,
|
|
|
|
UnitOfVolumeFlowRate.GALLONS_PER_MINUTE,
|
|
|
|
50.0,
|
|
|
|
"13.2",
|
|
|
|
),
|
|
|
|
(
|
2024-02-04 14:01:06 +00:00
|
|
|
NumberDeviceClass.VOLUME_FLOW_RATE,
|
2024-01-30 14:01:08 +00:00
|
|
|
UnitOfVolumeFlowRate.GALLONS_PER_MINUTE,
|
|
|
|
UnitOfVolumeFlowRate.LITERS_PER_MINUTE,
|
|
|
|
UnitOfVolumeFlowRate.LITERS_PER_MINUTE,
|
|
|
|
13.0,
|
|
|
|
"49.2",
|
|
|
|
),
|
2022-07-11 12:49:36 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_custom_unit(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
2024-05-28 11:40:45 +00:00
|
|
|
entity_registry: er.EntityRegistry,
|
2022-07-11 12:49:36 +00:00
|
|
|
device_class,
|
|
|
|
native_unit,
|
|
|
|
custom_unit,
|
|
|
|
state_unit,
|
|
|
|
native_value,
|
|
|
|
custom_value,
|
2023-02-15 09:50:02 +00:00
|
|
|
) -> None:
|
2022-07-11 12:49:36 +00:00
|
|
|
"""Test custom unit."""
|
|
|
|
entry = entity_registry.async_get_or_create("number", "test", "very_unique")
|
|
|
|
entity_registry.async_update_entity_options(
|
|
|
|
entry.entity_id, "number", {"unit_of_measurement": custom_unit}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-03-26 20:56:38 +00:00
|
|
|
entity0 = common.MockNumberEntity(
|
|
|
|
name="Test",
|
|
|
|
native_value=native_value,
|
|
|
|
native_unit_of_measurement=native_unit,
|
|
|
|
device_class=device_class,
|
|
|
|
unique_id="very_unique",
|
2022-07-11 12:49:36 +00:00
|
|
|
)
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
2022-07-11 12:49:36 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(hass, "number", {"number": {"platform": "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(custom_value))
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == state_unit
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
(
|
|
|
|
"native_unit",
|
|
|
|
"custom_unit",
|
|
|
|
"used_custom_unit",
|
|
|
|
"default_unit",
|
|
|
|
"native_value",
|
|
|
|
"custom_value",
|
|
|
|
"default_value",
|
|
|
|
),
|
2022-07-11 12:49:36 +00:00
|
|
|
[
|
|
|
|
(
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-07-11 12:49:36 +00:00
|
|
|
37.5,
|
|
|
|
99.5,
|
|
|
|
37.5,
|
|
|
|
),
|
|
|
|
(
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.FAHRENHEIT,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
2022-07-11 12:49:36 +00:00
|
|
|
100,
|
|
|
|
100,
|
|
|
|
38.0,
|
|
|
|
),
|
|
|
|
# Not a supported temperature unit
|
2023-01-15 14:45:00 +00:00
|
|
|
(
|
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
"no_unit",
|
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
UnitOfTemperature.CELSIUS,
|
|
|
|
1000,
|
|
|
|
1000,
|
|
|
|
1000,
|
|
|
|
),
|
2022-07-11 12:49:36 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_custom_unit_change(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
2024-05-28 11:40:45 +00:00
|
|
|
entity_registry: er.EntityRegistry,
|
2022-07-11 12:49:36 +00:00
|
|
|
native_unit,
|
|
|
|
custom_unit,
|
|
|
|
used_custom_unit,
|
|
|
|
default_unit,
|
|
|
|
native_value,
|
|
|
|
custom_value,
|
|
|
|
default_value,
|
2023-02-15 09:50:02 +00:00
|
|
|
) -> None:
|
2022-07-11 12:49:36 +00:00
|
|
|
"""Test custom unit changes are picked up."""
|
2024-03-26 20:56:38 +00:00
|
|
|
entity0 = common.MockNumberEntity(
|
|
|
|
name="Test",
|
|
|
|
native_value=native_value,
|
|
|
|
native_unit_of_measurement=native_unit,
|
|
|
|
device_class=NumberDeviceClass.TEMPERATURE,
|
|
|
|
unique_id="very_unique",
|
2022-07-11 12:49:36 +00:00
|
|
|
)
|
2024-03-26 20:56:38 +00:00
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity0])
|
2022-07-11 12:49:36 +00:00
|
|
|
|
|
|
|
assert await async_setup_component(hass, "number", {"number": {"platform": "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Default unit conversion according to unit system
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(default_value))
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == default_unit
|
|
|
|
|
|
|
|
entity_registry.async_update_entity_options(
|
|
|
|
"number.test", "number", {"unit_of_measurement": custom_unit}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Unit conversion to the custom unit
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(custom_value))
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == used_custom_unit
|
|
|
|
|
|
|
|
entity_registry.async_update_entity_options(
|
|
|
|
"number.test", "number", {"unit_of_measurement": native_unit}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Unit conversion to another custom unit
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(native_value))
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit
|
|
|
|
|
|
|
|
entity_registry.async_update_entity_options("number.test", "number", None)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Default unit conversion according to unit system
|
|
|
|
state = hass.states.get(entity0.entity_id)
|
|
|
|
assert float(state.state) == pytest.approx(float(default_value))
|
|
|
|
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == default_unit
|
2022-11-17 13:00:28 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_device_classes_aligned() -> None:
|
2022-11-17 13:00:28 +00:00
|
|
|
"""Make sure all sensor device classes are also available in NumberDeviceClass."""
|
|
|
|
|
|
|
|
for device_class in SensorDeviceClass:
|
2023-07-20 09:10:03 +00:00
|
|
|
if device_class in NON_NUMERIC_DEVICE_CLASSES:
|
2022-11-17 13:00:28 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
assert hasattr(NumberDeviceClass, device_class.name)
|
|
|
|
assert getattr(NumberDeviceClass, device_class.name).value == device_class.value
|
2023-01-12 08:34:10 +00:00
|
|
|
|
2024-06-12 14:44:29 +00:00
|
|
|
for device_class, unit in SENSOR_DEVICE_CLASS_UNITS.items():
|
2023-07-20 09:10:03 +00:00
|
|
|
if device_class in NON_NUMERIC_DEVICE_CLASSES:
|
2023-01-12 08:34:10 +00:00
|
|
|
continue
|
2024-06-12 14:44:29 +00:00
|
|
|
assert unit == NUMBER_DEVICE_CLASS_UNITS[device_class]
|
2023-06-22 21:48:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockFlow(ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-06 15:28:59 +00:00
|
|
|
def config_flow_fixture(hass: HomeAssistant) -> Generator[None]:
|
2023-06-22 21:48:49 +00:00
|
|
|
"""Mock config flow."""
|
|
|
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
|
|
|
|
with mock_config_flow(TEST_DOMAIN, MockFlow):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
async def test_name(hass: HomeAssistant) -> None:
|
|
|
|
"""Test number name."""
|
|
|
|
|
|
|
|
async def async_setup_entry_init(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Set up test config entry."""
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-05 01:34:39 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN])
|
2023-06-22 21:48:49 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
TEST_DOMAIN,
|
|
|
|
async_setup_entry=async_setup_entry_init,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-08-18 09:02:30 +00:00
|
|
|
# Unnamed number without device class -> no name
|
2023-06-22 21:48:49 +00:00
|
|
|
entity1 = NumberEntity()
|
|
|
|
entity1.entity_id = "number.test1"
|
|
|
|
|
2023-08-18 09:02:30 +00:00
|
|
|
# Unnamed number with device class but has_entity_name False -> no name
|
2023-06-22 21:48:49 +00:00
|
|
|
entity2 = NumberEntity()
|
|
|
|
entity2.entity_id = "number.test2"
|
|
|
|
entity2._attr_device_class = NumberDeviceClass.TEMPERATURE
|
|
|
|
|
2023-08-18 09:02:30 +00:00
|
|
|
# Unnamed number with device class and has_entity_name True -> named
|
2023-06-22 21:48:49 +00:00
|
|
|
entity3 = NumberEntity()
|
|
|
|
entity3.entity_id = "number.test3"
|
|
|
|
entity3._attr_device_class = NumberDeviceClass.TEMPERATURE
|
|
|
|
entity3._attr_has_entity_name = True
|
|
|
|
|
2023-08-18 09:02:30 +00:00
|
|
|
# Unnamed number with device class and has_entity_name True -> named
|
2023-06-22 21:48:49 +00:00
|
|
|
entity4 = NumberEntity()
|
|
|
|
entity4.entity_id = "number.test4"
|
|
|
|
entity4.entity_description = NumberEntityDescription(
|
|
|
|
"test",
|
|
|
|
NumberDeviceClass.TEMPERATURE,
|
|
|
|
has_entity_name=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_setup_entry_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up test number platform via config entry."""
|
|
|
|
async_add_entities([entity1, entity2, entity3, entity4])
|
|
|
|
|
|
|
|
mock_platform(
|
|
|
|
hass,
|
|
|
|
f"{TEST_DOMAIN}.{DOMAIN}",
|
|
|
|
MockPlatform(async_setup_entry=async_setup_entry_platform),
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain=TEST_DOMAIN)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity1.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {
|
|
|
|
"max": 100.0,
|
|
|
|
"min": 0.0,
|
|
|
|
"mode": NumberMode.AUTO,
|
|
|
|
"step": 1.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = hass.states.get(entity2.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {
|
|
|
|
"device_class": "temperature",
|
|
|
|
"max": 100.0,
|
|
|
|
"min": 0.0,
|
|
|
|
"mode": NumberMode.AUTO,
|
|
|
|
"step": 1.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = hass.states.get(entity3.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {
|
|
|
|
"device_class": "temperature",
|
|
|
|
"friendly_name": "Temperature",
|
|
|
|
"max": 100.0,
|
|
|
|
"min": 0.0,
|
|
|
|
"mode": NumberMode.AUTO,
|
|
|
|
"step": 1.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = hass.states.get(entity4.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {
|
|
|
|
"device_class": "temperature",
|
|
|
|
"friendly_name": "Temperature",
|
|
|
|
"max": 100.0,
|
|
|
|
"min": 0.0,
|
|
|
|
"mode": NumberMode.AUTO,
|
|
|
|
"step": 1.0,
|
|
|
|
}
|
2023-10-02 11:01:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_device_class_units(hass: HomeAssistant) -> None:
|
|
|
|
"""Test all numeric device classes have unit."""
|
|
|
|
# DEVICE_CLASS_UNITS should include all device classes except:
|
|
|
|
# - NumberDeviceClass.MONETARY
|
|
|
|
# - Device classes enumerated in NON_NUMERIC_DEVICE_CLASSES
|
|
|
|
assert set(NUMBER_DEVICE_CLASS_UNITS) == set(
|
|
|
|
NumberDeviceClass
|
|
|
|
) - NON_NUMERIC_DEVICE_CLASSES - {NumberDeviceClass.MONETARY}
|