2019-08-07 22:28:22 +00:00
|
|
|
"""Support for Home Assistant Updater binary sensors."""
|
2021-08-05 07:18:03 +00:00
|
|
|
from __future__ import annotations
|
2019-08-07 22:28:22 +00:00
|
|
|
|
2021-08-17 06:30:35 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 11:52:29 +00:00
|
|
|
BinarySensorDeviceClass,
|
2021-08-17 06:30:35 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-01-03 12:13:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2020-08-30 12:40:55 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-08-07 22:28:22 +00:00
|
|
|
|
2020-02-01 16:14:28 +00:00
|
|
|
from . import ATTR_NEWEST_VERSION, ATTR_RELEASE_NOTES, DOMAIN as UPDATER_DOMAIN
|
2019-08-07 22:28:22 +00:00
|
|
|
|
|
|
|
|
2022-01-03 12:13:03 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2019-08-07 22:28:22 +00:00
|
|
|
"""Set up the updater binary sensors."""
|
2020-02-01 16:14:28 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities([UpdaterBinary(hass.data[UPDATER_DOMAIN])])
|
2019-08-07 22:28:22 +00:00
|
|
|
|
|
|
|
|
2020-08-30 12:40:55 +00:00
|
|
|
class UpdaterBinary(CoordinatorEntity, BinarySensorEntity):
|
2019-08-07 22:28:22 +00:00
|
|
|
"""Representation of an updater binary sensor."""
|
|
|
|
|
2021-12-16 11:52:29 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.UPDATE
|
2021-08-17 06:30:35 +00:00
|
|
|
_attr_name = "Updater"
|
|
|
|
_attr_unique_id = "updater"
|
2019-08-07 22:28:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-03 15:40:07 +00:00
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if there is an update available."""
|
|
|
|
return self.coordinator.data and self.coordinator.data.update_available
|
2019-08-07 22:28:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-05 07:18:03 +00:00
|
|
|
def extra_state_attributes(self) -> dict | None:
|
2019-08-07 22:28:22 +00:00
|
|
|
"""Return the optional state attributes."""
|
2020-05-16 21:34:34 +00:00
|
|
|
if not self.coordinator.data:
|
|
|
|
return None
|
2020-02-01 16:14:28 +00:00
|
|
|
data = {}
|
|
|
|
if self.coordinator.data.release_notes:
|
|
|
|
data[ATTR_RELEASE_NOTES] = self.coordinator.data.release_notes
|
|
|
|
if self.coordinator.data.newest_version:
|
|
|
|
data[ATTR_NEWEST_VERSION] = self.coordinator.data.newest_version
|
2019-08-07 22:28:22 +00:00
|
|
|
return data
|