2019-03-15 00:24:53 +00:00
|
|
|
"""A entity class for mobile_app."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2022-02-23 16:47:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-09-05 00:56:34 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2019-03-15 00:24:53 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-05-09 13:05:31 +00:00
|
|
|
from homeassistant.const import ATTR_ICON, CONF_NAME, CONF_UNIQUE_ID, STATE_UNAVAILABLE
|
2024-01-19 13:41:32 +00:00
|
|
|
from homeassistant.core import State, callback
|
2019-03-15 00:24:53 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-02-11 19:18:03 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2019-03-15 00:24:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_SENSOR_ATTRIBUTES,
|
|
|
|
ATTR_SENSOR_DEVICE_CLASS,
|
2022-05-17 16:05:49 +00:00
|
|
|
ATTR_SENSOR_DISABLED,
|
2021-10-19 19:29:22 +00:00
|
|
|
ATTR_SENSOR_ENTITY_CATEGORY,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_SENSOR_ICON,
|
2021-02-11 19:18:03 +00:00
|
|
|
ATTR_SENSOR_STATE,
|
2024-04-18 12:06:51 +00:00
|
|
|
ATTR_SENSOR_STATE_CLASS,
|
2019-07-31 19:25:30 +00:00
|
|
|
SIGNAL_SENSOR_UPDATE,
|
|
|
|
)
|
2019-06-01 06:01:45 +00:00
|
|
|
from .helpers import device_info
|
2019-03-15 00:24:53 +00:00
|
|
|
|
|
|
|
|
2021-02-11 19:18:03 +00:00
|
|
|
class MobileAppEntity(RestoreEntity):
|
2019-03-15 00:24:53 +00:00
|
|
|
"""Representation of an mobile app entity."""
|
|
|
|
|
2022-05-09 13:05:31 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
2022-02-23 16:47:54 +00:00
|
|
|
def __init__(self, config: dict, entry: ConfigEntry) -> None:
|
2021-02-11 19:18:03 +00:00
|
|
|
"""Initialize the entity."""
|
2019-03-15 00:24:53 +00:00
|
|
|
self._config = config
|
|
|
|
self._entry = entry
|
|
|
|
self._registration = entry.data
|
2022-05-09 13:05:31 +00:00
|
|
|
self._attr_unique_id = config[CONF_UNIQUE_ID]
|
2024-01-19 13:41:32 +00:00
|
|
|
self._attr_entity_registry_enabled_default = not config.get(
|
|
|
|
ATTR_SENSOR_DISABLED
|
|
|
|
)
|
|
|
|
self._attr_name = config[CONF_NAME]
|
|
|
|
self._async_update_attr_from_config()
|
2019-03-15 00:24:53 +00:00
|
|
|
|
2024-01-19 13:41:32 +00:00
|
|
|
@callback
|
|
|
|
def _async_update_attr_from_config(self) -> None:
|
|
|
|
"""Update the entity from the config."""
|
|
|
|
config = self._config
|
|
|
|
self._attr_device_class = config.get(ATTR_SENSOR_DEVICE_CLASS)
|
2024-04-18 12:06:51 +00:00
|
|
|
self._attr_state_class = config.get(ATTR_SENSOR_STATE_CLASS)
|
2024-01-19 13:41:32 +00:00
|
|
|
self._attr_extra_state_attributes = config[ATTR_SENSOR_ATTRIBUTES]
|
|
|
|
self._attr_icon = config[ATTR_SENSOR_ICON]
|
|
|
|
self._attr_entity_category = config.get(ATTR_SENSOR_ENTITY_CATEGORY)
|
|
|
|
self._attr_available = config.get(ATTR_SENSOR_STATE) != STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-03-15 00:24:53 +00:00
|
|
|
"""Register callbacks."""
|
2021-04-01 23:22:08 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
2023-09-05 00:56:34 +00:00
|
|
|
self.hass,
|
|
|
|
f"{SIGNAL_SENSOR_UPDATE}-{self._attr_unique_id}",
|
|
|
|
self._handle_update,
|
2021-04-01 23:22:08 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-02-11 19:18:03 +00:00
|
|
|
|
2021-10-30 14:32:41 +00:00
|
|
|
if (state := await self.async_get_last_state()) is None:
|
2021-02-11 19:18:03 +00:00
|
|
|
return
|
|
|
|
|
2022-08-17 08:53:05 +00:00
|
|
|
await self.async_restore_last_state(state)
|
2019-03-15 00:24:53 +00:00
|
|
|
|
2024-01-19 13:41:32 +00:00
|
|
|
async def async_restore_last_state(self, last_state: State) -> None:
|
2021-02-11 19:18:03 +00:00
|
|
|
"""Restore previous state."""
|
2024-01-19 13:41:32 +00:00
|
|
|
config = self._config
|
|
|
|
config[ATTR_SENSOR_STATE] = last_state.state
|
|
|
|
config[ATTR_SENSOR_ATTRIBUTES] = {
|
2021-02-11 19:18:03 +00:00
|
|
|
**last_state.attributes,
|
|
|
|
**self._config[ATTR_SENSOR_ATTRIBUTES],
|
|
|
|
}
|
|
|
|
if ATTR_ICON in last_state.attributes:
|
2024-01-19 13:41:32 +00:00
|
|
|
config[ATTR_SENSOR_ICON] = last_state.attributes[ATTR_ICON]
|
2021-10-19 19:29:22 +00:00
|
|
|
|
2019-03-15 00:24:53 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device registry information for this entity."""
|
2019-06-01 06:01:45 +00:00
|
|
|
return device_info(self._registration)
|
2019-03-15 00:24:53 +00:00
|
|
|
|
|
|
|
@callback
|
2023-09-05 00:56:34 +00:00
|
|
|
def _handle_update(self, data: dict[str, Any]) -> None:
|
2019-03-15 00:24:53 +00:00
|
|
|
"""Handle async event updates."""
|
2023-09-05 00:56:34 +00:00
|
|
|
self._config.update(data)
|
2024-01-19 13:41:32 +00:00
|
|
|
self._async_update_attr_from_config()
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|