Don't inherit SensorEntity/NumberEntity and RestoreEntity in Shelly integration (#93531)

* Use RestoreNumber and Restore Sensor for block entities

* Update tests

* Use RestoreSensor for RPC entities

* Fix test for number platform
This commit is contained in:
Maciej Bieniek 2023-06-05 00:14:08 +00:00 committed by GitHub
parent 7c02e1ca99
commit fe61672792
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 39 deletions

View file

@ -1,15 +1,18 @@
"""Number for Shelly."""
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any, Final, cast
from aioshelly.block_device import Block
from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberExtraStoredData,
NumberMode,
RestoreNumber,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, EntityCategory
@ -19,6 +22,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity_registry import RegistryEntry
from .const import CONF_SLEEP_PERIOD, LOGGER
from .coordinator import ShellyBlockCoordinator
from .entity import (
BlockEntityDescription,
ShellySleepingBlockAttributeEntity,
@ -85,22 +89,39 @@ async def async_setup_entry(
)
# pylint: disable-next=hass-invalid-inheritance # needs fixing
class BlockSleepingNumber(ShellySleepingBlockAttributeEntity, NumberEntity):
class BlockSleepingNumber(ShellySleepingBlockAttributeEntity, RestoreNumber):
"""Represent a block sleeping number."""
entity_description: BlockNumberDescription
def __init__(
self,
coordinator: ShellyBlockCoordinator,
block: Block | None,
attribute: str,
description: BlockNumberDescription,
entry: RegistryEntry | None = None,
sensors: Mapping[tuple[str, str], BlockNumberDescription] | None = None,
) -> None:
"""Initialize the sleeping sensor."""
self.restored_data: NumberExtraStoredData | None = None
super().__init__(coordinator, block, attribute, description, entry, sensors)
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()
self.restored_data = await self.async_get_last_number_data()
@property
def native_value(self) -> float | None:
"""Return value of number."""
if self.block is not None:
return cast(float, self.attribute_value)
if self.last_state is None:
if self.restored_data is None:
return None
return cast(float, self.last_state.state)
return cast(float, self.restored_data.native_value)
async def async_set_native_value(self, value: float) -> None:
"""Set value."""