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
|
|
|
|
|
2016-08-30 19:34:33 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
|
2016-08-30 19:34:33 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-08-30 19:34:33 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2020-04-05 21:54:37 +00:00
|
|
|
from . import DOMAIN, KEY_STATUS, VALUE_ONLINE
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "UPS Online Status"
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2022-01-03 12:10:41 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-08-08 18:21:33 +00:00
|
|
|
"""Set up an APCUPSd Online Status binary sensor."""
|
2020-04-05 21:54:37 +00:00
|
|
|
apcups_data = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
add_entities([OnlineStatus(config, apcups_data)], True)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class OnlineStatus(BinarySensorEntity):
|
2016-08-30 19:34:33 +00:00
|
|
|
"""Representation of an UPS online status."""
|
2016-03-07 19:21:08 +00:00
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
def __init__(self, config, data):
|
2016-08-30 19:34:33 +00:00
|
|
|
"""Initialize the APCUPSd binary device."""
|
2016-02-11 07:33:53 +00:00
|
|
|
self._data = data
|
2021-07-08 09:55:26 +00:00
|
|
|
self._attr_name = config[CONF_NAME]
|
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."""
|
2021-07-08 09:55:26 +00:00
|
|
|
self._attr_is_on = int(self._data.status[KEY_STATUS], 16) & VALUE_ONLINE > 0
|