2019-03-15 17:39:53 +00:00
|
|
|
"""Support for Home Assistant Cloud binary sensors."""
|
2022-01-03 12:10:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-03-25 16:43:15 +00:00
|
|
|
import asyncio
|
|
|
|
|
2020-09-12 18:21:57 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-09 08:40:12 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 18:21:57 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-15 17:39:53 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-09 08:40:12 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-03-15 17:39:53 +00:00
|
|
|
|
|
|
|
from .const import DISPATCHER_REMOTE_UPDATE, DOMAIN
|
|
|
|
|
2019-03-25 16:43:15 +00:00
|
|
|
WAIT_UNTIL_CHANGE = 3
|
|
|
|
|
|
|
|
|
2022-01-03 12:10:41 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2019-03-15 17:39:53 +00:00
|
|
|
"""Set up the cloud binary sensors."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
cloud = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
async_add_entities([CloudRemoteBinary(cloud)])
|
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class CloudRemoteBinary(BinarySensorEntity):
|
2019-03-15 17:39:53 +00:00
|
|
|
"""Representation of an Cloud Remote UI Connection binary sensor."""
|
|
|
|
|
2021-07-26 19:23:32 +00:00
|
|
|
_attr_name = "Remote UI"
|
2021-12-09 08:40:12 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
2021-07-26 19:23:32 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
_attr_unique_id = "cloud-remote-ui-connectivity"
|
2021-12-09 08:40:12 +00:00
|
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
2021-07-26 19:23:32 +00:00
|
|
|
|
2019-03-15 17:39:53 +00:00
|
|
|
def __init__(self, cloud):
|
|
|
|
"""Initialize the binary sensor."""
|
|
|
|
self.cloud = cloud
|
|
|
|
self._unsub_dispatcher = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self.cloud.remote.is_connected
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.cloud.remote.certificate is not None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register update dispatcher."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-25 16:43:15 +00:00
|
|
|
async def async_state_update(data):
|
2019-03-15 17:39:53 +00:00
|
|
|
"""Update callback."""
|
2019-03-25 16:43:15 +00:00
|
|
|
await asyncio.sleep(WAIT_UNTIL_CHANGE)
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-03-15 17:39:53 +00:00
|
|
|
|
|
|
|
self._unsub_dispatcher = async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update
|
|
|
|
)
|
2019-03-15 17:39:53 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Register update dispatcher."""
|
|
|
|
if self._unsub_dispatcher is not None:
|
|
|
|
self._unsub_dispatcher()
|
|
|
|
self._unsub_dispatcher = None
|