Refactor Plugwise command handling (#66202)

This commit is contained in:
Franck Nijhof
2022-02-10 09:53:26 +01:00
committed by GitHub
parent 678e56b8b7
commit b3814aa4e6
5 changed files with 178 additions and 148 deletions

View File

@@ -1,6 +1,7 @@
"""Tests for the Plugwise Climate integration."""
from plugwise.exceptions import PlugwiseException
import pytest
from homeassistant.components.climate.const import (
HVAC_MODE_AUTO,
@@ -8,6 +9,7 @@ from homeassistant.components.climate.const import (
HVAC_MODE_OFF,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.exceptions import HomeAssistantError
from tests.components.plugwise.common import async_init_integration
@@ -56,32 +58,38 @@ async def test_adam_climate_adjust_negative_testing(hass, mock_smile_adam):
entry = await async_init_integration(hass, mock_smile_adam)
assert entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"climate",
"set_temperature",
{"entity_id": "climate.zone_lisa_wk", "temperature": 25},
blocking=True,
)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"climate",
"set_temperature",
{"entity_id": "climate.zone_lisa_wk", "temperature": 25},
blocking=True,
)
state = hass.states.get("climate.zone_lisa_wk")
attrs = state.attributes
assert attrs["temperature"] == 21.5
await hass.services.async_call(
"climate",
"set_preset_mode",
{"entity_id": "climate.zone_thermostat_jessie", "preset_mode": "home"},
blocking=True,
)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"climate",
"set_preset_mode",
{"entity_id": "climate.zone_thermostat_jessie", "preset_mode": "home"},
blocking=True,
)
state = hass.states.get("climate.zone_thermostat_jessie")
attrs = state.attributes
assert attrs["preset_mode"] == "asleep"
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.zone_thermostat_jessie", "hvac_mode": HVAC_MODE_AUTO},
blocking=True,
)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"climate",
"set_hvac_mode",
{
"entity_id": "climate.zone_thermostat_jessie",
"hvac_mode": HVAC_MODE_AUTO,
},
blocking=True,
)
state = hass.states.get("climate.zone_thermostat_jessie")
attrs = state.attributes
@@ -97,10 +105,11 @@ async def test_adam_climate_entity_climate_changes(hass, mock_smile_adam):
{"entity_id": "climate.zone_lisa_wk", "temperature": 25},
blocking=True,
)
state = hass.states.get("climate.zone_lisa_wk")
attrs = state.attributes
assert attrs["temperature"] == 25.0
assert mock_smile_adam.set_temperature.call_count == 1
mock_smile_adam.set_temperature.assert_called_with(
"c50f167537524366a5af7aa3942feb1e", 25.0
)
await hass.services.async_call(
"climate",
@@ -108,12 +117,11 @@ async def test_adam_climate_entity_climate_changes(hass, mock_smile_adam):
{"entity_id": "climate.zone_lisa_wk", "preset_mode": "away"},
blocking=True,
)
state = hass.states.get("climate.zone_lisa_wk")
attrs = state.attributes
assert attrs["preset_mode"] == "away"
assert attrs["supported_features"] == 17
assert mock_smile_adam.set_preset.call_count == 1
mock_smile_adam.set_preset.assert_called_with(
"c50f167537524366a5af7aa3942feb1e", "away"
)
await hass.services.async_call(
"climate",
@@ -122,10 +130,10 @@ async def test_adam_climate_entity_climate_changes(hass, mock_smile_adam):
blocking=True,
)
state = hass.states.get("climate.zone_thermostat_jessie")
attrs = state.attributes
assert attrs["temperature"] == 25.0
assert mock_smile_adam.set_temperature.call_count == 2
mock_smile_adam.set_temperature.assert_called_with(
"82fa13f017d240daa0d0ea1775420f24", 25.0
)
await hass.services.async_call(
"climate",
@@ -133,10 +141,11 @@ async def test_adam_climate_entity_climate_changes(hass, mock_smile_adam):
{"entity_id": "climate.zone_thermostat_jessie", "preset_mode": "home"},
blocking=True,
)
state = hass.states.get("climate.zone_thermostat_jessie")
attrs = state.attributes
assert attrs["preset_mode"] == "home"
assert mock_smile_adam.set_preset.call_count == 2
mock_smile_adam.set_preset.assert_called_with(
"82fa13f017d240daa0d0ea1775420f24", "home"
)
async def test_anna_climate_entity_attributes(hass, mock_smile_anna):
@@ -176,10 +185,10 @@ async def test_anna_climate_entity_climate_changes(hass, mock_smile_anna):
blocking=True,
)
state = hass.states.get("climate.anna")
attrs = state.attributes
assert attrs["temperature"] == 25.0
assert mock_smile_anna.set_temperature.call_count == 1
mock_smile_anna.set_temperature.assert_called_with(
"c784ee9fdab44e1395b8dee7d7a497d5", 25.0
)
await hass.services.async_call(
"climate",
@@ -188,10 +197,10 @@ async def test_anna_climate_entity_climate_changes(hass, mock_smile_anna):
blocking=True,
)
state = hass.states.get("climate.anna")
attrs = state.attributes
assert attrs["preset_mode"] == "away"
assert mock_smile_anna.set_preset.call_count == 1
mock_smile_anna.set_preset.assert_called_with(
"c784ee9fdab44e1395b8dee7d7a497d5", "away"
)
await hass.services.async_call(
"climate",
@@ -200,7 +209,8 @@ async def test_anna_climate_entity_climate_changes(hass, mock_smile_anna):
blocking=True,
)
state = hass.states.get("climate.anna")
attrs = state.attributes
assert state.state == "heat_cool"
assert mock_smile_anna.set_temperature.call_count == 1
assert mock_smile_anna.set_schedule_state.call_count == 1
mock_smile_anna.set_schedule_state.assert_called_with(
"c784ee9fdab44e1395b8dee7d7a497d5", None, "false"
)

