Skip to content

Commit

Permalink
Add hvac_modes property to Plugwise (#102636)
Browse files Browse the repository at this point in the history
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
  • Loading branch information
bouwew and frenck committed Oct 24, 2023
1 parent 9600c7f commit 8c3ae1b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 62 deletions.
19 changes: 12 additions & 7 deletions homeassistant/components/plugwise/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ def __init__(
self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
self._attr_preset_modes = presets

# Determine hvac modes and current hvac mode
self._attr_hvac_modes = [HVACMode.HEAT]
if self.coordinator.data.gateway["cooling_present"]:
self._attr_hvac_modes = [HVACMode.HEAT_COOL]
if self.device["available_schedules"] != ["None"]:
self._attr_hvac_modes.append(HVACMode.AUTO)

self._attr_min_temp = self.device["thermostat"]["lower_bound"]
self._attr_max_temp = self.device["thermostat"]["upper_bound"]
# Ensure we don't drop below 0.1
Expand Down Expand Up @@ -117,6 +110,18 @@ def hvac_mode(self) -> HVACMode:
return HVACMode.HEAT
return HVACMode(mode)

@property
def hvac_modes(self) -> list[HVACMode]:
"""Return the list of available HVACModes."""
hvac_modes = [HVACMode.HEAT]
if self.coordinator.data.gateway["cooling_present"]:
hvac_modes = [HVACMode.HEAT_COOL]

if self.device["available_schedules"] != ["None"]:
hvac_modes.append(HVACMode.AUTO)

return hvac_modes

@property
def hvac_action(self) -> HVACAction | None:
"""Return the current running hvac operation if supported."""
Expand Down
84 changes: 29 additions & 55 deletions tests/components/plugwise/test_climate.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""Tests for the Plugwise Climate integration."""
from unittest.mock import MagicMock

from datetime import timedelta
from unittest.mock import MagicMock, patch

from plugwise.exceptions import PlugwiseError
import pytest

from homeassistant.components.climate import HVACMode
from homeassistant.components.climate.const import HVACMode
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util.dt import utcnow

from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, async_fire_time_changed


async def test_adam_climate_entity_attributes(
Expand Down Expand Up @@ -87,8 +90,6 @@ async def test_adam_climate_adjust_negative_testing(
hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry
) -> None:
"""Test exceptions of climate entities."""
mock_smile_adam.set_preset.side_effect = PlugwiseError
mock_smile_adam.set_schedule_state.side_effect = PlugwiseError
mock_smile_adam.set_temperature.side_effect = PlugwiseError

with pytest.raises(HomeAssistantError):
Expand All @@ -99,25 +100,6 @@ async def test_adam_climate_adjust_negative_testing(
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,
)

with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"climate",
"set_hvac_mode",
{
"entity_id": "climate.zone_thermostat_jessie",
"hvac_mode": HVACMode.AUTO,
},
blocking=True,
)


async def test_adam_climate_entity_climate_changes(
hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry
Expand All @@ -129,7 +111,6 @@ async def test_adam_climate_entity_climate_changes(
{"entity_id": "climate.zone_lisa_wk", "temperature": 25},
blocking=True,
)

assert mock_smile_adam.set_temperature.call_count == 1
mock_smile_adam.set_temperature.assert_called_with(
"c50f167537524366a5af7aa3942feb1e", {"setpoint": 25.0}
Expand All @@ -145,7 +126,6 @@ async def test_adam_climate_entity_climate_changes(
},
blocking=True,
)

assert mock_smile_adam.set_temperature.call_count == 2
mock_smile_adam.set_temperature.assert_called_with(
"c50f167537524366a5af7aa3942feb1e", {"setpoint": 25.0}
Expand All @@ -165,34 +145,20 @@ async def test_adam_climate_entity_climate_changes(
{"entity_id": "climate.zone_lisa_wk", "preset_mode": "away"},
blocking=True,
)

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",
"set_temperature",
{"entity_id": "climate.zone_thermostat_jessie", "temperature": 25},
blocking=True,
)

assert mock_smile_adam.set_temperature.call_count == 3
mock_smile_adam.set_temperature.assert_called_with(
"82fa13f017d240daa0d0ea1775420f24", {"setpoint": 25.0}
)

await hass.services.async_call(
"climate",
"set_preset_mode",
{"entity_id": "climate.zone_thermostat_jessie", "preset_mode": "home"},
"set_hvac_mode",
{"entity_id": "climate.zone_lisa_wk", "hvac_mode": "heat"},
blocking=True,
)

assert mock_smile_adam.set_preset.call_count == 2
mock_smile_adam.set_preset.assert_called_with(
"82fa13f017d240daa0d0ea1775420f24", "home"
assert mock_smile_adam.set_schedule_state.call_count == 2
mock_smile_adam.set_schedule_state.assert_called_with(
"c50f167537524366a5af7aa3942feb1e", "GF7 Woonkamer", "off"
)

with pytest.raises(HomeAssistantError):
Expand Down Expand Up @@ -270,7 +236,9 @@ async def test_anna_3_climate_entity_attributes(


async def test_anna_climate_entity_climate_changes(
hass: HomeAssistant, mock_smile_anna: MagicMock, init_integration: MockConfigEntry
hass: HomeAssistant,
mock_smile_anna: MagicMock,
init_integration: MockConfigEntry,
) -> None:
"""Test handling of user requests in anna climate device environment."""
await hass.services.async_call(
Expand All @@ -279,7 +247,6 @@ async def test_anna_climate_entity_climate_changes(
{"entity_id": "climate.anna", "target_temp_high": 25, "target_temp_low": 20},
blocking=True,
)

assert mock_smile_anna.set_temperature.call_count == 1
mock_smile_anna.set_temperature.assert_called_with(
"c784ee9fdab44e1395b8dee7d7a497d5",
Expand All @@ -292,7 +259,6 @@ async def test_anna_climate_entity_climate_changes(
{"entity_id": "climate.anna", "preset_mode": "away"},
blocking=True,
)

assert mock_smile_anna.set_preset.call_count == 1
mock_smile_anna.set_preset.assert_called_with(
"c784ee9fdab44e1395b8dee7d7a497d5", "away"
Expand All @@ -301,24 +267,32 @@ async def test_anna_climate_entity_climate_changes(
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.anna", "hvac_mode": "heat"},
{"entity_id": "climate.anna", "hvac_mode": "auto"},
blocking=True,
)

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", "standaard", "off"
"c784ee9fdab44e1395b8dee7d7a497d5", "standaard", "on"
)

await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.anna", "hvac_mode": "auto"},
{"entity_id": "climate.anna", "hvac_mode": "heat"},
blocking=True,
)

assert mock_smile_anna.set_schedule_state.call_count == 2
mock_smile_anna.set_schedule_state.assert_called_with(
"c784ee9fdab44e1395b8dee7d7a497d5", "standaard", "on"
"c784ee9fdab44e1395b8dee7d7a497d5", "standaard", "off"
)
data = mock_smile_anna.async_update.return_value
data.devices["3cb70739631c4d17a86b8b12e8a5161b"]["available_schedules"] = ["None"]
with patch(
"homeassistant.components.plugwise.coordinator.Smile.async_update",
return_value=data,
):
async_fire_time_changed(hass, utcnow() + timedelta(minutes=1))
await hass.async_block_till_done()
state = hass.states.get("climate.anna")
assert state.state == HVACMode.HEAT
assert state.attributes["hvac_modes"] == [HVACMode.HEAT]

0 comments on commit 8c3ae1b

Please sign in to comment.