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
|
2023-11-21 21:40:05 +00:00
|
|
|
from typing import Final
|
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
|
2023-11-21 21:40:05 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2023-11-21 21:40:05 +00:00
|
|
|
from . import DOMAIN, APCUPSdCoordinator
|
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",
|
2024-02-25 09:42:04 +00:00
|
|
|
translation_key="online_status",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2023-11-21 21:40:05 +00:00
|
|
|
# The bit in STATFLAG that indicates the online status of the APC UPS.
|
|
|
|
_VALUE_ONLINE_MASK: Final = 0b1000
|
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."""
|
2023-11-21 21:40:05 +00:00
|
|
|
coordinator: APCUPSdCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2022-09-28 07:14:04 +00:00
|
|
|
|
|
|
|
# Do not create the binary sensor if APCUPSd does not provide STATFLAG field for us
|
|
|
|
# to determine the online status.
|
2023-11-21 21:40:05 +00:00
|
|
|
if _DESCRIPTION.key.upper() not in coordinator.data:
|
2022-09-28 07:14:04 +00:00
|
|
|
return
|
2020-04-05 21:54:37 +00:00
|
|
|
|
2023-11-21 21:40:05 +00:00
|
|
|
async_add_entities([OnlineStatus(coordinator, _DESCRIPTION)])
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2023-11-21 21:40:05 +00:00
|
|
|
class OnlineStatus(CoordinatorEntity[APCUPSdCoordinator], 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,
|
2023-11-21 21:40:05 +00:00
|
|
|
coordinator: APCUPSdCoordinator,
|
2022-09-28 07:14:04 +00:00
|
|
|
description: BinarySensorEntityDescription,
|
|
|
|
) -> None:
|
2016-08-30 19:34:33 +00:00
|
|
|
"""Initialize the APCUPSd binary device."""
|
2023-11-21 21:40:05 +00:00
|
|
|
super().__init__(coordinator, context=description.key.upper())
|
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
# Set up unique id and device info if serial number is available.
|
2023-11-21 21:40:05 +00:00
|
|
|
if (serial_no := coordinator.ups_serial_no) is not None:
|
2022-09-28 07:14:04 +00:00
|
|
|
self._attr_unique_id = f"{serial_no}_{description.key}"
|
|
|
|
self.entity_description = description
|
2023-11-21 21:40:05 +00:00
|
|
|
self._attr_device_info = coordinator.device_info
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2023-11-21 21:40:05 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Returns true if the UPS is online."""
|
|
|
|
# Check if ONLINE bit is set in STATFLAG.
|
2022-09-28 07:14:04 +00:00
|
|
|
key = self.entity_description.key.upper()
|
2023-11-21 21:40:05 +00:00
|
|
|
return int(self.coordinator.data[key], 16) & _VALUE_ONLINE_MASK != 0
|