Files
core/homeassistant/components/cloud/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.1 KiB
Python
Raw Normal View History

"""Support for Home Assistant Cloud binary sensors."""
from __future__ import annotations
2019-03-25 17:43:15 +01:00
import asyncio
2023-05-24 11:46:11 +02:00
from typing import Any
from hass_nabucasa import Cloud
2019-03-25 17:43:15 +01:00
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2023-05-24 11:46:11 +02:00
from .client import CloudClient
2024-06-24 21:02:08 +02:00
from .const import DATA_CLOUD, DISPATCHER_REMOTE_UPDATE
2019-03-25 17:43:15 +01:00
WAIT_UNTIL_CHANGE = 3
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Home Assistant Cloud binary sensors."""
2024-06-24 21:02:08 +02:00
cloud = hass.data[DATA_CLOUD]
async_add_entities([CloudRemoteBinary(cloud)])
class CloudRemoteBinary(BinarySensorEntity):
"""Representation of an Cloud Remote UI Connection binary sensor."""
_attr_name = "Remote UI"
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
_attr_should_poll = False
_attr_unique_id = "cloud-remote-ui-connectivity"
_attr_entity_category = EntityCategory.DIAGNOSTIC
2023-05-24 11:46:11 +02:00
def __init__(self, cloud: Cloud[CloudClient]) -> None:
"""Initialize the binary sensor."""
self.cloud = cloud
@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
2022-08-19 13:02:46 +02:00
async def async_added_to_hass(self) -> None:
"""Register update dispatcher."""
2019-07-31 12:25:30 -07:00
2023-05-24 11:46:11 +02:00
async def async_state_update(data: Any) -> None:
"""Update callback."""
2019-03-25 17:43:15 +01:00
await asyncio.sleep(WAIT_UNTIL_CHANGE)
2020-04-01 14:19:51 -07:00
self.async_write_ha_state()
self.async_on_remove(
async_dispatcher_connect(
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update
)
)