Correct sensor restore entity in modbus (#87563)

* Correct sensor restore entity.

* Review comments.
This commit is contained in:
jan iversen 2023-02-09 21:35:44 +01:00 committed by Paulus Schoutsen
parent 0a69f825d2
commit f2697b3e96
2 changed files with 11 additions and 7 deletions

View File

@ -5,7 +5,11 @@ from datetime import datetime
import logging import logging
from typing import Any from typing import Any
from homeassistant.components.sensor import CONF_STATE_CLASS, SensorEntity from homeassistant.components.sensor import (
CONF_STATE_CLASS,
RestoreSensor,
SensorEntity,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_SENSORS, CONF_SENSORS,
@ -14,7 +18,6 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
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, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
@ -53,7 +56,7 @@ async def async_setup_platform(
async_add_entities(sensors) async_add_entities(sensors)
class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity): class ModbusRegisterSensor(BaseStructPlatform, RestoreSensor, SensorEntity):
"""Modbus register sensor.""" """Modbus register sensor."""
def __init__( def __init__(
@ -90,8 +93,9 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Handle entity which will be added.""" """Handle entity which will be added."""
await self.async_base_added_to_hass() await self.async_base_added_to_hass()
if state := await self.async_get_last_state(): state = await self.async_get_last_sensor_data()
self._attr_native_value = state.state if state:
self._attr_native_value = state.native_value
async def async_update(self, now: datetime | None = None) -> None: async def async_update(self, now: datetime | None = None) -> None:
"""Update the state of the sensor.""" """Update the state of the sensor."""
@ -135,7 +139,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
class SlaveSensor( class SlaveSensor(
CoordinatorEntity[DataUpdateCoordinator[list[int] | None]], CoordinatorEntity[DataUpdateCoordinator[list[int] | None]],
RestoreEntity, RestoreSensor,
SensorEntity, SensorEntity,
): ):
"""Modbus slave register sensor.""" """Modbus slave register sensor."""

View File

@ -855,7 +855,7 @@ async def test_wrap_sensor(hass, mock_do_cycle, expected):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"mock_test_state", "mock_test_state",
[(State(ENTITY_ID, "117"), State(f"{ENTITY_ID}_1", "119"))], [(State(ENTITY_ID, "unknown"), State(f"{ENTITY_ID}_1", "119"))],
indirect=True, indirect=True,
) )
@pytest.mark.parametrize( @pytest.mark.parametrize(