mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Migrate knx NumberEntity to native_value (#73536)
This commit is contained in:
parent
8007effd4f
commit
8c0ae545c9
@ -7,7 +7,7 @@ from xknx import XKNX
|
|||||||
from xknx.devices import NumericValue
|
from xknx.devices import NumericValue
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.number import NumberEntity
|
from homeassistant.components.number import RestoreNumber
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_ENTITY_CATEGORY,
|
CONF_ENTITY_CATEGORY,
|
||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
@ -19,7 +19,6 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -57,7 +56,7 @@ def _create_numeric_value(xknx: XKNX, config: ConfigType) -> NumericValue:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class KNXNumber(KnxEntity, NumberEntity, RestoreEntity):
|
class KNXNumber(KnxEntity, RestoreNumber):
|
||||||
"""Representation of a KNX number."""
|
"""Representation of a KNX number."""
|
||||||
|
|
||||||
_device: NumericValue
|
_device: NumericValue
|
||||||
@ -65,39 +64,41 @@ class KNXNumber(KnxEntity, NumberEntity, RestoreEntity):
|
|||||||
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
||||||
"""Initialize a KNX number."""
|
"""Initialize a KNX number."""
|
||||||
super().__init__(_create_numeric_value(xknx, config))
|
super().__init__(_create_numeric_value(xknx, config))
|
||||||
self._attr_max_value = config.get(
|
self._attr_native_max_value = config.get(
|
||||||
NumberSchema.CONF_MAX,
|
NumberSchema.CONF_MAX,
|
||||||
self._device.sensor_value.dpt_class.value_max,
|
self._device.sensor_value.dpt_class.value_max,
|
||||||
)
|
)
|
||||||
self._attr_min_value = config.get(
|
self._attr_native_min_value = config.get(
|
||||||
NumberSchema.CONF_MIN,
|
NumberSchema.CONF_MIN,
|
||||||
self._device.sensor_value.dpt_class.value_min,
|
self._device.sensor_value.dpt_class.value_min,
|
||||||
)
|
)
|
||||||
self._attr_mode = config[CONF_MODE]
|
self._attr_mode = config[CONF_MODE]
|
||||||
self._attr_step = config.get(
|
self._attr_native_step = config.get(
|
||||||
NumberSchema.CONF_STEP,
|
NumberSchema.CONF_STEP,
|
||||||
self._device.sensor_value.dpt_class.resolution,
|
self._device.sensor_value.dpt_class.resolution,
|
||||||
)
|
)
|
||||||
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
||||||
self._attr_unique_id = str(self._device.sensor_value.group_address)
|
self._attr_unique_id = str(self._device.sensor_value.group_address)
|
||||||
self._attr_unit_of_measurement = self._device.unit_of_measurement()
|
self._attr_native_unit_of_measurement = self._device.unit_of_measurement()
|
||||||
self._device.sensor_value.value = max(0, self._attr_min_value)
|
self._device.sensor_value.value = max(0, self._attr_native_min_value)
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Restore last state."""
|
"""Restore last state."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
if not self._device.sensor_value.readable and (
|
if (
|
||||||
last_state := await self.async_get_last_state()
|
not self._device.sensor_value.readable
|
||||||
|
and (last_state := await self.async_get_last_state())
|
||||||
|
and (last_number_data := await self.async_get_last_number_data())
|
||||||
):
|
):
|
||||||
if last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
if last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
||||||
self._device.sensor_value.value = float(last_state.state)
|
self._device.sensor_value.value = last_number_data.native_value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self) -> float:
|
def native_value(self) -> float:
|
||||||
"""Return the entity value to represent the entity state."""
|
"""Return the entity value to represent the entity state."""
|
||||||
# self._device.sensor_value.value is set in __init__ so it is never None
|
# self._device.sensor_value.value is set in __init__ so it is never None
|
||||||
return cast(float, self._device.resolve_state())
|
return cast(float, self._device.resolve_state())
|
||||||
|
|
||||||
async def async_set_value(self, value: float) -> None:
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
"""Set new value."""
|
"""Set new value."""
|
||||||
await self._device.set(value)
|
await self._device.set(value)
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
"""Test KNX number."""
|
"""Test KNX number."""
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.knx.const import CONF_RESPOND_TO_READ, KNX_ADDRESS
|
from homeassistant.components.knx.const import CONF_RESPOND_TO_READ, KNX_ADDRESS
|
||||||
@ -10,6 +8,8 @@ from homeassistant.core import HomeAssistant, State
|
|||||||
|
|
||||||
from .conftest import KNXTestKit
|
from .conftest import KNXTestKit
|
||||||
|
|
||||||
|
from tests.common import mock_restore_cache_with_extra_data
|
||||||
|
|
||||||
|
|
||||||
async def test_number_set_value(hass: HomeAssistant, knx: KNXTestKit):
|
async def test_number_set_value(hass: HomeAssistant, knx: KNXTestKit):
|
||||||
"""Test KNX number with passive_address and respond_to_read restoring state."""
|
"""Test KNX number with passive_address and respond_to_read restoring state."""
|
||||||
@ -64,12 +64,18 @@ async def test_number_restore_and_respond(hass: HomeAssistant, knx: KNXTestKit):
|
|||||||
"""Test KNX number with passive_address and respond_to_read restoring state."""
|
"""Test KNX number with passive_address and respond_to_read restoring state."""
|
||||||
test_address = "1/1/1"
|
test_address = "1/1/1"
|
||||||
test_passive_address = "3/3/3"
|
test_passive_address = "3/3/3"
|
||||||
fake_state = State("number.test", "160")
|
|
||||||
|
|
||||||
with patch(
|
RESTORE_DATA = {
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
"native_max_value": None, # Ignored by KNX number
|
||||||
return_value=fake_state,
|
"native_min_value": None, # Ignored by KNX number
|
||||||
):
|
"native_step": None, # Ignored by KNX number
|
||||||
|
"native_unit_of_measurement": None, # Ignored by KNX number
|
||||||
|
"native_value": 160.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
mock_restore_cache_with_extra_data(
|
||||||
|
hass, ((State("number.test", "abc"), RESTORE_DATA),)
|
||||||
|
)
|
||||||
await knx.setup_integration(
|
await knx.setup_integration(
|
||||||
{
|
{
|
||||||
NumberSchema.PLATFORM: {
|
NumberSchema.PLATFORM: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user