Add support for Bond fireplaces with integrated lights (#41255)

This commit is contained in:
Eugene Prystupa 2020-10-18 15:11:24 -04:00 committed by GitHub
parent e5eae22ad8
commit 6366872119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 11 deletions

View File

@ -18,10 +18,13 @@ _LOGGER = logging.getLogger(__name__)
class BondEntity(Entity):
"""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, sub_device: Optional[str] = None
):
"""Initialize entity with API and device info."""
self._hub = hub
self._device = device
self._sub_device = sub_device
self._available = True
@property
@ -29,7 +32,8 @@ class BondEntity(Entity):
"""Get unique ID for the entity."""
hub_id = self._hub.bond_id
device_id = self._device.device_id
return f"{hub_id}_{device_id}"
sub_device_id: str = f"_{self._sub_device}" if self._sub_device else ""
return f"{hub_id}_{device_id}{sub_device_id}"
@property
def name(self) -> Optional[str]:

View File

@ -29,7 +29,7 @@ async def async_setup_entry(
"""Set up Bond light devices."""
hub: BondHub = hass.data[DOMAIN][entry.entry_id]
lights: List[Entity] = [
fan_lights: List[Entity] = [
BondLight(hub, device)
for device in hub.devices
if DeviceType.is_fan(device.type) and device.supports_light()
@ -41,15 +41,23 @@ async def async_setup_entry(
if DeviceType.is_fireplace(device.type)
]
async_add_entities(lights + fireplaces, True)
fp_lights: List[Entity] = [
BondLight(hub, device, "light")
for device in hub.devices
if DeviceType.is_fireplace(device.type) and device.supports_light()
]
async_add_entities(fan_lights + fireplaces + fp_lights, True)
class BondLight(BondEntity, LightEntity):
"""Representation of a Bond light."""
def __init__(self, hub: BondHub, device: BondDevice):
def __init__(
self, hub: BondHub, device: BondDevice, sub_device: Optional[str] = None
):
"""Create HA entity representing Bond fan."""
super().__init__(hub, device)
super().__init__(hub, device, sub_device)
self._brightness: Optional[int] = None
self._light: Optional[int] = None

View File

@ -49,24 +49,74 @@ def dimmable_ceiling_fan(name: str):
def fireplace(name: str):
"""Create a fireplace with given name."""
return {"name": name, "type": DeviceType.FIREPLACE}
return {
"name": name,
"type": DeviceType.FIREPLACE,
"actions": [Action.TURN_ON, Action.TURN_OFF],
}
async def test_entity_registry(hass: core.HomeAssistant):
"""Tests that the devices are registered in the entity registry."""
def fireplace_with_light(name: str):
"""Create a fireplace with given name."""
return {
"name": name,
"type": DeviceType.FIREPLACE,
"actions": [
Action.TURN_ON,
Action.TURN_OFF,
Action.TURN_LIGHT_ON,
Action.TURN_LIGHT_OFF,
],
}
async def test_fan_entity_registry(hass: core.HomeAssistant):
"""Tests that fan with light devices are registered in the entity registry."""
await setup_platform(
hass,
LIGHT_DOMAIN,
ceiling_fan("name-1"),
ceiling_fan("fan-name"),
bond_version={"bondid": "test-hub-id"},
bond_device_id="test-device-id",
)
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
entity = registry.entities["light.name_1"]
entity = registry.entities["light.fan_name"]
assert entity.unique_id == "test-hub-id_test-device-id"
async def test_fireplace_entity_registry(hass: core.HomeAssistant):
"""Tests that flame fireplace devices are registered in the entity registry."""
await setup_platform(
hass,
LIGHT_DOMAIN,
fireplace("fireplace-name"),
bond_version={"bondid": "test-hub-id"},
bond_device_id="test-device-id",
)
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
entity = registry.entities["light.fireplace_name"]
assert entity.unique_id == "test-hub-id_test-device-id"
async def test_fireplace_with_light_entity_registry(hass: core.HomeAssistant):
"""Tests that flame+light devices are registered in the entity registry."""
await setup_platform(
hass,
LIGHT_DOMAIN,
fireplace_with_light("fireplace-name"),
bond_version={"bondid": "test-hub-id"},
bond_device_id="test-device-id",
)
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
entity_flame = registry.entities["light.fireplace_name"]
assert entity_flame.unique_id == "test-hub-id_test-device-id"
entity_light = registry.entities["light.fireplace_name_2"]
assert entity_light.unique_id == "test-hub-id_test-device-id_light"
async def test_sbb_trust_state(hass: core.HomeAssistant):
"""Assumed state should be False if device is a Smart by Bond."""
version = {