2019-02-13 20:21:14 +00:00
|
|
|
"""Support for tracking the online status of a UPS."""
|
2022-01-03 12:10:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
import logging
|
2016-08-30 19:34:33 +00:00
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
from . import DOMAIN, VALUE_ONLINE, APCUPSdData
|
2020-04-05 21:54:37 +00:00
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
_DESCRIPTION = BinarySensorEntityDescription(
|
|
|
|
key="statflag",
|
|
|
|
name="UPS Online Status",
|
|
|
|
icon="mdi:heart",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 12:10:41 +00:00
|
|
|
hass: HomeAssistant,
|
2022-09-28 07:14:04 +00:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 12:10:41 +00:00
|
|
|
) -> None:
|
2017-08-08 18:21:33 +00:00
|
|
|
"""Set up an APCUPSd Online Status binary sensor."""
|
2022-09-28 07:14:04 +00:00
|
|
|
data_service: APCUPSdData = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
|
|
|
|
# Do not create the binary sensor if APCUPSd does not provide STATFLAG field for us
|
|
|
|
# to determine the online status.
|
|
|
|
if data_service.statflag is None:
|
|
|
|
return
|
2020-04-05 21:54:37 +00:00
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
async_add_entities(
|
|
|
|
[OnlineStatus(data_service, _DESCRIPTION)],
|
|
|
|
update_before_add=True,
|
|
|
|
)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class OnlineStatus(BinarySensorEntity):
|
2022-09-28 07:14:04 +00:00
|
|
|
"""Representation of a UPS online status."""
|
2016-03-07 19:21:08 +00:00
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data_service: APCUPSdData,
|
|
|
|
description: BinarySensorEntityDescription,
|
|
|
|
) -> None:
|
2016-08-30 19:34:33 +00:00
|
|
|
"""Initialize the APCUPSd binary device."""
|
2022-09-28 07:14:04 +00:00
|
|
|
# Set up unique id and device info if serial number is available.
|
|
|
|
if (serial_no := data_service.serial_no) is not None:
|
|
|
|
self._attr_unique_id = f"{serial_no}_{description.key}"
|
2023-07-24 16:42:08 +00:00
|
|
|
self._attr_device_info = data_service.device_info
|
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._data_service = data_service
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2022-08-18 13:56:52 +00:00
|
|
|
def update(self) -> None:
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Get the status report from APCUPSd and set this entity's state."""
|
2023-01-15 13:45:05 +00:00
|
|
|
try:
|
|
|
|
self._data_service.update()
|
|
|
|
except OSError as ex:
|
|
|
|
if self._attr_available:
|
|
|
|
self._attr_available = False
|
|
|
|
_LOGGER.exception("Got exception while fetching state: %s", ex)
|
|
|
|
return
|
2022-09-28 07:14:04 +00:00
|
|
|
|
2023-01-15 13:45:05 +00:00
|
|
|
self._attr_available = True
|
2022-09-28 07:14:04 +00:00
|
|
|
key = self.entity_description.key.upper()
|
|
|
|
if key not in self._data_service.status:
|
|
|
|
self._attr_is_on = None
|
|
|
|
return
|
|
|
|
|
|
|
|
self._attr_is_on = int(self._data_service.status[key], 16) & VALUE_ONLINE > 0
|