Refactor Bond integration to remove duplication (#37740)

* Refactor Bond integration to remove duplication in Entity code and unit tests

* Refactor Bond integration to remove duplication in Entity code and unit tests (PR feedback)
This commit is contained in:
Eugene Prystupa authored and GitHub committed 2020-07-10 20:20:50 -05:00
1 parent 0fbdb47dcf
commit 1e9bc278e0
6 files changed
+85 -160

No files matched your search

+5 -25
View File
@@ -1,15 +1,15 @@
"""Support for Bond covers."""
from typing import Any, Callable, Dict, List, Optional
from typing import Any, Callable, List, Optional
from bond import BOND_DEVICE_TYPE_MOTORIZED_SHADES, Bond
from homeassistant.components.cover import DEVICE_CLASS_SHADE, CoverEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from .const import DOMAIN
from .entity import BondEntity
from .utils import BondDevice, get_bond_devices
@@ -32,13 +32,13 @@ async def async_setup_entry(
async_add_entities(covers, True)
class BondCover(CoverEntity):
class BondCover(BondEntity, CoverEntity):
"""Representation of a Bond cover."""
def __init__(self, bond: Bond, device: BondDevice):
"""Create HA entity representing Bond cover."""
self._bond = bond
self._device = device
super().__init__(bond, device)
self._closed: Optional[bool] = None
@property
@@ -46,26 +46,6 @@ class BondCover(CoverEntity):
"""Get device class."""
return DEVICE_CLASS_SHADE
@property
def unique_id(self) -> Optional[str]:
"""Get unique ID for the entity."""
return self._device.device_id
@property
def name(self) -> Optional[str]:
"""Get entity name."""
return self._device.name
@property
def device_info(self) -> Optional[Dict[str, Any]]:
"""Get a an HA device representing this cover."""
return {ATTR_NAME: self.name, "identifiers": {(DOMAIN, self._device.device_id)}}
@property
def assumed_state(self) -> bool:
"""Let HA know this entity relies on an assumed state tracked by Bond."""
return True
def update(self):
"""Fetch assumed state of the cover from the hub using API."""
state: dict = self._bond.getDeviceState(self._device.device_id)
+38
View File
@@ -0,0 +1,38 @@
"""An abstract class common to all Bond entities."""
from typing import Any, Dict, Optional
from bond import Bond
from homeassistant.components.bond.utils import BondDevice
from homeassistant.const import ATTR_NAME
from .const import DOMAIN
class BondEntity:
"""Generic Bond entity encapsulating common features of any Bond controlled device."""
def __init__(self, bond: Bond, device: BondDevice):
"""Initialize entity with API and device info."""
self._bond = bond
self._device = device
@property
def unique_id(self) -> Optional[str]:
"""Get unique ID for the entity."""
return self._device.device_id
@property
def name(self) -> Optional[str]:
"""Get entity name."""
return self._device.name
@property
def device_info(self) -> Optional[Dict[str, Any]]:
"""Get a an HA device representing this Bond controlled device."""
return {ATTR_NAME: self.name, "identifiers": {(DOMAIN, self._device.device_id)}}
@property
def assumed_state(self) -> bool:
"""Let HA know this entity relies on an assumed state tracked by Bond."""
return True
+4 -25
View File
@@ -1,5 +1,5 @@
"""Support for Bond fans."""
from typing import Any, Callable, Dict, List, Optional
from typing import Any, Callable, List, Optional
from bond import BOND_DEVICE_TYPE_CEILING_FAN, Bond
@@ -15,8 +15,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from ...const import ATTR_NAME
from .const import DOMAIN
from .entity import BondEntity
from .utils import BondDevice, get_bond_devices
@@ -39,37 +39,16 @@ async def async_setup_entry(
async_add_entities(fans, True)
class BondFan(FanEntity):
class BondFan(BondEntity, FanEntity):
"""Representation of a Bond fan."""
def __init__(self, bond: Bond, device: BondDevice):
"""Create HA entity representing Bond fan."""
self._bond = bond
self._device = device
super().__init__(bond, device)
self._power: Optional[bool] = None
self._speed: Optional[int] = None
@property
def unique_id(self) -> Optional[str]:
"""Get unique ID for the entity."""
return self._device.device_id
@property
def name(self) -> Optional[str]:
"""Get entity name."""
return self._device.name
@property
def device_info(self) -> Optional[Dict[str, Any]]:
"""Get a an HA device representing this fan."""
return {ATTR_NAME: self.name, "identifiers": {(DOMAIN, self._device.device_id)}}
@property
def assumed_state(self) -> bool:
"""Let HA know this entity relies on an assumed state tracked by Bond."""
return True
@property
def supported_features(self) -> int:
"""Flag supported features."""