Parameterize SmartTub tests (#47189)

* Parameterize SmartTub tests

* parameterize light service calls

* remove stray print()

* add comment
This commit is contained in:
Matt Zimmerman 2021-03-01 04:53:57 -08:00 committed by GitHub
parent a2b13785c2
commit 947f6ea51e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 86 deletions

View file

@ -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()

View file

@ -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}"
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,
assert state.state == light_state
light: SpaLight = next(
light for light in await spa.get_lights() if light.zone == light_zone
)
light.set_mode.assert_called()
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)
else:
assert state.state == "on"
await hass.services.async_call(
"light",
"turn_off",
{"entity_id": entity_id},
service_name,
{"entity_id": entity_id, **service_params},
blocking=True,
)
light.set_mode.assert_called_with(*set_mode_args)

View file

@ -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

View file

@ -1,25 +1,25 @@
"""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)
entity_id = f"switch.{spa.brand}_{spa.model}_{entity_suffix}"
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"
assert state.state == pump_state
await hass.services.async_call(
"switch",