2021-03-01 08:41:04 +00:00
|
|
|
"""Sensor platform for Hass.io addons."""
|
2021-03-18 08:25:40 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-22 10:23:21 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2023-03-13 14:39:49 +00:00
|
|
|
SensorDeviceClass,
|
2021-10-22 10:23:21 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-13 20:14:50 +00:00
|
|
|
SensorStateClass,
|
2021-10-22 10:23:21 +00:00
|
|
|
)
|
2021-03-01 08:41:04 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-03-13 14:39:49 +00:00
|
|
|
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfInformation
|
2021-03-01 08:41:04 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-03-01 08:41:04 +00:00
|
|
|
|
|
|
|
from . import ADDONS_COORDINATOR
|
2021-10-22 10:23:21 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_CPU_PERCENT,
|
|
|
|
ATTR_MEMORY_PERCENT,
|
|
|
|
ATTR_VERSION,
|
|
|
|
ATTR_VERSION_LATEST,
|
|
|
|
DATA_KEY_ADDONS,
|
2023-03-09 18:06:35 +00:00
|
|
|
DATA_KEY_CORE,
|
2023-03-13 14:39:49 +00:00
|
|
|
DATA_KEY_HOST,
|
2021-10-22 10:23:21 +00:00
|
|
|
DATA_KEY_OS,
|
2023-03-09 18:06:35 +00:00
|
|
|
DATA_KEY_SUPERVISOR,
|
|
|
|
)
|
|
|
|
from .entity import (
|
|
|
|
HassioAddonEntity,
|
|
|
|
HassioCoreEntity,
|
2023-03-13 14:39:49 +00:00
|
|
|
HassioHostEntity,
|
2023-03-09 18:06:35 +00:00
|
|
|
HassioOSEntity,
|
|
|
|
HassioSupervisorEntity,
|
2021-10-22 10:23:21 +00:00
|
|
|
)
|
2021-03-01 08:41:04 +00:00
|
|
|
|
2021-10-22 10:23:21 +00:00
|
|
|
COMMON_ENTITY_DESCRIPTIONS = (
|
2021-08-17 10:14:14 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key=ATTR_VERSION,
|
|
|
|
name="Version",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key=ATTR_VERSION_LATEST,
|
2022-07-10 20:05:54 +00:00
|
|
|
name="Newest version",
|
2021-08-17 10:14:14 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-03-09 18:06:35 +00:00
|
|
|
STATS_ENTITY_DESCRIPTIONS = (
|
2021-10-22 10:23:21 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key=ATTR_CPU_PERCENT,
|
2022-07-10 20:05:54 +00:00
|
|
|
name="CPU percent",
|
2021-10-22 10:23:21 +00:00
|
|
|
icon="mdi:cpu-64-bit",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-13 20:14:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-10-22 10:23:21 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key=ATTR_MEMORY_PERCENT,
|
2022-07-10 20:05:54 +00:00
|
|
|
name="Memory percent",
|
2021-10-22 10:23:21 +00:00
|
|
|
icon="mdi:memory",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-13 20:14:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-10-22 10:23:21 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-03-09 18:06:35 +00:00
|
|
|
ADDON_ENTITY_DESCRIPTIONS = COMMON_ENTITY_DESCRIPTIONS + STATS_ENTITY_DESCRIPTIONS
|
|
|
|
CORE_ENTITY_DESCRIPTIONS = STATS_ENTITY_DESCRIPTIONS
|
2021-10-22 10:23:21 +00:00
|
|
|
OS_ENTITY_DESCRIPTIONS = COMMON_ENTITY_DESCRIPTIONS
|
2023-03-09 18:06:35 +00:00
|
|
|
SUPERVISOR_ENTITY_DESCRIPTIONS = STATS_ENTITY_DESCRIPTIONS
|
2021-10-22 10:23:21 +00:00
|
|
|
|
2023-03-13 14:39:49 +00:00
|
|
|
HOST_ENTITY_DESCRIPTIONS = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key="agent_version",
|
|
|
|
name="OS Agent version",
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key="apparmor_version",
|
|
|
|
name="Apparmor version",
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key="disk_total",
|
|
|
|
name="Disk total",
|
|
|
|
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key="disk_used",
|
|
|
|
name="Disk used",
|
|
|
|
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
key="disk_free",
|
|
|
|
name="Disk free",
|
|
|
|
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-03-01 08:41:04 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
2021-04-29 10:28:14 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-01 08:41:04 +00:00
|
|
|
) -> None:
|
|
|
|
"""Sensor set up for Hass.io config entry."""
|
|
|
|
coordinator = hass.data[ADDONS_COORDINATOR]
|
|
|
|
|
2023-03-09 18:06:35 +00:00
|
|
|
entities: list[
|
2023-03-13 14:39:49 +00:00
|
|
|
HassioOSSensor | HassioAddonSensor | CoreSensor | SupervisorSensor | HostSensor
|
2023-03-09 18:06:35 +00:00
|
|
|
] = []
|
2021-03-01 08:41:04 +00:00
|
|
|
|
2021-10-22 10:23:21 +00:00
|
|
|
for addon in coordinator.data[DATA_KEY_ADDONS].values():
|
|
|
|
for entity_description in ADDON_ENTITY_DESCRIPTIONS:
|
2021-03-01 08:41:04 +00:00
|
|
|
entities.append(
|
2021-08-17 10:14:14 +00:00
|
|
|
HassioAddonSensor(
|
|
|
|
addon=addon,
|
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
2021-03-01 08:41:04 +00:00
|
|
|
)
|
2021-08-17 10:14:14 +00:00
|
|
|
|
2023-03-09 18:06:35 +00:00
|
|
|
for entity_description in CORE_ENTITY_DESCRIPTIONS:
|
|
|
|
entities.append(
|
|
|
|
CoreSensor(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for entity_description in SUPERVISOR_ENTITY_DESCRIPTIONS:
|
|
|
|
entities.append(
|
|
|
|
SupervisorSensor(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-03-13 14:39:49 +00:00
|
|
|
for entity_description in HOST_ENTITY_DESCRIPTIONS:
|
|
|
|
entities.append(
|
|
|
|
HostSensor(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-10-22 10:23:21 +00:00
|
|
|
if coordinator.is_hass_os:
|
|
|
|
for entity_description in OS_ENTITY_DESCRIPTIONS:
|
2021-08-17 10:14:14 +00:00
|
|
|
entities.append(
|
|
|
|
HassioOSSensor(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
|
|
|
)
|
2021-03-01 08:41:04 +00:00
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class HassioAddonSensor(HassioAddonEntity, SensorEntity):
|
2021-03-01 08:41:04 +00:00
|
|
|
"""Sensor to track a Hass.io add-on attribute."""
|
|
|
|
|
|
|
|
@property
|
2021-08-12 12:23:56 +00:00
|
|
|
def native_value(self) -> str:
|
2021-08-17 10:14:14 +00:00
|
|
|
"""Return native value of entity."""
|
|
|
|
return self.coordinator.data[DATA_KEY_ADDONS][self._addon_slug][
|
|
|
|
self.entity_description.key
|
|
|
|
]
|
2021-03-01 08:41:04 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class HassioOSSensor(HassioOSEntity, SensorEntity):
|
2021-03-01 08:41:04 +00:00
|
|
|
"""Sensor to track a Hass.io add-on attribute."""
|
|
|
|
|
|
|
|
@property
|
2021-08-12 12:23:56 +00:00
|
|
|
def native_value(self) -> str:
|
2021-08-17 10:14:14 +00:00
|
|
|
"""Return native value of entity."""
|
|
|
|
return self.coordinator.data[DATA_KEY_OS][self.entity_description.key]
|
2023-03-09 18:06:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CoreSensor(HassioCoreEntity, SensorEntity):
|
|
|
|
"""Sensor to track a core attribute."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> str:
|
|
|
|
"""Return native value of entity."""
|
|
|
|
return self.coordinator.data[DATA_KEY_CORE][self.entity_description.key]
|
|
|
|
|
|
|
|
|
|
|
|
class SupervisorSensor(HassioSupervisorEntity, SensorEntity):
|
|
|
|
"""Sensor to track a supervisor attribute."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> str:
|
|
|
|
"""Return native value of entity."""
|
|
|
|
return self.coordinator.data[DATA_KEY_SUPERVISOR][self.entity_description.key]
|
2023-03-13 14:39:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HostSensor(HassioHostEntity, SensorEntity):
|
|
|
|
"""Sensor to track a host attribute."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> str:
|
|
|
|
"""Return native value of entity."""
|
|
|
|
return self.coordinator.data[DATA_KEY_HOST][self.entity_description.key]
|