Move cloud binary sensor to config entry (#106409)

* Move cloud binary sensor to config entry

* Fix docstring
This commit is contained in:
Martin Hjelmare 2023-12-26 15:12:37 +01:00 committed by GitHub
parent c8f9285aba
commit e764372d1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 21 deletions

View file

@ -63,7 +63,7 @@ from .subscription import async_subscription_info
DEFAULT_MODE = MODE_PROD DEFAULT_MODE = MODE_PROD
PLATFORMS = [Platform.STT] PLATFORMS = [Platform.BINARY_SENSOR, Platform.STT]
SERVICE_REMOTE_CONNECT = "remote_connect" SERVICE_REMOTE_CONNECT = "remote_connect"
SERVICE_REMOTE_DISCONNECT = "remote_disconnect" SERVICE_REMOTE_DISCONNECT = "remote_disconnect"
@ -284,7 +284,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
tts_info = {"platform_loaded": tts_platform_loaded} tts_info = {"platform_loaded": tts_platform_loaded}
await async_load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config)
await async_load_platform(hass, Platform.TTS, DOMAIN, tts_info, config) await async_load_platform(hass, Platform.TTS, DOMAIN, tts_info, config)
await tts_platform_loaded.wait() await tts_platform_loaded.wait()

View file

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Callable
from typing import Any from typing import Any
from hass_nabucasa import Cloud from hass_nabucasa import Cloud
@ -11,11 +10,11 @@ from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .client import CloudClient from .client import CloudClient
from .const import DISPATCHER_REMOTE_UPDATE, DOMAIN from .const import DISPATCHER_REMOTE_UPDATE, DOMAIN
@ -23,17 +22,13 @@ from .const import DISPATCHER_REMOTE_UPDATE, DOMAIN
WAIT_UNTIL_CHANGE = 3 WAIT_UNTIL_CHANGE = 3
async def async_setup_platform( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the cloud binary sensors.""" """Set up the Home Assistant Cloud binary sensors."""
if discovery_info is None: cloud: Cloud[CloudClient] = hass.data[DOMAIN]
return
cloud = hass.data[DOMAIN]
async_add_entities([CloudRemoteBinary(cloud)]) async_add_entities([CloudRemoteBinary(cloud)])
@ -49,7 +44,6 @@ class CloudRemoteBinary(BinarySensorEntity):
def __init__(self, cloud: Cloud[CloudClient]) -> None: def __init__(self, cloud: Cloud[CloudClient]) -> None:
"""Initialize the binary sensor.""" """Initialize the binary sensor."""
self.cloud = cloud self.cloud = cloud
self._unsub_dispatcher: Callable[[], None] | None = None
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
@ -69,12 +63,8 @@ class CloudRemoteBinary(BinarySensorEntity):
await asyncio.sleep(WAIT_UNTIL_CHANGE) await asyncio.sleep(WAIT_UNTIL_CHANGE)
self.async_write_ha_state() self.async_write_ha_state()
self._unsub_dispatcher = async_dispatcher_connect( self.async_on_remove(
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update async_dispatcher_connect(
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update
)
) )
async def async_will_remove_from_hass(self) -> None:
"""Register update dispatcher."""
if self._unsub_dispatcher is not None:
self._unsub_dispatcher()
self._unsub_dispatcher = None