From 947f6ea51e29e2b3f3133ba3203a7be6f53d583c Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Mon, 1 Mar 2021 04:53:57 -0800 Subject: [PATCH] Parameterize SmartTub tests (#47189) * Parameterize SmartTub tests * parameterize light service calls * remove stray print() * add comment --- homeassistant/components/smarttub/light.py | 2 +- tests/components/smarttub/test_light.py | 65 ++++++++++++---------- tests/components/smarttub/test_sensor.py | 56 ++++++++----------- tests/components/smarttub/test_switch.py | 44 +++++++-------- 4 files changed, 81 insertions(+), 86 deletions(-) diff --git a/homeassistant/components/smarttub/light.py b/homeassistant/components/smarttub/light.py index a4ada7c3024..1baf7e527eb 100644 --- a/homeassistant/components/smarttub/light.py +++ b/homeassistant/components/smarttub/light.py @@ -137,5 +137,5 @@ class SmartTubLight(SmartTubEntity, LightEntity): async def async_turn_off(self, **kwargs): """Turn the light off.""" - await self.light.set_mode(self.light.LightMode.OFF, 0) + await self.light.set_mode(SpaLight.LightMode.OFF, 0) await self.coordinator.async_request_refresh() diff --git a/tests/components/smarttub/test_light.py b/tests/components/smarttub/test_light.py index 5e9d9459eab..fe178278bee 100644 --- a/tests/components/smarttub/test_light.py +++ b/tests/components/smarttub/test_light.py @@ -1,38 +1,45 @@ """Test the SmartTub light platform.""" +import pytest from smarttub import SpaLight -async def test_light(spa, setup_entry, hass): +# the light in light_zone should have initial state light_state. we will call +# service_name with service_params, and expect the resultant call to +# SpaLight.set_mode to have set_mode_args parameters +@pytest.mark.parametrize( + "light_zone,light_state,service_name,service_params,set_mode_args", + [ + (1, "off", "turn_on", {}, (SpaLight.LightMode.PURPLE, 50)), + (1, "off", "turn_on", {"brightness": 255}, (SpaLight.LightMode.PURPLE, 100)), + (2, "on", "turn_off", {}, (SpaLight.LightMode.OFF, 0)), + ], +) +async def test_light( + spa, + setup_entry, + hass, + light_zone, + light_state, + service_name, + service_params, + set_mode_args, +): """Test light entity.""" - for light in spa.get_lights.return_value: - entity_id = f"light.{spa.brand}_{spa.model}_light_{light.zone}" - state = hass.states.get(entity_id) - assert state is not None - if light.mode == SpaLight.LightMode.OFF: - assert state.state == "off" - await hass.services.async_call( - "light", - "turn_on", - {"entity_id": entity_id}, - blocking=True, - ) - light.set_mode.assert_called() + entity_id = f"light.{spa.brand}_{spa.model}_light_{light_zone}" + state = hass.states.get(entity_id) + assert state is not None + assert state.state == light_state - await hass.services.async_call( - "light", - "turn_on", - {"entity_id": entity_id, "brightness": 255}, - blocking=True, - ) - light.set_mode.assert_called_with(SpaLight.LightMode.PURPLE, 100) + light: SpaLight = next( + light for light in await spa.get_lights() if light.zone == light_zone + ) - else: - assert state.state == "on" - await hass.services.async_call( - "light", - "turn_off", - {"entity_id": entity_id}, - blocking=True, - ) + await hass.services.async_call( + "light", + service_name, + {"entity_id": entity_id, **service_params}, + blocking=True, + ) + light.set_mode.assert_called_with(*set_mode_args) diff --git a/tests/components/smarttub/test_sensor.py b/tests/components/smarttub/test_sensor.py index 2d52d6d07a5..4f179b24910 100644 --- a/tests/components/smarttub/test_sensor.py +++ b/tests/components/smarttub/test_sensor.py @@ -1,46 +1,30 @@ """Test the SmartTub sensor platform.""" -from . import trigger_update +import pytest -async def test_sensors(spa, setup_entry, hass): - """Test the sensors.""" +@pytest.mark.parametrize( + "entity_suffix,expected_state", + [ + ("state", "normal"), + ("flow_switch", "open"), + ("ozone", "off"), + ("uv", "off"), + ("blowout_cycle", "inactive"), + ("cleanup_cycle", "inactive"), + ], +) +async def test_sensor(spa, setup_entry, hass, entity_suffix, expected_state): + """Test simple sensors.""" - entity_id = f"sensor.{spa.brand}_{spa.model}_state" + entity_id = f"sensor.{spa.brand}_{spa.model}_{entity_suffix}" state = hass.states.get(entity_id) assert state is not None - assert state.state == "normal" + assert state.state == expected_state - spa.get_status.return_value.state = "BAD" - await trigger_update(hass) - state = hass.states.get(entity_id) - assert state is not None - assert state.state == "bad" - entity_id = f"sensor.{spa.brand}_{spa.model}_flow_switch" - state = hass.states.get(entity_id) - assert state is not None - assert state.state == "open" - - entity_id = f"sensor.{spa.brand}_{spa.model}_ozone" - state = hass.states.get(entity_id) - assert state is not None - assert state.state == "off" - - entity_id = f"sensor.{spa.brand}_{spa.model}_uv" - state = hass.states.get(entity_id) - assert state is not None - assert state.state == "off" - - entity_id = f"sensor.{spa.brand}_{spa.model}_blowout_cycle" - state = hass.states.get(entity_id) - assert state is not None - assert state.state == "inactive" - - entity_id = f"sensor.{spa.brand}_{spa.model}_cleanup_cycle" - state = hass.states.get(entity_id) - assert state is not None - assert state.state == "inactive" +async def test_primary_filtration(spa, setup_entry, hass): + """Test the primary filtration cycle sensor.""" entity_id = f"sensor.{spa.brand}_{spa.model}_primary_filtration_cycle" state = hass.states.get(entity_id) @@ -51,6 +35,10 @@ async def test_sensors(spa, setup_entry, hass): assert state.attributes["mode"] == "normal" assert state.attributes["start_hour"] == 2 + +async def test_secondary_filtration(spa, setup_entry, hass): + """Test the secondary filtration cycle sensor.""" + entity_id = f"sensor.{spa.brand}_{spa.model}_secondary_filtration_cycle" state = hass.states.get(entity_id) assert state is not None diff --git a/tests/components/smarttub/test_switch.py b/tests/components/smarttub/test_switch.py index 1ef84631196..89e0ec03b23 100644 --- a/tests/components/smarttub/test_switch.py +++ b/tests/components/smarttub/test_switch.py @@ -1,30 +1,30 @@ """Test the SmartTub switch platform.""" -from smarttub import SpaPump +import pytest -async def test_pumps(spa, setup_entry, hass): +@pytest.mark.parametrize( + "pump_id,entity_suffix,pump_state", + [ + ("CP", "circulation_pump", "off"), + ("P1", "jet_p1", "off"), + ("P2", "jet_p2", "on"), + ], +) +async def test_pumps(spa, setup_entry, hass, pump_id, pump_state, entity_suffix): """Test pump entities.""" - for pump in spa.get_pumps.return_value: - if pump.type == SpaPump.PumpType.CIRCULATION: - entity_id = f"switch.{spa.brand}_{spa.model}_circulation_pump" - elif pump.type == SpaPump.PumpType.JET: - entity_id = f"switch.{spa.brand}_{spa.model}_jet_{pump.id.lower()}" - else: - raise NotImplementedError("Unknown pump type") + pump = next(pump for pump in await spa.get_pumps() if pump.id == pump_id) - state = hass.states.get(entity_id) - assert state is not None - if pump.state == SpaPump.PumpState.OFF: - assert state.state == "off" - else: - assert state.state == "on" + entity_id = f"switch.{spa.brand}_{spa.model}_{entity_suffix}" + state = hass.states.get(entity_id) + assert state is not None + assert state.state == pump_state - await hass.services.async_call( - "switch", - "toggle", - {"entity_id": entity_id}, - blocking=True, - ) - pump.toggle.assert_called() + await hass.services.async_call( + "switch", + "toggle", + {"entity_id": entity_id}, + blocking=True, + ) + pump.toggle.assert_called()