Refactor bond integration to be completely async (#38066)

This commit is contained in:
Eugene Prystupa 2020-07-22 18:22:25 -07:00 committed by GitHub
parent 15fe727f2c
commit 3480fb6996
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 251 additions and 259 deletions

View file

@ -2,7 +2,7 @@
from typing import List, Optional
from bond import Actions, Bond
from bond_api import Action, Bond
class BondDevice:
@ -27,18 +27,12 @@ class BondDevice:
def supports_speed(self) -> bool:
"""Return True if this device supports any of the speed related commands."""
actions: List[str] = self._attrs["actions"]
return bool([action for action in actions if action in [Actions.SET_SPEED]])
return bool([action for action in actions if action in [Action.SET_SPEED]])
def supports_direction(self) -> bool:
"""Return True if this device supports any of the direction related commands."""
actions: List[str] = self._attrs["actions"]
return bool(
[
action
for action in actions
if action in [Actions.SET_DIRECTION, Actions.TOGGLE_DIRECTION]
]
)
return bool([action for action in actions if action in [Action.SET_DIRECTION]])
def supports_light(self) -> bool:
"""Return True if this device supports any of the light related commands."""
@ -47,7 +41,7 @@ class BondDevice:
[
action
for action in actions
if action in [Actions.TURN_LIGHT_ON, Actions.TOGGLE_LIGHT]
if action in [Action.TURN_LIGHT_ON, Action.TURN_LIGHT_OFF]
]
)
@ -61,17 +55,17 @@ class BondHub:
self._version: Optional[dict] = None
self._devices: Optional[List[BondDevice]] = None
def setup(self):
async def setup(self):
"""Read hub version information."""
self._version = self.bond.getVersion()
self._version = await self.bond.version()
# Fetch all available devices using Bond API.
device_ids = self.bond.getDeviceIds()
device_ids = await self.bond.devices()
self._devices = [
BondDevice(
device_id,
self.bond.getDevice(device_id),
self.bond.getProperties(device_id),
await self.bond.device(device_id),
await self.bond.device_properties(device_id),
)
for device_id in device_ids
]