2019-04-03 15:40:03 +00:00
|
|
|
"""Provides a sensor to track various status aspects of a UPS."""
|
2021-08-11 13:56:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-03 23:19:43 +00:00
|
|
|
import logging
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2021-08-11 13:56:41 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2022-01-03 18:09:36 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-11-10 12:49:05 +00:00
|
|
|
from homeassistant.const import STATE_UNKNOWN
|
2022-01-03 18:09:36 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-24 09:34:45 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 18:09:36 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-08-11 13:56:41 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2021-12-12 23:24:46 +00:00
|
|
|
from . import PyNUTData
|
2020-04-01 02:08:27 +00:00
|
|
|
from .const import (
|
2020-04-09 01:26:10 +00:00
|
|
|
COORDINATOR,
|
2020-04-01 02:08:27 +00:00
|
|
|
DOMAIN,
|
|
|
|
KEY_STATUS,
|
|
|
|
KEY_STATUS_DISPLAY,
|
|
|
|
PYNUT_DATA,
|
|
|
|
PYNUT_UNIQUE_ID,
|
|
|
|
SENSOR_TYPES,
|
|
|
|
STATE_TYPES,
|
|
|
|
)
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2020-04-01 02:08:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-12-01 07:58:16 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:09:36 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-04-01 02:08:27 +00:00
|
|
|
"""Set up the NUT sensors."""
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2020-04-01 02:08:27 +00:00
|
|
|
pynut_data = hass.data[DOMAIN][config_entry.entry_id]
|
2020-04-09 01:26:10 +00:00
|
|
|
coordinator = pynut_data[COORDINATOR]
|
|
|
|
data = pynut_data[PYNUT_DATA]
|
2021-10-17 17:43:15 +00:00
|
|
|
unique_id = pynut_data[PYNUT_UNIQUE_ID]
|
2021-10-14 02:05:06 +00:00
|
|
|
status = coordinator.data
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2021-10-13 21:33:03 +00:00
|
|
|
resources = [sensor_id for sensor_id in SENSOR_TYPES if sensor_id in status]
|
|
|
|
# Display status is a special case that falls back to the status value
|
|
|
|
# of the UPS instead.
|
|
|
|
if KEY_STATUS in resources:
|
|
|
|
resources.append(KEY_STATUS_DISPLAY)
|
|
|
|
|
|
|
|
entities = [
|
|
|
|
NUTSensor(
|
|
|
|
coordinator,
|
|
|
|
SENSOR_TYPES[sensor_type],
|
2021-10-17 17:43:15 +00:00
|
|
|
data,
|
2021-10-13 21:33:03 +00:00
|
|
|
unique_id,
|
|
|
|
)
|
|
|
|
for sensor_type in resources
|
|
|
|
]
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2020-04-01 02:08:27 +00:00
|
|
|
async_add_entities(entities, True)
|
2016-12-01 07:58:16 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class NUTSensor(CoordinatorEntity, SensorEntity):
|
2016-12-01 07:58:16 +00:00
|
|
|
"""Representation of a sensor entity for NUT status values."""
|
|
|
|
|
2020-04-01 02:08:27 +00:00
|
|
|
def __init__(
|
2020-04-09 01:26:10 +00:00
|
|
|
self,
|
2021-08-11 13:56:41 +00:00
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
sensor_description: SensorEntityDescription,
|
2021-10-17 17:43:15 +00:00
|
|
|
data: PyNUTData,
|
2021-08-11 13:56:41 +00:00
|
|
|
unique_id: str,
|
|
|
|
) -> None:
|
2016-12-01 07:58:16 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-08-30 12:40:26 +00:00
|
|
|
super().__init__(coordinator)
|
2021-08-11 13:56:41 +00:00
|
|
|
self.entity_description = sensor_description
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2021-10-17 17:43:15 +00:00
|
|
|
device_name = data.name.title()
|
|
|
|
self._attr_name = f"{device_name} {sensor_description.name}"
|
2021-10-14 02:05:06 +00:00
|
|
|
self._attr_unique_id = f"{unique_id}_{sensor_description.key}"
|
2021-10-24 09:34:45 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, unique_id)},
|
|
|
|
name=device_name,
|
|
|
|
)
|
2021-10-17 17:43:15 +00:00
|
|
|
self._attr_device_info.update(data.device_info)
|
2020-04-01 02:08:27 +00:00
|
|
|
|
2016-12-01 07:58:16 +00:00
|
|
|
@property
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self):
|
2016-12-01 07:58:16 +00:00
|
|
|
"""Return entity state from ups."""
|
2021-10-14 02:05:06 +00:00
|
|
|
status = self.coordinator.data
|
2021-08-11 13:56:41 +00:00
|
|
|
if self.entity_description.key == KEY_STATUS_DISPLAY:
|
2021-10-14 02:05:06 +00:00
|
|
|
return _format_display_state(status)
|
|
|
|
return status.get(self.entity_description.key)
|
2016-12-01 07:58:16 +00:00
|
|
|
|
2020-03-30 01:53:25 +00:00
|
|
|
|
|
|
|
def _format_display_state(status):
|
|
|
|
"""Return UPS display state."""
|
|
|
|
try:
|
|
|
|
return " ".join(STATE_TYPES[state] for state in status[KEY_STATUS].split())
|
|
|
|
except KeyError:
|
|
|
|
return STATE_UNKNOWN
|