Centralize bond update state logic (#38093)

* Refactor bond integration to centralize update state logic in single superclass

* Refactor bond integration to centralize update state logic in single superclass
This commit is contained in:
Eugene Prystupa 2020-07-22 19:15:27 -07:00 committed by GitHub
parent 3480fb6996
commit a756d1e637
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 30 deletions

View File

@ -39,17 +39,15 @@ class BondCover(BondEntity, CoverEntity):
self._closed: Optional[bool] = None 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 @property
def device_class(self) -> Optional[str]: def device_class(self) -> Optional[str]:
"""Get device class.""" """Get device class."""
return DEVICE_CLASS_SHADE 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 @property
def is_closed(self): def is_closed(self):
"""Return if the cover is closed or not.""" """Return if the cover is closed or not."""

View File

@ -1,13 +1,15 @@
"""An abstract class common to all Bond entities.""" """An abstract class common to all Bond entities."""
from abc import abstractmethod
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from homeassistant.const import ATTR_NAME from homeassistant.const import ATTR_NAME
from homeassistant.helpers.entity import Entity
from .const import DOMAIN from .const import DOMAIN
from .utils import BondDevice, BondHub from .utils import BondDevice, BondHub
class BondEntity: class BondEntity(Entity):
"""Generic Bond entity encapsulating common features of any Bond controlled device.""" """Generic Bond entity encapsulating common features of any Bond controlled device."""
def __init__(self, hub: BondHub, device: BondDevice): def __init__(self, hub: BondHub, device: BondDevice):
@ -38,3 +40,12 @@ class BondEntity:
def assumed_state(self) -> bool: def assumed_state(self) -> bool:
"""Let HA know this entity relies on an assumed state tracked by Bond.""" """Let HA know this entity relies on an assumed state tracked by Bond."""
return True 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

View File

@ -50,6 +50,11 @@ class BondFan(BondEntity, FanEntity):
self._speed: Optional[int] = None self._speed: Optional[int] = None
self._direction: 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 @property
def supported_features(self) -> int: def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
@ -90,13 +95,6 @@ class BondFan(BondEntity, FanEntity):
return direction 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: async def async_set_speed(self, speed: str) -> None:
"""Set the desired speed for the fan.""" """Set the desired speed for the fan."""
max_speed = self._device.props.get("max_speed", 3) max_speed = self._device.props.get("max_speed", 3)

View File

@ -50,16 +50,14 @@ class BondLight(BondEntity, LightEntity):
self._light: Optional[int] = None self._light: Optional[int] = None
def _apply_state(self, state: dict):
self._light = state.get("light")
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return if light is currently on.""" """Return if light is currently on."""
return self._light == 1 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: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light.""" """Turn on the light."""
await self._hub.bond.action(self._device.device_id, Action.turn_light_on()) await self._hub.bond.action(self._device.device_id, Action.turn_light_on())
@ -80,6 +78,10 @@ class BondFireplace(BondEntity, LightEntity):
# Bond flame level, 0-100 # Bond flame level, 0-100
self._flame: Optional[int] = None self._flame: Optional[int] = None
def _apply_state(self, state: dict):
self._power = state.get("power")
self._flame = state.get("flame")
@property @property
def supported_features(self) -> Optional[int]: def supported_features(self) -> Optional[int]:
"""Flag brightness as supported feature to represent flame level.""" """Flag brightness as supported feature to represent flame level."""
@ -112,9 +114,3 @@ class BondFireplace(BondEntity, LightEntity):
def icon(self) -> Optional[str]: def icon(self) -> Optional[str]:
"""Show fireplace icon for the entity.""" """Show fireplace icon for the entity."""
return "mdi:fireplace" if self._power == 1 else "mdi:fireplace-off" 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")

View File

@ -39,6 +39,9 @@ class BondSwitch(BondEntity, SwitchEntity):
self._power: Optional[bool] = None self._power: Optional[bool] = None
def _apply_state(self, state: dict):
self._power = state.get("power")
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return True if power is on.""" """Return True if power is on."""
@ -51,8 +54,3 @@ class BondSwitch(BondEntity, SwitchEntity):
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
await self._hub.bond.action(self._device.device_id, Action.turn_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")