2021-03-01 09:41:04 +01:00
|
|
|
"""Base for Hass.io entities."""
|
2021-03-18 09:25:40 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2021-03-01 09:41:04 +01:00
|
|
|
|
|
|
|
from homeassistant.const import ATTR_NAME
|
2021-10-23 15:35:33 +02:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
2021-03-01 09:41:04 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
from . import DOMAIN, HassioDataUpdateCoordinator
|
2021-10-26 19:58:17 +02:00
|
|
|
from .const import ATTR_SLUG, DATA_KEY_ADDONS, DATA_KEY_OS
|
2021-03-01 09:41:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HassioAddonEntity(CoordinatorEntity):
|
|
|
|
"""Base entity for a Hass.io add-on."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: HassioDataUpdateCoordinator,
|
2021-08-17 12:14:14 +02:00
|
|
|
entity_description: EntityDescription,
|
2021-03-18 09:25:40 +01:00
|
|
|
addon: dict[str, Any],
|
2021-03-01 09:41:04 +01:00
|
|
|
) -> None:
|
|
|
|
"""Initialize base entity."""
|
|
|
|
super().__init__(coordinator)
|
2021-08-17 12:14:14 +02:00
|
|
|
self.entity_description = entity_description
|
|
|
|
self._addon_slug = addon[ATTR_SLUG]
|
|
|
|
self._attr_name = f"{addon[ATTR_NAME]}: {entity_description.name}"
|
|
|
|
self._attr_unique_id = f"{addon[ATTR_SLUG]}_{entity_description.key}"
|
2021-10-23 15:35:33 +02:00
|
|
|
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, addon[ATTR_SLUG])})
|
2021-03-01 09:41:04 +01:00
|
|
|
|
2021-10-26 19:58:17 +02:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return (
|
|
|
|
super().available
|
|
|
|
and self.entity_description.key
|
|
|
|
in self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug]
|
|
|
|
)
|
|
|
|
|
2021-03-01 09:41:04 +01:00
|
|
|
|
|
|
|
class HassioOSEntity(CoordinatorEntity):
|
|
|
|
"""Base Entity for Hass.io OS."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: HassioDataUpdateCoordinator,
|
2021-08-17 12:14:14 +02:00
|
|
|
entity_description: EntityDescription,
|
2021-03-01 09:41:04 +01:00
|
|
|
) -> None:
|
|
|
|
"""Initialize base entity."""
|
|
|
|
super().__init__(coordinator)
|
2021-08-17 12:14:14 +02:00
|
|
|
self.entity_description = entity_description
|
|
|
|
self._attr_name = f"Home Assistant Operating System: {entity_description.name}"
|
|
|
|
self._attr_unique_id = f"home_assistant_os_{entity_description.key}"
|
2021-10-23 15:35:33 +02:00
|
|
|
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, "OS")})
|
2021-10-26 19:58:17 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return (
|
|
|
|
super().available
|
|
|
|
and self.entity_description.key in self.coordinator.data[DATA_KEY_OS]
|
|
|
|
)
|