2020-07-06 01:17:53 +00:00
|
|
|
"""Reusable utilities for the Bond component."""
|
2021-02-20 07:06:43 +00:00
|
|
|
import asyncio
|
2020-07-28 02:53:56 +00:00
|
|
|
import logging
|
2020-07-12 16:31:53 +00:00
|
|
|
from typing import List, Optional
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-02-20 07:06:43 +00:00
|
|
|
from aiohttp import ClientResponseError
|
2020-07-23 01:22:25 +00:00
|
|
|
from bond_api import Action, Bond
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-02-08 23:37:32 +00:00
|
|
|
from .const import BRIDGE_MAKE
|
|
|
|
|
2020-07-28 02:53:56 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
class BondDevice:
|
|
|
|
"""Helper device class to hold ID and attributes together."""
|
|
|
|
|
2020-07-13 23:00:05 +00:00
|
|
|
def __init__(self, device_id: str, attrs: dict, props: dict):
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Create a helper device from ID and attributes returned by API."""
|
|
|
|
self.device_id = device_id
|
2020-07-13 23:00:05 +00:00
|
|
|
self.props = props
|
2020-07-06 01:17:53 +00:00
|
|
|
self._attrs = attrs
|
|
|
|
|
2020-07-28 02:53:56 +00:00
|
|
|
def __repr__(self):
|
|
|
|
"""Return readable representation of a bond device."""
|
|
|
|
return {
|
|
|
|
"device_id": self.device_id,
|
|
|
|
"props": self.props,
|
|
|
|
"attrs": self._attrs,
|
|
|
|
}.__repr__()
|
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
@property
|
2020-07-11 00:23:35 +00:00
|
|
|
def name(self) -> str:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Get the name of this device."""
|
|
|
|
return self._attrs["name"]
|
|
|
|
|
|
|
|
@property
|
2020-07-11 00:23:35 +00:00
|
|
|
def type(self) -> str:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Get the type of this device."""
|
|
|
|
return self._attrs["type"]
|
|
|
|
|
2021-02-08 23:37:32 +00:00
|
|
|
@property
|
|
|
|
def location(self) -> str:
|
|
|
|
"""Get the location of this device."""
|
2021-02-20 07:06:43 +00:00
|
|
|
return self._attrs.get("location")
|
2021-02-08 23:37:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def template(self) -> str:
|
|
|
|
"""Return this model template."""
|
|
|
|
return self._attrs.get("template")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def branding_profile(self) -> str:
|
|
|
|
"""Return this branding profile."""
|
|
|
|
return self.props.get("branding_profile")
|
|
|
|
|
2020-07-28 01:39:23 +00:00
|
|
|
@property
|
|
|
|
def trust_state(self) -> bool:
|
|
|
|
"""Check if Trust State is turned on."""
|
|
|
|
return self.props.get("trust_state", False)
|
|
|
|
|
2020-07-12 20:30:24 +00:00
|
|
|
def supports_speed(self) -> bool:
|
|
|
|
"""Return True if this device supports any of the speed related commands."""
|
2020-07-11 00:23:35 +00:00
|
|
|
actions: List[str] = self._attrs["actions"]
|
2020-07-23 01:22:25 +00:00
|
|
|
return bool([action for action in actions if action in [Action.SET_SPEED]])
|
2020-07-12 20:30:24 +00:00
|
|
|
|
|
|
|
def supports_direction(self) -> bool:
|
|
|
|
"""Return True if this device supports any of the direction related commands."""
|
|
|
|
actions: List[str] = self._attrs["actions"]
|
2020-07-23 01:22:25 +00:00
|
|
|
return bool([action for action in actions if action in [Action.SET_DIRECTION]])
|
2020-07-11 00:23:35 +00:00
|
|
|
|
2020-07-12 23:45:47 +00:00
|
|
|
def supports_light(self) -> bool:
|
|
|
|
"""Return True if this device supports any of the light related commands."""
|
|
|
|
actions: List[str] = self._attrs["actions"]
|
2020-07-13 21:07:35 +00:00
|
|
|
return bool(
|
|
|
|
[
|
|
|
|
action
|
|
|
|
for action in actions
|
2020-07-23 01:22:25 +00:00
|
|
|
if action in [Action.TURN_LIGHT_ON, Action.TURN_LIGHT_OFF]
|
2020-07-13 21:07:35 +00:00
|
|
|
]
|
2020-07-12 23:45:47 +00:00
|
|
|
)
|
|
|
|
|
2020-07-30 01:01:59 +00:00
|
|
|
def supports_set_brightness(self) -> bool:
|
|
|
|
"""Return True if this device supports setting a light brightness."""
|
|
|
|
actions: List[str] = self._attrs["actions"]
|
|
|
|
return bool([action for action in actions if action in [Action.SET_BRIGHTNESS]])
|
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-12 16:31:53 +00:00
|
|
|
class BondHub:
|
|
|
|
"""Hub device representing Bond Bridge."""
|
|
|
|
|
|
|
|
def __init__(self, bond: Bond):
|
|
|
|
"""Initialize Bond Hub."""
|
|
|
|
self.bond: Bond = bond
|
2021-02-20 07:06:43 +00:00
|
|
|
self._bridge: Optional[dict] = None
|
2020-07-12 16:31:53 +00:00
|
|
|
self._version: Optional[dict] = None
|
2020-07-16 15:31:15 +00:00
|
|
|
self._devices: Optional[List[BondDevice]] = None
|
2020-07-12 16:31:53 +00:00
|
|
|
|
2021-02-20 07:06:43 +00:00
|
|
|
async def setup(self, max_devices=None):
|
2020-07-12 16:31:53 +00:00
|
|
|
"""Read hub version information."""
|
2020-07-23 01:22:25 +00:00
|
|
|
self._version = await self.bond.version()
|
2020-08-03 15:55:04 +00:00
|
|
|
_LOGGER.debug("Bond reported the following version info: %s", self._version)
|
2020-07-16 15:31:15 +00:00
|
|
|
# Fetch all available devices using Bond API.
|
2020-07-23 01:22:25 +00:00
|
|
|
device_ids = await self.bond.devices()
|
2021-02-20 07:06:43 +00:00
|
|
|
self._devices = []
|
|
|
|
for idx, device_id in enumerate(device_ids):
|
|
|
|
if max_devices is not None and idx >= max_devices:
|
|
|
|
break
|
|
|
|
|
|
|
|
device, props = await asyncio.gather(
|
|
|
|
self.bond.device(device_id), self.bond.device_properties(device_id)
|
2020-07-13 23:00:05 +00:00
|
|
|
)
|
2021-02-20 07:06:43 +00:00
|
|
|
|
|
|
|
self._devices.append(BondDevice(device_id, device, props))
|
2020-07-12 16:31:53 +00:00
|
|
|
|
2020-07-28 02:53:56 +00:00
|
|
|
_LOGGER.debug("Discovered Bond devices: %s", self._devices)
|
2021-02-20 07:06:43 +00:00
|
|
|
try:
|
|
|
|
# Smart by bond devices do not have a bridge api call
|
|
|
|
self._bridge = await self.bond.bridge()
|
|
|
|
except ClientResponseError:
|
|
|
|
self._bridge = {}
|
|
|
|
_LOGGER.debug("Bond reported the following bridge info: %s", self._bridge)
|
2020-07-28 02:53:56 +00:00
|
|
|
|
2020-07-12 16:31:53 +00:00
|
|
|
@property
|
2021-02-20 07:06:43 +00:00
|
|
|
def bond_id(self) -> Optional[str]:
|
2020-07-12 16:31:53 +00:00
|
|
|
"""Return unique Bond ID for this hub."""
|
2021-02-20 07:06:43 +00:00
|
|
|
# Old firmwares are missing the bondid
|
|
|
|
return self._version.get("bondid")
|
2020-07-12 16:31:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def target(self) -> str:
|
2021-02-08 23:37:32 +00:00
|
|
|
"""Return this hub target."""
|
2020-07-12 16:31:53 +00:00
|
|
|
return self._version.get("target")
|
|
|
|
|
2021-02-08 23:37:32 +00:00
|
|
|
@property
|
|
|
|
def model(self) -> str:
|
|
|
|
"""Return this hub model."""
|
|
|
|
return self._version.get("model")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def make(self) -> str:
|
|
|
|
"""Return this hub make."""
|
|
|
|
return self._version.get("make", BRIDGE_MAKE)
|
|
|
|
|
2021-02-20 07:06:43 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> Optional[str]:
|
|
|
|
"""Get the name of this bridge."""
|
|
|
|
if not self.is_bridge and self._devices:
|
|
|
|
return self._devices[0].name
|
|
|
|
return self._bridge.get("name")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self) -> Optional[str]:
|
|
|
|
"""Get the location of this bridge."""
|
|
|
|
if not self.is_bridge and self._devices:
|
|
|
|
return self._devices[0].location
|
|
|
|
return self._bridge.get("location")
|
|
|
|
|
2020-07-12 16:31:53 +00:00
|
|
|
@property
|
|
|
|
def fw_ver(self) -> str:
|
|
|
|
"""Return this hub firmware version."""
|
|
|
|
return self._version.get("fw_ver")
|
2020-07-16 15:31:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def devices(self) -> List[BondDevice]:
|
|
|
|
"""Return a list of all devices controlled by this hub."""
|
|
|
|
return self._devices
|
2020-07-28 01:39:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_bridge(self) -> bool:
|
2020-07-28 02:53:56 +00:00
|
|
|
"""Return if the Bond is a Bond Bridge."""
|
2021-02-20 07:06:43 +00:00
|
|
|
return bool(self._bridge)
|