From 6366872119c4f20acf06fe679a231a9768ac06a2 Mon Sep 17 00:00:00 2001 From: Eugene Prystupa Date: Sun, 18 Oct 2020 15:11:24 -0400 Subject: [PATCH] Add support for Bond fireplaces with integrated lights (#41255) --- homeassistant/components/bond/entity.py | 8 +++- homeassistant/components/bond/light.py | 16 +++++-- tests/components/bond/test_light.py | 60 ++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/bond/entity.py b/homeassistant/components/bond/entity.py index 716f121f1a1..501d6574960 100644 --- a/homeassistant/components/bond/entity.py +++ b/homeassistant/components/bond/entity.py @@ -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]: diff --git a/homeassistant/components/bond/light.py b/homeassistant/components/bond/light.py index 5e66019579d..82187bf6fab 100644 --- a/homeassistant/components/bond/light.py +++ b/homeassistant/components/bond/light.py @@ -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 diff --git a/tests/components/bond/test_light.py b/tests/components/bond/test_light.py index 70b585a76da..6d871187e26 100644 --- a/tests/components/bond/test_light.py +++ b/tests/components/bond/test_light.py @@ -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 = {