From 02e2c40c48b9ea6399c4b392ccf12dd5e99c3960 Mon Sep 17 00:00:00 2001 From: Marcio Granzotto Rodrigues Date: Mon, 27 Jul 2020 22:39:23 -0300 Subject: [PATCH] Bond - Make assumed state conditional (#38209) --- homeassistant/components/bond/entity.py | 2 +- homeassistant/components/bond/utils.py | 10 ++++++ tests/components/bond/common.py | 3 +- tests/components/bond/test_light.py | 48 ++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/bond/entity.py b/homeassistant/components/bond/entity.py index 8c3b9c638f5..6743bc979ec 100644 --- a/homeassistant/components/bond/entity.py +++ b/homeassistant/components/bond/entity.py @@ -46,7 +46,7 @@ class BondEntity(Entity): @property def assumed_state(self) -> bool: """Let HA know this entity relies on an assumed state tracked by Bond.""" - return True + return self._hub.is_bridge and not self._device.trust_state @property def available(self) -> bool: diff --git a/homeassistant/components/bond/utils.py b/homeassistant/components/bond/utils.py index 545360b25fd..a4df306b429 100644 --- a/homeassistant/components/bond/utils.py +++ b/homeassistant/components/bond/utils.py @@ -24,6 +24,11 @@ class BondDevice: """Get the type of this device.""" return self._attrs["type"] + @property + def trust_state(self) -> bool: + """Check if Trust State is turned on.""" + return self.props.get("trust_state", False) + def supports_speed(self) -> bool: """Return True if this device supports any of the speed related commands.""" actions: List[str] = self._attrs["actions"] @@ -89,3 +94,8 @@ class BondHub: def devices(self) -> List[BondDevice]: """Return a list of all devices controlled by this hub.""" return self._devices + + @property + def is_bridge(self) -> bool: + """Return if the Bond is a Bond Bridge. If False, it means that it is a Smart by Bond product. Assumes that it is if the model is not available.""" + return self._version.get("model", "BD-").startswith("BD-") diff --git a/tests/components/bond/common.py b/tests/components/bond/common.py index 1a37455b338..181fe3eaf07 100644 --- a/tests/components/bond/common.py +++ b/tests/components/bond/common.py @@ -51,6 +51,7 @@ async def setup_platform( discovered_device: Dict[str, Any], bond_device_id: str = "bond-device-id", props: Dict[str, Any] = None, + bond_version: Dict[str, Any] = None, ): """Set up the specified Bond platform.""" mock_entry = MockConfigEntry( @@ -60,7 +61,7 @@ async def setup_platform( mock_entry.add_to_hass(hass) with patch("homeassistant.components.bond.PLATFORMS", [platform]): - with patch_bond_version(), patch_bond_device_ids( + with patch_bond_version(return_value=bond_version), patch_bond_device_ids( return_value=[bond_device_id] ), patch_bond_device( return_value=discovered_device diff --git a/tests/components/bond/test_light.py b/tests/components/bond/test_light.py index b507395dab3..555da5e707f 100644 --- a/tests/components/bond/test_light.py +++ b/tests/components/bond/test_light.py @@ -6,7 +6,12 @@ from bond_api import Action, DeviceType from homeassistant import core from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.const import ( + ATTR_ASSUMED_STATE, + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow @@ -44,6 +49,47 @@ async def test_entity_registry(hass: core.HomeAssistant): assert [key for key in registry.entities] == ["light.name_1"] +async def test_sbb_trust_state(hass: core.HomeAssistant): + """Assumed state should be False if device is a Smart by Bond.""" + version = { + "model": "MR123A", + "bondid": "test-bond-id", + } + await setup_platform( + hass, LIGHT_DOMAIN, ceiling_fan("name-1"), bond_version=version + ) + + device = hass.states.get("light.name_1") + assert device.attributes.get(ATTR_ASSUMED_STATE) is not True + + +async def test_trust_state_not_specified(hass: core.HomeAssistant): + """Assumed state should be True if Trust State is not specified.""" + await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1")) + + device = hass.states.get("light.name_1") + assert device.attributes.get(ATTR_ASSUMED_STATE) is True + + +async def test_trust_state(hass: core.HomeAssistant): + """Assumed state should be True if Trust State is False.""" + await setup_platform( + hass, LIGHT_DOMAIN, ceiling_fan("name-1"), props={"trust_state": False} + ) + + device = hass.states.get("light.name_1") + assert device.attributes.get(ATTR_ASSUMED_STATE) is True + + +async def test_no_trust_state(hass: core.HomeAssistant): + """Assumed state should be False if Trust State is True.""" + await setup_platform( + hass, LIGHT_DOMAIN, ceiling_fan("name-1"), props={"trust_state": True} + ) + device = hass.states.get("light.name_1") + assert device.attributes.get(ATTR_ASSUMED_STATE) is not True + + async def test_turn_on_light(hass: core.HomeAssistant): """Tests that turn on command delegates to API.""" await setup_platform(