View File

@@ -1,10 +1,11 @@
"""Tests for the Plugwise switch integration."""
from plugwise.exceptions import PlugwiseException
import pytest
from homeassistant.components.plugwise.const import DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
@@ -29,23 +30,31 @@ async def test_adam_climate_switch_negative_testing(hass, mock_smile_adam):
entry = await async_init_integration(hass, mock_smile_adam)
assert entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"switch",
"turn_off",
{"entity_id": "switch.cv_pomp"},
blocking=True,
)
state = hass.states.get("switch.cv_pomp")
assert str(state.state) == "on"
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"switch",
"turn_off",
{"entity_id": "switch.cv_pomp"},
blocking=True,
)
await hass.services.async_call(
"switch",
"turn_on",
{"entity_id": "switch.fibaro_hc2"},
blocking=True,
assert mock_smile_adam.set_switch_state.call_count == 1
mock_smile_adam.set_switch_state.assert_called_with(
"78d1126fc4c743db81b61c20e88342a7", None, "relay", "off"
)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"switch",
"turn_on",
{"entity_id": "switch.fibaro_hc2"},
blocking=True,
)
assert mock_smile_adam.set_switch_state.call_count == 2
mock_smile_adam.set_switch_state.assert_called_with(
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", "on"
)
state = hass.states.get("switch.fibaro_hc2")
assert str(state.state) == "on"
async def test_adam_climate_switch_changes(hass, mock_smile_adam):
@@ -59,8 +68,11 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam):
{"entity_id": "switch.cv_pomp"},
blocking=True,
)
state = hass.states.get("switch.cv_pomp")
assert str(state.state) == "off"
assert mock_smile_adam.set_switch_state.call_count == 1
mock_smile_adam.set_switch_state.assert_called_with(
"78d1126fc4c743db81b61c20e88342a7", None, "relay", "off"
)
await hass.services.async_call(
"switch",
@@ -68,17 +80,23 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam):
{"entity_id": "switch.fibaro_hc2"},
blocking=True,
)
state = hass.states.get("switch.fibaro_hc2")
assert str(state.state) == "off"
assert mock_smile_adam.set_switch_state.call_count == 2
mock_smile_adam.set_switch_state.assert_called_with(
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", "off"
)
await hass.services.async_call(
"switch",
"toggle",
"turn_on",
{"entity_id": "switch.fibaro_hc2"},
blocking=True,
)
state = hass.states.get("switch.fibaro_hc2")
assert str(state.state) == "on"
assert mock_smile_adam.set_switch_state.call_count == 3
mock_smile_adam.set_switch_state.assert_called_with(
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", "on"
)
async def test_stretch_switch_entities(hass, mock_stretch):
@@ -104,9 +122,10 @@ async def test_stretch_switch_changes(hass, mock_stretch):
{"entity_id": "switch.koelkast_92c4a"},
blocking=True,
)
state = hass.states.get("switch.koelkast_92c4a")
assert str(state.state) == "off"
assert mock_stretch.set_switch_state.call_count == 1
mock_stretch.set_switch_state.assert_called_with(
"e1c884e7dede431dadee09506ec4f859", None, "relay", "off"
)
await hass.services.async_call(
"switch",
@@ -114,17 +133,21 @@ async def test_stretch_switch_changes(hass, mock_stretch):
{"entity_id": "switch.droger_52559"},
blocking=True,
)
state = hass.states.get("switch.droger_52559")
assert str(state.state) == "off"
assert mock_stretch.set_switch_state.call_count == 2
mock_stretch.set_switch_state.assert_called_with(
"cfe95cf3de1948c0b8955125bf754614", None, "relay", "off"
)
await hass.services.async_call(
"switch",
"toggle",
"turn_on",
{"entity_id": "switch.droger_52559"},
blocking=True,
)
state = hass.states.get("switch.droger_52559")
assert str(state.state) == "on"
assert mock_stretch.set_switch_state.call_count == 3
mock_stretch.set_switch_state.assert_called_with(
"cfe95cf3de1948c0b8955125bf754614", None, "relay", "on"
)
async def test_unique_id_migration_plug_relay(hass, mock_smile_adam):