2020-07-11 01:20:50 +00:00
|
|
|
"""An abstract class common to all Bond entities."""
|
2021-03-17 22:34:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-07-23 02:15:27 +00:00
|
|
|
from abc import abstractmethod
|
2021-02-09 08:43:38 +00:00
|
|
|
from asyncio import Lock, TimeoutError as AsyncIOTimeoutError
|
|
|
|
from datetime import timedelta
|
2020-07-24 20:14:47 +00:00
|
|
|
import logging
|
2021-03-17 22:34:25 +00:00
|
|
|
from typing import Any
|
2020-07-11 01:20:50 +00:00
|
|
|
|
2020-07-24 20:14:47 +00:00
|
|
|
from aiohttp import ClientError
|
2021-02-09 08:43:38 +00:00
|
|
|
from bond_api import BPUPSubscriptions
|
2020-07-24 20:14:47 +00:00
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
from homeassistant.const import ATTR_NAME
|
2021-02-09 08:43:38 +00:00
|
|
|
from homeassistant.core import callback
|
2020-07-23 02:15:27 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-02-09 08:43:38 +00:00
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
2020-07-11 01:20:50 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
2020-07-12 16:31:53 +00:00
|
|
|
from .utils import BondDevice, BondHub
|
2020-07-11 01:20:50 +00:00
|
|
|
|
2020-07-24 20:14:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-02-09 08:43:38 +00:00
|
|
|
_FALLBACK_SCAN_INTERVAL = timedelta(seconds=10)
|
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
|
2020-07-23 02:15:27 +00:00
|
|
|
class BondEntity(Entity):
|
2020-07-11 01:20:50 +00:00
|
|
|
"""Generic Bond entity encapsulating common features of any Bond controlled device."""
|
|
|
|
|
2020-10-18 19:11:24 +00:00
|
|
|
def __init__(
|
2021-02-09 08:43:38 +00:00
|
|
|
self,
|
|
|
|
hub: BondHub,
|
|
|
|
device: BondDevice,
|
|
|
|
bpup_subs: BPUPSubscriptions,
|
2021-03-17 22:34:25 +00:00
|
|
|
sub_device: str | None = None,
|
2020-10-18 19:11:24 +00:00
|
|
|
):
|
2020-07-11 01:20:50 +00:00
|
|
|
"""Initialize entity with API and device info."""
|
2020-07-12 16:31:53 +00:00
|
|
|
self._hub = hub
|
2020-07-11 01:20:50 +00:00
|
|
|
self._device = device
|
2021-02-09 08:43:38 +00:00
|
|
|
self._device_id = device.device_id
|
2020-10-18 19:11:24 +00:00
|
|
|
self._sub_device = sub_device
|
2020-07-24 20:14:47 +00:00
|
|
|
self._available = True
|
2021-02-09 08:43:38 +00:00
|
|
|
self._bpup_subs = bpup_subs
|
2021-03-17 22:34:25 +00:00
|
|
|
self._update_lock: Lock | None = None
|
2021-02-09 08:43:38 +00:00
|
|
|
self._initialized = False
|
2020-07-11 01:20:50 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-17 22:34:25 +00:00
|
|
|
def unique_id(self) -> str | None:
|
2020-07-11 01:20:50 +00:00
|
|
|
"""Get unique ID for the entity."""
|
2020-08-03 02:50:03 +00:00
|
|
|
hub_id = self._hub.bond_id
|
2021-02-09 08:43:38 +00:00
|
|
|
device_id = self._device_id
|
2020-10-18 19:11:24 +00:00
|
|
|
sub_device_id: str = f"_{self._sub_device}" if self._sub_device else ""
|
|
|
|
return f"{hub_id}_{device_id}{sub_device_id}"
|
2020-07-11 01:20:50 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-17 22:34:25 +00:00
|
|
|
def name(self) -> str | None:
|
2020-07-11 01:20:50 +00:00
|
|
|
"""Get entity name."""
|
2021-02-20 18:03:40 +00:00
|
|
|
if self._sub_device:
|
|
|
|
sub_device_name = self._sub_device.replace("_", " ").title()
|
|
|
|
return f"{self._device.name} {sub_device_name}"
|
2020-07-11 01:20:50 +00:00
|
|
|
return self._device.name
|
|
|
|
|
2021-02-09 08:43:38 +00:00
|
|
|
@property
|
2021-03-01 02:16:30 +00:00
|
|
|
def should_poll(self) -> bool:
|
2021-02-09 08:43:38 +00:00
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
@property
|
2021-03-17 22:34:25 +00:00
|
|
|
def device_info(self) -> dict[str, Any] | None:
|
2020-07-11 01:20:50 +00:00
|
|
|
"""Get a an HA device representing this Bond controlled device."""
|
2021-02-08 23:37:32 +00:00
|
|
|
device_info = {
|
2020-07-12 16:31:53 +00:00
|
|
|
ATTR_NAME: self.name,
|
2021-02-08 23:37:32 +00:00
|
|
|
"manufacturer": self._hub.make,
|
2021-02-20 07:06:43 +00:00
|
|
|
"identifiers": {(DOMAIN, self._hub.bond_id, self._device.device_id)},
|
|
|
|
"suggested_area": self._device.location,
|
2020-07-12 16:31:53 +00:00
|
|
|
"via_device": (DOMAIN, self._hub.bond_id),
|
|
|
|
}
|
2021-02-08 23:37:32 +00:00
|
|
|
if not self._hub.is_bridge:
|
|
|
|
device_info["model"] = self._hub.model
|
|
|
|
device_info["sw_version"] = self._hub.fw_ver
|
|
|
|
else:
|
|
|
|
model_data = []
|
|
|
|
if self._device.branding_profile:
|
|
|
|
model_data.append(self._device.branding_profile)
|
|
|
|
if self._device.template:
|
|
|
|
model_data.append(self._device.template)
|
|
|
|
if model_data:
|
|
|
|
device_info["model"] = " ".join(model_data)
|
|
|
|
|
|
|
|
return device_info
|
2020-07-11 01:20:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self) -> bool:
|
|
|
|
"""Let HA know this entity relies on an assumed state tracked by Bond."""
|
2020-07-28 01:39:23 +00:00
|
|
|
return self._hub.is_bridge and not self._device.trust_state
|
2020-07-23 02:15:27 +00:00
|
|
|
|
2020-07-24 20:14:47 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Report availability of this entity based on last API call results."""
|
|
|
|
return self._available
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def async_update(self) -> None:
|
2020-07-23 02:15:27 +00:00
|
|
|
"""Fetch assumed state of the cover from the hub using API."""
|
2021-02-09 08:43:38 +00:00
|
|
|
await self._async_update_from_api()
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def _async_update_if_bpup_not_alive(self, *_: Any) -> None:
|
2021-02-09 08:43:38 +00:00
|
|
|
"""Fetch via the API if BPUP is not alive."""
|
2021-04-18 20:46:46 +00:00
|
|
|
if (
|
|
|
|
self.hass.is_stopping
|
|
|
|
or self._bpup_subs.alive
|
|
|
|
and self._initialized
|
|
|
|
and self._available
|
|
|
|
):
|
2021-02-09 08:43:38 +00:00
|
|
|
return
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
assert self._update_lock is not None
|
2021-02-09 08:43:38 +00:00
|
|
|
if self._update_lock.locked():
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Updating %s took longer than the scheduled update interval %s",
|
|
|
|
self.entity_id,
|
|
|
|
_FALLBACK_SCAN_INTERVAL,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
async with self._update_lock:
|
|
|
|
await self._async_update_from_api()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def _async_update_from_api(self) -> None:
|
2021-02-09 08:43:38 +00:00
|
|
|
"""Fetch via the API."""
|
2020-07-24 20:14:47 +00:00
|
|
|
try:
|
2021-02-09 08:43:38 +00:00
|
|
|
state: dict = await self._hub.bond.device_state(self._device_id)
|
2020-07-24 20:14:47 +00:00
|
|
|
except (ClientError, AsyncIOTimeoutError, OSError) as error:
|
|
|
|
if self._available:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Entity %s has become unavailable", self.entity_id, exc_info=error
|
|
|
|
)
|
|
|
|
self._available = False
|
|
|
|
else:
|
2021-02-09 08:43:38 +00:00
|
|
|
self._async_state_callback(state)
|
2020-07-23 02:15:27 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
2021-03-01 02:16:30 +00:00
|
|
|
def _apply_state(self, state: dict) -> None:
|
2020-07-23 02:15:27 +00:00
|
|
|
raise NotImplementedError
|
2021-02-09 08:43:38 +00:00
|
|
|
|
|
|
|
@callback
|
2021-03-01 02:16:30 +00:00
|
|
|
def _async_state_callback(self, state: dict) -> None:
|
2021-02-09 08:43:38 +00:00
|
|
|
"""Process a state change."""
|
|
|
|
self._initialized = True
|
|
|
|
if not self._available:
|
|
|
|
_LOGGER.info("Entity %s has come back", self.entity_id)
|
|
|
|
self._available = True
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Device state for %s (%s) is:\n%s", self.name, self.entity_id, state
|
|
|
|
)
|
|
|
|
self._apply_state(state)
|
|
|
|
|
|
|
|
@callback
|
2021-03-01 02:16:30 +00:00
|
|
|
def _async_bpup_callback(self, state: dict) -> None:
|
2021-02-09 08:43:38 +00:00
|
|
|
"""Process a state change from BPUP."""
|
|
|
|
self._async_state_callback(state)
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2021-02-09 08:43:38 +00:00
|
|
|
"""Subscribe to BPUP and start polling."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self._update_lock = Lock()
|
|
|
|
self._bpup_subs.subscribe(self._device_id, self._async_bpup_callback)
|
|
|
|
self.async_on_remove(
|
|
|
|
async_track_time_interval(
|
|
|
|
self.hass, self._async_update_if_bpup_not_alive, _FALLBACK_SCAN_INTERVAL
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Unsubscribe from BPUP data on remove."""
|
|
|
|
await super().async_will_remove_from_hass()
|
|
|
|
self._bpup_subs.unsubscribe(self._device_id, self._async_bpup_callback)
|