Skip to content

Commit

Permalink
Centralize bond update state logic (#38093)
Browse files Browse the repository at this point in the history
* Refactor bond integration to centralize update state logic in single superclass

* Refactor bond integration to centralize update state logic in single superclass
  • Loading branch information
prystupa committed Jul 23, 2020
1 parent 3480fb6 commit a756d1e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
10 changes: 4 additions & 6 deletions homeassistant/components/bond/cover.py
Expand Up @@ -39,17 +39,15 @@ def __init__(self, hub: BondHub, device: BondDevice):

self._closed: Optional[bool] = None

def _apply_state(self, state: dict):
cover_open = state.get("open")
self._closed = True if cover_open == 0 else False if cover_open == 1 else None

@property
def device_class(self) -> Optional[str]:
"""Get device class."""
return DEVICE_CLASS_SHADE

async def async_update(self):
"""Fetch assumed state of the cover from the hub using API."""
state: dict = await self._hub.bond.device_state(self._device.device_id)
cover_open = state.get("open")
self._closed = True if cover_open == 0 else False if cover_open == 1 else None

@property
def is_closed(self):
"""Return if the cover is closed or not."""
Expand Down
13 changes: 12 additions & 1 deletion homeassistant/components/bond/entity.py
@@ -1,13 +1,15 @@
"""An abstract class common to all Bond entities."""
from abc import abstractmethod
from typing import Any, Dict, Optional

from homeassistant.const import ATTR_NAME
from homeassistant.helpers.entity import Entity

from .const import DOMAIN
from .utils import BondDevice, BondHub


class BondEntity:
class BondEntity(Entity):
"""Generic Bond entity encapsulating common features of any Bond controlled device."""

def __init__(self, hub: BondHub, device: BondDevice):
Expand Down Expand Up @@ -38,3 +40,12 @@ def device_info(self) -> Optional[Dict[str, Any]]:
def assumed_state(self) -> bool:
"""Let HA know this entity relies on an assumed state tracked by Bond."""
return True

async def async_update(self):
"""Fetch assumed state of the cover from the hub using API."""
state: dict = await self._hub.bond.device_state(self._device.device_id)
self._apply_state(state)

@abstractmethod
def _apply_state(self, state: dict):
raise NotImplementedError
12 changes: 5 additions & 7 deletions homeassistant/components/bond/fan.py
Expand Up @@ -50,6 +50,11 @@ def __init__(self, hub: BondHub, device: BondDevice):
self._speed: Optional[int] = None
self._direction: Optional[int] = None

def _apply_state(self, state: dict):
self._power = state.get("power")
self._speed = state.get("speed")
self._direction = state.get("direction")

@property
def supported_features(self) -> int:
"""Flag supported features."""
Expand Down Expand Up @@ -90,13 +95,6 @@ def current_direction(self) -> Optional[str]:

return direction

async def async_update(self):
"""Fetch assumed state of the fan from the hub using API."""
state: dict = await self._hub.bond.device_state(self._device.device_id)
self._power = state.get("power")
self._speed = state.get("speed")
self._direction = state.get("direction")

async def async_set_speed(self, speed: str) -> None:
"""Set the desired speed for the fan."""
max_speed = self._device.props.get("max_speed", 3)
Expand Down
18 changes: 7 additions & 11 deletions homeassistant/components/bond/light.py
Expand Up @@ -50,16 +50,14 @@ def __init__(self, hub: BondHub, device: BondDevice):

self._light: Optional[int] = None

def _apply_state(self, state: dict):
self._light = state.get("light")

@property
def is_on(self) -> bool:
"""Return if light is currently on."""
return self._light == 1

async def async_update(self):
"""Fetch assumed state of the light from the hub using API."""
state: dict = await self._hub.bond.device_state(self._device.device_id)
self._light = state.get("light")

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
await self._hub.bond.action(self._device.device_id, Action.turn_light_on())
Expand All @@ -80,6 +78,10 @@ def __init__(self, hub: BondHub, device: BondDevice):
# Bond flame level, 0-100
self._flame: Optional[int] = None

def _apply_state(self, state: dict):
self._power = state.get("power")
self._flame = state.get("flame")

@property
def supported_features(self) -> Optional[int]:
"""Flag brightness as supported feature to represent flame level."""
Expand Down Expand Up @@ -112,9 +114,3 @@ def brightness(self):
def icon(self) -> Optional[str]:
"""Show fireplace icon for the entity."""
return "mdi:fireplace" if self._power == 1 else "mdi:fireplace-off"

async def async_update(self):
"""Fetch assumed state of the device from the hub using API."""
state: dict = await self._hub.bond.device_state(self._device.device_id)
self._power = state.get("power")
self._flame = state.get("flame")
8 changes: 3 additions & 5 deletions homeassistant/components/bond/switch.py
Expand Up @@ -39,6 +39,9 @@ def __init__(self, hub: BondHub, device: BondDevice):

self._power: Optional[bool] = None

def _apply_state(self, state: dict):
self._power = state.get("power")

@property
def is_on(self) -> bool:
"""Return True if power is on."""
Expand All @@ -51,8 +54,3 @@ async def async_turn_on(self, **kwargs: Any) -> None:
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
await self._hub.bond.action(self._device.device_id, Action.turn_off())

async def async_update(self):
"""Fetch assumed state of the device from the hub using API."""
state: dict = await self._hub.bond.device_state(self._device.device_id)
self._power = state.get("power")

0 comments on commit a756d1e

Please sign in to comment.