1 """Reusable utilities for the Bond component."""
3 from __future__
import annotations
6 from typing
import Any, cast
8 from aiohttp
import ClientResponseError
9 from bond_async
import Action, Bond, BondType
13 from .const
import BRIDGE_MAKE
17 _LOGGER = logging.getLogger(__name__)
21 """Helper device class to hold ID and attributes together."""
26 attrs: dict[str, Any],
27 props: dict[str, Any],
28 state: dict[str, Any],
30 """Create a helper device from ID and attributes returned by API."""
35 self._supported_actions: set[str] = set(self.
_attrs_attrs.
get(
"actions", []))
38 """Return readable representation of a bond device."""
41 "props": self.
propsprops,
42 "attrs": self.
_attrs_attrs,
43 "state": self.
statestate,
48 """Get the name of this device."""
49 return cast(str, self.
_attrs_attrs[
"name"])
53 """Get the type of this device."""
54 return cast(str, self.
_attrs_attrs[
"type"])
58 """Get the location of this device."""
63 """Return this model template."""
68 """Return this branding profile."""
69 return self.
propsprops.
get(
"branding_profile")
73 """Check if Trust State is turned on."""
74 return self.
propsprops.
get(
"trust_state",
False)
77 """Check to see if the device supports an actions."""
78 return action
in self._supported_actions
81 """Check to see if the device supports any of the actions."""
82 return bool(self._supported_actions.intersection(actions))
85 """Return True if this device supports any of the speed related commands."""
89 """Return True if this device supports any of the direction related commands."""
93 """Return True if this device supports setting the position."""
97 """Return True if this device supports opening."""
101 """Return True if this device supports closing."""
105 """Return True if this device supports tilt opening."""
109 """Return True if this device supports tilt closing."""
113 """Return True if this device supports hold aka stop."""
117 """Return True if this device supports any of the light related commands."""
118 return self.
_has_any_action_has_any_action({Action.TURN_LIGHT_ON, Action.TURN_LIGHT_OFF})
121 """Return true if the device has an up light."""
122 return self.
_has_any_action_has_any_action({Action.TURN_UP_LIGHT_ON, Action.TURN_UP_LIGHT_OFF})
125 """Return true if the device has a down light."""
127 {Action.TURN_DOWN_LIGHT_ON, Action.TURN_DOWN_LIGHT_OFF}
131 """Return True if this device supports setting a light brightness."""
136 """Hub device representing Bond Bridge."""
139 """Initialize Bond Hub."""
140 self.bond: Bond = bond
142 self.
_bridge_bridge: dict[str, Any] = {}
143 self.
_version_version: dict[str, Any] = {}
144 self.
_devices_devices: list[BondDevice] = []
146 async
def setup(self, max_devices: int |
None =
None) ->
None:
147 """Read hub version information."""
149 _LOGGER.debug(
"Bond reported the following version info: %s", self.
_version_version)
151 device_ids = await self.bond.
devices()
153 setup_device_ids = []
155 for idx, device_id
in enumerate(device_ids):
156 if max_devices
is not None and idx >= max_devices:
158 setup_device_ids.append(device_id)
161 self.bond.
device(device_id),
162 self.bond.device_properties(device_id),
163 self.bond.device_state(device_id),
169 for device_id
in setup_device_ids:
173 responses[response_idx],
174 responses[response_idx + 1],
175 responses[response_idx + 2],
180 _LOGGER.debug(
"Discovered Bond devices: %s", self.
_devices_devices)
183 self.
_bridge_bridge = await self.bond.bridge()
184 except ClientResponseError:
186 _LOGGER.debug(
"Bond reported the following bridge info: %s", self.
_bridge_bridge)
190 """Return unique Bond ID for this hub."""
196 """Return this hub target."""
201 """Return this hub model."""
206 """Return this hub make."""
207 return self.
_version_version.
get(
"make", BRIDGE_MAKE)
211 """Get the name of this bridge."""
213 return self.
_devices_devices[0].name
214 return cast(str, self.
_bridge_bridge[
"name"])
218 """Get the location of this bridge."""
220 return self.
_devices_devices[0].location
225 """Return this hub firmware version."""
230 """Return this hub hardware version."""
235 """Return a list of all devices controlled by this hub."""
240 """Return if the Bond is a Bond Bridge."""
241 bondid = self.
_version_version[
"bondid"]
242 return bool(BondType.is_bridge_from_serial(bondid))
bool supports_close(self)
bool supports_up_light(self)
bool supports_down_light(self)
bool _has_any_action(self, set[str] actions)
None __init__(self, str device_id, dict[str, Any] attrs, dict[str, Any] props, dict[str, Any] state)
str|None branding_profile(self)
bool has_action(self, str action)
bool supports_direction(self)
bool supports_set_brightness(self)
bool supports_tilt_open(self)
bool supports_tilt_close(self)
bool supports_set_position(self)
bool supports_speed(self)
bool supports_light(self)
list[BondDevice] devices(self)
None setup(self, int|None max_devices=None)
None __init__(self, Bond bond, str host)
web.Response get(self, web.Request request, str config_key)
Any gather_with_limited_concurrency(int limit, *Any tasks, bool return_exceptions=False)