From 92582beeff7a5d1e9fa6cfae3afa41f596b5e3c2 Mon Sep 17 00:00:00 2001 From: Eric Severance Date: Mon, 23 May 2022 10:28:16 -0700 Subject: [PATCH] Use properties of wemo Maker device (#72378) --- homeassistant/components/wemo/switch.py | 6 ++--- tests/components/wemo/conftest.py | 6 +++++ tests/components/wemo/test_binary_sensor.py | 13 ----------- tests/components/wemo/test_switch.py | 25 +++++++++++++++++++++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/wemo/switch.py b/homeassistant/components/wemo/switch.py index 48cc1d92d67..c3d5bcb0462 100644 --- a/homeassistant/components/wemo/switch.py +++ b/homeassistant/components/wemo/switch.py @@ -64,15 +64,15 @@ class WemoSwitch(WemoBinaryStateEntity, SwitchEntity): attr: dict[str, Any] = {} if isinstance(self.wemo, Maker): # Is the maker sensor on or off. - if self.wemo.maker_params["hassensor"]: + if self.wemo.has_sensor: # Note a state of 1 matches the WeMo app 'not triggered'! - if self.wemo.maker_params["sensorstate"]: + if self.wemo.sensor_state: attr[ATTR_SENSOR_STATE] = STATE_OFF else: attr[ATTR_SENSOR_STATE] = STATE_ON # Is the maker switch configured as toggle(0) or momentary (1). - if self.wemo.maker_params["switchmode"]: + if self.wemo.switch_mode: attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_MOMENTARY else: attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_TOGGLE diff --git a/tests/components/wemo/conftest.py b/tests/components/wemo/conftest.py index fbb2f186cf5..ae900e2522f 100644 --- a/tests/components/wemo/conftest.py +++ b/tests/components/wemo/conftest.py @@ -77,6 +77,12 @@ def create_pywemo_device(pywemo_registry, pywemo_model): device.today_on_time = 5678 device.total_on_time = 9012 + if issubclass(cls, pywemo.Maker): + device.has_sensor = 1 + device.sensor_state = 1 + device.switch_mode = 1 + device.switch_state = 0 + url = f"http://{MOCK_HOST}:{MOCK_PORT}/setup.xml" with patch("pywemo.setup_url_for_address", return_value=url), patch( "pywemo.discovery.device_from_description", return_value=device diff --git a/tests/components/wemo/test_binary_sensor.py b/tests/components/wemo/test_binary_sensor.py index eccc1b180a5..f26428bf32b 100644 --- a/tests/components/wemo/test_binary_sensor.py +++ b/tests/components/wemo/test_binary_sensor.py @@ -81,19 +81,6 @@ class TestMaker(EntityTestHelpers): """Select the MakerBinarySensor entity.""" return MakerBinarySensor._name_suffix.lower() - @pytest.fixture(name="pywemo_device") - def pywemo_device_fixture(self, pywemo_device): - """Fixture for WeMoDevice instances.""" - pywemo_device.maker_params = { - "hassensor": 1, - "sensorstate": 1, - "switchmode": 1, - "switchstate": 0, - } - pywemo_device.has_sensor = pywemo_device.maker_params["hassensor"] - pywemo_device.sensor_state = pywemo_device.maker_params["sensorstate"] - yield pywemo_device - async def test_registry_state_callback( self, hass, pywemo_registry, pywemo_device, wemo_entity ): diff --git a/tests/components/wemo/test_switch.py b/tests/components/wemo/test_switch.py index d54099e5e4a..0c024295632 100644 --- a/tests/components/wemo/test_switch.py +++ b/tests/components/wemo/test_switch.py @@ -14,6 +14,9 @@ from homeassistant.components.wemo.switch import ( ATTR_ON_TODAY_TIME, ATTR_ON_TOTAL_TIME, ATTR_POWER_THRESHOLD, + ATTR_SENSOR_STATE, + ATTR_SWITCH_MODE, + MAKER_SWITCH_MOMENTARY, ) from homeassistant.const import ( ATTR_ENTITY_ID, @@ -147,3 +150,25 @@ async def test_insight_state_attributes(hass, pywemo_registry): await async_update() attributes = hass.states.get(wemo_entity.entity_id).attributes assert attributes[ATTR_CURRENT_STATE_DETAIL] == STATE_UNKNOWN + + +async def test_maker_state_attributes(hass, pywemo_registry): + """Verify the switch attributes are set for the Insight device.""" + await async_setup_component(hass, HA_DOMAIN, {}) + with create_pywemo_device(pywemo_registry, "Maker") as maker: + wemo_entity = await async_create_wemo_entity(hass, maker, "") + attributes = hass.states.get(wemo_entity.entity_id).attributes + assert attributes[ATTR_SENSOR_STATE] == STATE_OFF + assert attributes[ATTR_SWITCH_MODE] == MAKER_SWITCH_MOMENTARY + + # Test 'ON' sensor state and 'TOGGLE' switch mode values. + maker.sensor_state = 0 + maker.switch_mode = 0 + await hass.services.async_call( + HA_DOMAIN, + SERVICE_UPDATE_ENTITY, + {ATTR_ENTITY_ID: [wemo_entity.entity_id]}, + blocking=True, + ) + attributes = hass.states.get(wemo_entity.entity_id).attributes + assert attributes[ATTR_SENSOR_STATE] == STATE_ON