2020-09-07 12:13:20 +00:00
|
|
|
"""Shelly entity helper."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 16:10:28 +00:00
|
|
|
import asyncio
|
2021-09-29 14:19:06 +00:00
|
|
|
from collections.abc import Callable
|
2020-09-07 12:13:20 +00:00
|
|
|
from dataclasses import dataclass
|
2021-02-03 16:03:22 +00:00
|
|
|
import logging
|
2021-09-29 14:19:06 +00:00
|
|
|
from typing import Any, Final, cast
|
2020-09-07 12:13:20 +00:00
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
from aioshelly.block_device import Block
|
2021-05-04 16:10:28 +00:00
|
|
|
import async_timeout
|
2020-09-07 12:13:20 +00:00
|
|
|
|
2021-05-23 20:10:22 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-02-03 16:03:22 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
device_registry,
|
|
|
|
entity,
|
|
|
|
entity_registry,
|
|
|
|
update_coordinator,
|
|
|
|
)
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-02-03 16:03:22 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2021-09-13 06:31:35 +00:00
|
|
|
from . import BlockDeviceWrapper, RpcDeviceWrapper, ShellyDeviceRestWrapper
|
2021-09-17 12:53:39 +00:00
|
|
|
from .const import (
|
|
|
|
AIOSHELLY_DEVICE_TIMEOUT_SEC,
|
|
|
|
BLOCK,
|
|
|
|
DATA_CONFIG_ENTRY,
|
|
|
|
DOMAIN,
|
|
|
|
REST,
|
|
|
|
RPC,
|
|
|
|
)
|
2021-09-13 06:31:35 +00:00
|
|
|
from .utils import (
|
|
|
|
async_remove_shelly_entity,
|
|
|
|
get_block_entity_name,
|
|
|
|
get_rpc_entity_name,
|
2021-09-17 12:53:39 +00:00
|
|
|
get_rpc_key_instances,
|
2021-09-13 06:31:35 +00:00
|
|
|
)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
_LOGGER: Final = logging.getLogger(__name__)
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
async def async_setup_entry_attribute_entities(
|
2021-07-21 17:11:44 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
sensors: dict[tuple[str, str], BlockAttributeDescription],
|
|
|
|
sensor_class: Callable,
|
|
|
|
) -> None:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Set up entities for attributes."""
|
2021-09-13 06:31:35 +00:00
|
|
|
wrapper: BlockDeviceWrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
|
2020-10-21 11:37:17 +00:00
|
|
|
config_entry.entry_id
|
2021-09-13 06:31:35 +00:00
|
|
|
][BLOCK]
|
2021-02-03 16:03:22 +00:00
|
|
|
|
|
|
|
if wrapper.device.initialized:
|
|
|
|
await async_setup_block_attribute_entities(
|
|
|
|
hass, async_add_entities, wrapper, sensors, sensor_class
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await async_restore_block_attribute_entities(
|
2021-05-03 06:49:13 +00:00
|
|
|
hass, config_entry, async_add_entities, wrapper, sensors, sensor_class
|
2021-02-03 16:03:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_block_attribute_entities(
|
2021-07-21 17:11:44 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-09-13 06:31:35 +00:00
|
|
|
wrapper: BlockDeviceWrapper,
|
2021-07-21 17:11:44 +00:00
|
|
|
sensors: dict[tuple[str, str], BlockAttributeDescription],
|
|
|
|
sensor_class: Callable,
|
|
|
|
) -> None:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Set up entities for block attributes."""
|
2020-09-07 12:13:20 +00:00
|
|
|
blocks = []
|
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
assert wrapper.device.blocks
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
for block in wrapper.device.blocks:
|
|
|
|
for sensor_id in block.sensor_ids:
|
|
|
|
description = sensors.get((block.type, sensor_id))
|
|
|
|
if description is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Filter out non-existing sensors and sensors without a value
|
|
|
|
if getattr(block, sensor_id, None) in (-1, None):
|
|
|
|
continue
|
|
|
|
|
2020-11-19 10:42:24 +00:00
|
|
|
# Filter and remove entities that according to settings should not create an entity
|
|
|
|
if description.removal_condition and description.removal_condition(
|
|
|
|
wrapper.device.settings, block
|
|
|
|
):
|
|
|
|
domain = sensor_class.__module__.split(".")[-1]
|
2021-02-03 16:03:22 +00:00
|
|
|
unique_id = f"{wrapper.mac}-{block.description}-{sensor_id}"
|
2020-11-19 10:42:24 +00:00
|
|
|
await async_remove_shelly_entity(hass, domain, unique_id)
|
|
|
|
else:
|
|
|
|
blocks.append((block, sensor_id, description))
|
2020-09-07 12:13:20 +00:00
|
|
|
|
|
|
|
if not blocks:
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
2020-11-11 08:05:08 +00:00
|
|
|
sensor_class(wrapper, block, sensor_id, description)
|
2020-09-07 12:13:20 +00:00
|
|
|
for block, sensor_id, description in blocks
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
async def async_restore_block_attribute_entities(
|
2021-07-21 17:11:44 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-09-13 06:31:35 +00:00
|
|
|
wrapper: BlockDeviceWrapper,
|
2021-07-21 17:11:44 +00:00
|
|
|
sensors: dict[tuple[str, str], BlockAttributeDescription],
|
|
|
|
sensor_class: Callable,
|
|
|
|
) -> None:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Restore block attributes entities."""
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
ent_reg = await entity_registry.async_get_registry(hass)
|
|
|
|
entries = entity_registry.async_entries_for_config_entry(
|
|
|
|
ent_reg, config_entry.entry_id
|
|
|
|
)
|
|
|
|
|
|
|
|
domain = sensor_class.__module__.split(".")[-1]
|
|
|
|
|
|
|
|
for entry in entries:
|
|
|
|
if entry.domain != domain:
|
|
|
|
continue
|
|
|
|
|
|
|
|
attribute = entry.unique_id.split("-")[-1]
|
|
|
|
description = BlockAttributeDescription(
|
|
|
|
name="",
|
|
|
|
icon=entry.original_icon,
|
|
|
|
unit=entry.unit_of_measurement,
|
|
|
|
device_class=entry.device_class,
|
|
|
|
)
|
|
|
|
|
2021-05-03 06:49:13 +00:00
|
|
|
entities.append(
|
|
|
|
sensor_class(wrapper, None, attribute, description, entry, sensors)
|
|
|
|
)
|
2021-02-03 16:03:22 +00:00
|
|
|
|
|
|
|
if not entities:
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
async def async_setup_entry_rpc(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
sensors: dict[str, RpcAttributeDescription],
|
|
|
|
sensor_class: Callable,
|
|
|
|
) -> None:
|
|
|
|
"""Set up entities for REST sensors."""
|
|
|
|
wrapper: RpcDeviceWrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
|
|
|
|
config_entry.entry_id
|
|
|
|
][RPC]
|
|
|
|
|
|
|
|
entities = []
|
|
|
|
for sensor_id in sensors:
|
|
|
|
description = sensors[sensor_id]
|
|
|
|
key_instances = get_rpc_key_instances(wrapper.device.status, description.key)
|
|
|
|
|
|
|
|
for key in key_instances:
|
2021-10-04 20:46:46 +00:00
|
|
|
# Filter non-existing sensors
|
|
|
|
if description.sub_key not in wrapper.device.status[key]:
|
|
|
|
continue
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
# Filter and remove entities that according to settings should not create an entity
|
|
|
|
if description.removal_condition and description.removal_condition(
|
|
|
|
wrapper.device.config, key
|
|
|
|
):
|
|
|
|
domain = sensor_class.__module__.split(".")[-1]
|
|
|
|
unique_id = f"{wrapper.mac}-{key}-{sensor_id}"
|
|
|
|
await async_remove_shelly_entity(hass, domain, unique_id)
|
|
|
|
else:
|
|
|
|
entities.append((key, sensor_id, description))
|
|
|
|
|
|
|
|
if not entities:
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
sensor_class(wrapper, key, sensor_id, description)
|
|
|
|
for key, sensor_id, description in entities
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-11 19:13:14 +00:00
|
|
|
async def async_setup_entry_rest(
|
2021-07-21 17:11:44 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
sensors: dict[str, RestAttributeDescription],
|
|
|
|
sensor_class: Callable,
|
|
|
|
) -> None:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Set up entities for REST sensors."""
|
|
|
|
wrapper: ShellyDeviceRestWrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
|
|
|
|
config_entry.entry_id
|
|
|
|
][REST]
|
|
|
|
|
|
|
|
entities = []
|
|
|
|
for sensor_id in sensors:
|
2020-11-27 08:40:06 +00:00
|
|
|
description = sensors.get(sensor_id)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
if not wrapper.device.settings.get("sleep_mode"):
|
2020-11-27 08:40:06 +00:00
|
|
|
entities.append((sensor_id, description))
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
if not entities:
|
|
|
|
return
|
|
|
|
|
2020-11-27 08:40:06 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
sensor_class(wrapper, sensor_id, description)
|
|
|
|
for sensor_id, description in entities
|
|
|
|
]
|
|
|
|
)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
@dataclass
|
|
|
|
class BlockAttributeDescription:
|
|
|
|
"""Class to describe a sensor."""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
# Callable = lambda attr_info: unit
|
2021-03-18 13:31:38 +00:00
|
|
|
icon: str | None = None
|
|
|
|
unit: None | str | Callable[[dict], str] = None
|
2020-09-07 12:13:20 +00:00
|
|
|
value: Callable[[Any], Any] = lambda val: val
|
2021-03-18 13:31:38 +00:00
|
|
|
device_class: str | None = None
|
2021-05-21 09:44:34 +00:00
|
|
|
state_class: str | None = None
|
2020-09-07 12:13:20 +00:00
|
|
|
default_enabled: bool = True
|
2021-09-10 21:48:55 +00:00
|
|
|
available: Callable[[Block], bool] | None = None
|
2020-11-19 10:42:24 +00:00
|
|
|
# Callable (settings, block), return true if entity should be removed
|
2021-09-10 21:48:55 +00:00
|
|
|
removal_condition: Callable[[dict, Block], bool] | None = None
|
|
|
|
extra_state_attributes: Callable[[Block], dict | None] | None = None
|
2021-10-14 22:17:00 +00:00
|
|
|
entity_category: str | None = None
|
2020-09-07 12:13:20 +00:00
|
|
|
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
@dataclass
|
|
|
|
class RpcAttributeDescription:
|
|
|
|
"""Class to describe a RPC sensor."""
|
|
|
|
|
|
|
|
key: str
|
2021-10-04 20:46:46 +00:00
|
|
|
sub_key: str
|
2021-09-17 12:53:39 +00:00
|
|
|
name: str
|
|
|
|
icon: str | None = None
|
|
|
|
unit: str | None = None
|
2021-10-04 20:46:46 +00:00
|
|
|
value: Callable[[Any, Any], Any] | None = None
|
2021-09-17 12:53:39 +00:00
|
|
|
device_class: str | None = None
|
|
|
|
state_class: str | None = None
|
|
|
|
default_enabled: bool = True
|
|
|
|
available: Callable[[dict], bool] | None = None
|
|
|
|
removal_condition: Callable[[dict, str], bool] | None = None
|
2021-10-15 02:02:37 +00:00
|
|
|
extra_state_attributes: Callable[[dict, dict], dict | None] | None = None
|
2021-10-14 22:17:00 +00:00
|
|
|
entity_category: str | None = None
|
2021-09-17 12:53:39 +00:00
|
|
|
|
|
|
|
|
2020-11-11 19:13:14 +00:00
|
|
|
@dataclass
|
|
|
|
class RestAttributeDescription:
|
|
|
|
"""Class to describe a REST sensor."""
|
|
|
|
|
|
|
|
name: str
|
2021-03-18 13:31:38 +00:00
|
|
|
icon: str | None = None
|
|
|
|
unit: str | None = None
|
2021-05-03 06:49:13 +00:00
|
|
|
value: Callable[[dict, Any], Any] | None = None
|
2021-03-18 13:31:38 +00:00
|
|
|
device_class: str | None = None
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class: str | None = None
|
2020-11-11 19:13:14 +00:00
|
|
|
default_enabled: bool = True
|
2021-03-18 13:31:38 +00:00
|
|
|
extra_state_attributes: Callable[[dict], dict | None] | None = None
|
2021-10-14 22:17:00 +00:00
|
|
|
entity_category: str | None = None
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
class ShellyBlockEntity(entity.Entity):
|
2021-09-13 06:31:35 +00:00
|
|
|
"""Helper class to represent a block entity."""
|
2020-09-07 12:13:20 +00:00
|
|
|
|
2021-09-13 06:31:35 +00:00
|
|
|
def __init__(self, wrapper: BlockDeviceWrapper, block: Block) -> None:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Initialize Shelly entity."""
|
|
|
|
self.wrapper = wrapper
|
|
|
|
self.block = block
|
2021-09-13 06:31:35 +00:00
|
|
|
self._name = get_block_entity_name(wrapper.device, block)
|
2020-09-07 12:13:20 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def name(self) -> str:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Name of entity."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def should_poll(self) -> bool:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""If device should be polled."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Device info."""
|
|
|
|
return {
|
|
|
|
"connections": {(device_registry.CONNECTION_NETWORK_MAC, self.wrapper.mac)}
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def available(self) -> bool:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Available."""
|
|
|
|
return self.wrapper.last_update_success
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Return unique ID of entity."""
|
|
|
|
return f"{self.wrapper.mac}-{self.block.description}"
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""When entity is added to HASS."""
|
|
|
|
self.async_on_remove(self.wrapper.async_add_listener(self._update_callback))
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_update(self) -> None:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Update entity with latest info."""
|
|
|
|
await self.wrapper.async_request_refresh()
|
|
|
|
|
|
|
|
@callback
|
2021-07-21 17:11:44 +00:00
|
|
|
def _update_callback(self) -> None:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Handle device update."""
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def set_state(self, **kwargs: Any) -> Any:
|
2021-05-04 16:10:28 +00:00
|
|
|
"""Set block state (HTTP request)."""
|
|
|
|
_LOGGER.debug("Setting state for entity %s, state: %s", self.name, kwargs)
|
|
|
|
try:
|
|
|
|
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
|
|
|
|
return await self.block.set_state(**kwargs)
|
|
|
|
except (asyncio.TimeoutError, OSError) as err:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Setting state for entity %s failed, state: %s, error: %s",
|
|
|
|
self.name,
|
|
|
|
kwargs,
|
|
|
|
repr(err),
|
|
|
|
)
|
|
|
|
self.wrapper.last_update_success = False
|
|
|
|
return None
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
|
2021-09-13 06:31:35 +00:00
|
|
|
class ShellyRpcEntity(entity.Entity):
|
|
|
|
"""Helper class to represent a rpc entity."""
|
|
|
|
|
|
|
|
def __init__(self, wrapper: RpcDeviceWrapper, key: str) -> None:
|
|
|
|
"""Initialize Shelly entity."""
|
|
|
|
self.wrapper = wrapper
|
|
|
|
self.key = key
|
|
|
|
self._attr_should_poll = False
|
|
|
|
self._attr_device_info = {
|
|
|
|
"connections": {(device_registry.CONNECTION_NETWORK_MAC, wrapper.mac)}
|
|
|
|
}
|
|
|
|
self._attr_unique_id = f"{wrapper.mac}-{key}"
|
|
|
|
self._attr_name = get_rpc_entity_name(wrapper.device, key)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Available."""
|
|
|
|
return self.wrapper.device.connected
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to HASS."""
|
|
|
|
self.async_on_remove(self.wrapper.async_add_listener(self._update_callback))
|
|
|
|
|
|
|
|
async def async_update(self) -> None:
|
|
|
|
"""Update entity with latest info."""
|
|
|
|
await self.wrapper.async_request_refresh()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self) -> None:
|
|
|
|
"""Handle device update."""
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def call_rpc(self, method: str, params: Any) -> Any:
|
|
|
|
"""Call RPC method."""
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Call RPC for entity %s, method: %s, params: %s",
|
|
|
|
self.name,
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
|
|
|
|
return await self.wrapper.device.call_rpc(method, params)
|
|
|
|
except asyncio.TimeoutError as err:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Call RPC for entity %s failed, method: %s, params: %s, error: %s",
|
|
|
|
self.name,
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
repr(err),
|
|
|
|
)
|
|
|
|
self.wrapper.last_update_success = False
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity):
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Helper class to represent a block attribute."""
|
2020-09-07 12:13:20 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-09-13 06:31:35 +00:00
|
|
|
wrapper: BlockDeviceWrapper,
|
2021-09-10 21:48:55 +00:00
|
|
|
block: Block,
|
2020-09-07 12:13:20 +00:00
|
|
|
attribute: str,
|
|
|
|
description: BlockAttributeDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize sensor."""
|
|
|
|
super().__init__(wrapper, block)
|
|
|
|
self.attribute = attribute
|
|
|
|
self.description = description
|
|
|
|
|
|
|
|
unit = self.description.unit
|
|
|
|
|
|
|
|
if callable(unit):
|
2021-02-03 16:03:22 +00:00
|
|
|
unit = unit(block.info(attribute))
|
2020-09-07 12:13:20 +00:00
|
|
|
|
2021-05-03 06:49:13 +00:00
|
|
|
self._unit: None | str | Callable[[dict], str] = unit
|
2021-07-21 17:11:44 +00:00
|
|
|
self._unique_id: str = f"{super().unique_id}-{self.attribute}"
|
2021-09-13 06:31:35 +00:00
|
|
|
self._name = get_block_entity_name(wrapper.device, block, self.description.name)
|
2020-09-07 12:13:20 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Return unique ID of entity."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def name(self) -> str:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Name of sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self) -> bool:
|
|
|
|
"""Return if it should be enabled by default."""
|
|
|
|
return self.description.default_enabled
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def attribute_value(self) -> StateType:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Value of sensor."""
|
|
|
|
value = getattr(self.block, self.attribute)
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
return cast(StateType, self.description.value(value))
|
2020-09-07 12:13:20 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def device_class(self) -> str | None:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Device class of sensor."""
|
|
|
|
return self.description.device_class
|
|
|
|
|
2020-11-12 09:38:53 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def icon(self) -> str | None:
|
2020-11-12 09:38:53 +00:00
|
|
|
"""Icon of sensor."""
|
|
|
|
return self.description.icon
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def available(self) -> bool:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Available."""
|
|
|
|
available = super().available
|
|
|
|
|
|
|
|
if not available or not self.description.available:
|
|
|
|
return available
|
|
|
|
|
|
|
|
return self.description.available(self.block)
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Return the state attributes."""
|
2021-03-11 20:23:20 +00:00
|
|
|
if self.description.extra_state_attributes is None:
|
2020-09-07 12:13:20 +00:00
|
|
|
return None
|
|
|
|
|
2021-03-11 20:23:20 +00:00
|
|
|
return self.description.extra_state_attributes(self.block)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2021-10-14 22:17:00 +00:00
|
|
|
@property
|
|
|
|
def entity_category(self) -> str | None:
|
|
|
|
"""Return category of entity."""
|
|
|
|
return self.description.entity_category
|
|
|
|
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
class ShellyRestAttributeEntity(update_coordinator.CoordinatorEntity):
|
|
|
|
"""Class to load info from REST."""
|
|
|
|
|
|
|
|
def __init__(
|
2020-11-27 08:40:06 +00:00
|
|
|
self,
|
2021-09-13 06:31:35 +00:00
|
|
|
wrapper: BlockDeviceWrapper,
|
2020-11-27 08:40:06 +00:00
|
|
|
attribute: str,
|
|
|
|
description: RestAttributeDescription,
|
2020-11-11 19:13:14 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize sensor."""
|
|
|
|
super().__init__(wrapper)
|
|
|
|
self.wrapper = wrapper
|
2020-11-27 08:40:06 +00:00
|
|
|
self.attribute = attribute
|
2020-11-11 19:13:14 +00:00
|
|
|
self.description = description
|
2021-09-13 06:31:35 +00:00
|
|
|
self._name = get_block_entity_name(wrapper.device, None, self.description.name)
|
2020-11-27 08:40:06 +00:00
|
|
|
self._last_value = None
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def name(self) -> str:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Name of sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Device info."""
|
|
|
|
return {
|
|
|
|
"connections": {(device_registry.CONNECTION_NETWORK_MAC, self.wrapper.mac)}
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self) -> bool:
|
|
|
|
"""Return if it should be enabled by default."""
|
|
|
|
return self.description.default_enabled
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def available(self) -> bool:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Available."""
|
|
|
|
return self.wrapper.last_update_success
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def attribute_value(self) -> StateType:
|
2020-11-27 08:40:06 +00:00
|
|
|
"""Value of sensor."""
|
2021-07-21 17:11:44 +00:00
|
|
|
if callable(self.description.value):
|
|
|
|
self._last_value = self.description.value(
|
|
|
|
self.wrapper.device.status, self._last_value
|
|
|
|
)
|
2020-11-27 08:40:06 +00:00
|
|
|
return self._last_value
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def device_class(self) -> str | None:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Device class of sensor."""
|
|
|
|
return self.description.device_class
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def icon(self) -> str | None:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Icon of sensor."""
|
|
|
|
return self.description.icon
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Return unique ID of entity."""
|
2020-11-27 08:40:06 +00:00
|
|
|
return f"{self.wrapper.mac}-{self.attribute}"
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Return the state attributes."""
|
2021-03-11 20:23:20 +00:00
|
|
|
if self.description.extra_state_attributes is None:
|
2020-11-11 19:13:14 +00:00
|
|
|
return None
|
|
|
|
|
2021-03-11 20:23:20 +00:00
|
|
|
return self.description.extra_state_attributes(self.wrapper.device.status)
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2021-10-14 22:17:00 +00:00
|
|
|
@property
|
|
|
|
def entity_category(self) -> str | None:
|
|
|
|
"""Return category of entity."""
|
|
|
|
return self.description.entity_category
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity):
|
|
|
|
"""Helper class to represent a rpc attribute."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
wrapper: RpcDeviceWrapper,
|
|
|
|
key: str,
|
|
|
|
attribute: str,
|
|
|
|
description: RpcAttributeDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize sensor."""
|
|
|
|
super().__init__(wrapper, key)
|
2021-10-04 20:46:46 +00:00
|
|
|
self.sub_key = description.sub_key
|
2021-09-17 12:53:39 +00:00
|
|
|
self.attribute = attribute
|
|
|
|
self.description = description
|
|
|
|
|
|
|
|
self._attr_unique_id = f"{super().unique_id}-{attribute}"
|
|
|
|
self._attr_name = get_rpc_entity_name(wrapper.device, key, description.name)
|
|
|
|
self._attr_entity_registry_enabled_default = description.default_enabled
|
|
|
|
self._attr_device_class = description.device_class
|
|
|
|
self._attr_icon = description.icon
|
|
|
|
self._last_value = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attribute_value(self) -> StateType:
|
|
|
|
"""Value of sensor."""
|
|
|
|
if callable(self.description.value):
|
|
|
|
self._last_value = self.description.value(
|
2021-10-04 20:46:46 +00:00
|
|
|
self.wrapper.device.status[self.key][self.sub_key], self._last_value
|
2021-09-17 12:53:39 +00:00
|
|
|
)
|
2021-10-04 20:46:46 +00:00
|
|
|
else:
|
|
|
|
self._last_value = self.wrapper.device.status[self.key][self.sub_key]
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
return self._last_value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Available."""
|
|
|
|
available = super().available
|
|
|
|
|
|
|
|
if not available or not self.description.available:
|
|
|
|
return available
|
|
|
|
|
2021-10-04 20:46:46 +00:00
|
|
|
return self.description.available(
|
|
|
|
self.wrapper.device.status[self.key][self.sub_key]
|
|
|
|
)
|
2021-09-17 12:53:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
|
|
|
"""Return the state attributes."""
|
|
|
|
if self.description.extra_state_attributes is None:
|
|
|
|
return None
|
|
|
|
|
2021-10-15 02:02:37 +00:00
|
|
|
assert self.wrapper.device.shelly
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
return self.description.extra_state_attributes(
|
2021-10-15 02:02:37 +00:00
|
|
|
self.wrapper.device.status[self.key][self.sub_key],
|
|
|
|
self.wrapper.device.shelly,
|
2021-09-17 12:53:39 +00:00
|
|
|
)
|
|
|
|
|
2021-10-14 22:17:00 +00:00
|
|
|
@property
|
|
|
|
def entity_category(self) -> str | None:
|
|
|
|
"""Return category of entity."""
|
|
|
|
return self.description.entity_category
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEntity):
|
|
|
|
"""Represent a shelly sleeping block attribute entity."""
|
|
|
|
|
|
|
|
# pylint: disable=super-init-not-called
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-09-13 06:31:35 +00:00
|
|
|
wrapper: BlockDeviceWrapper,
|
2021-09-10 21:48:55 +00:00
|
|
|
block: Block | None,
|
2021-02-03 16:03:22 +00:00
|
|
|
attribute: str,
|
|
|
|
description: BlockAttributeDescription,
|
2021-05-03 06:49:13 +00:00
|
|
|
entry: entity_registry.RegistryEntry | None = None,
|
2021-07-21 17:11:44 +00:00
|
|
|
sensors: dict[tuple[str, str], BlockAttributeDescription] | None = None,
|
2021-02-03 16:03:22 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the sleeping sensor."""
|
2021-05-03 06:49:13 +00:00
|
|
|
self.sensors = sensors
|
2021-07-21 17:11:44 +00:00
|
|
|
self.last_state: StateType = None
|
2021-02-03 16:03:22 +00:00
|
|
|
self.wrapper = wrapper
|
|
|
|
self.attribute = attribute
|
2021-09-10 21:48:55 +00:00
|
|
|
self.block: Block | None = block # type: ignore[assignment]
|
2021-02-03 16:03:22 +00:00
|
|
|
self.description = description
|
|
|
|
self._unit = self.description.unit
|
|
|
|
|
|
|
|
if block is not None:
|
|
|
|
if callable(self._unit):
|
|
|
|
self._unit = self._unit(block.info(attribute))
|
|
|
|
|
|
|
|
self._unique_id = f"{self.wrapper.mac}-{block.description}-{attribute}"
|
2021-09-13 06:31:35 +00:00
|
|
|
self._name = get_block_entity_name(
|
2021-02-03 16:03:22 +00:00
|
|
|
self.wrapper.device, block, self.description.name
|
|
|
|
)
|
2021-05-03 06:49:13 +00:00
|
|
|
elif entry is not None:
|
2021-02-03 16:03:22 +00:00
|
|
|
self._unique_id = entry.unique_id
|
2021-07-21 17:11:44 +00:00
|
|
|
self._name = cast(str, entry.original_name)
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
last_state = await self.async_get_last_state()
|
|
|
|
|
|
|
|
if last_state is not None:
|
|
|
|
self.last_state = last_state.state
|
2021-05-23 20:10:22 +00:00
|
|
|
self.description.state_class = last_state.attributes.get(ATTR_STATE_CLASS)
|
2021-02-03 16:03:22 +00:00
|
|
|
|
|
|
|
@callback
|
2021-07-21 17:11:44 +00:00
|
|
|
def _update_callback(self) -> None:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Handle device update."""
|
2021-05-03 06:49:13 +00:00
|
|
|
if (
|
|
|
|
self.block is not None
|
|
|
|
or not self.wrapper.device.initialized
|
|
|
|
or self.sensors is None
|
|
|
|
):
|
2021-02-03 16:03:22 +00:00
|
|
|
super()._update_callback()
|
|
|
|
return
|
|
|
|
|
|
|
|
_, entity_block, entity_sensor = self.unique_id.split("-")
|
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
assert self.wrapper.device.blocks
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
for block in self.wrapper.device.blocks:
|
|
|
|
if block.description != entity_block:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for sensor_id in block.sensor_ids:
|
|
|
|
if sensor_id != entity_sensor:
|
|
|
|
continue
|
|
|
|
|
2021-05-03 06:49:13 +00:00
|
|
|
description = self.sensors.get((block.type, sensor_id))
|
|
|
|
if description is None:
|
|
|
|
continue
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
self.block = block
|
2021-05-03 06:49:13 +00:00
|
|
|
self.description = description
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
_LOGGER.debug("Entity %s attached to block", self.name)
|
|
|
|
super()._update_callback()
|
|
|
|
return
|