Skip to content

Commit

Permalink
Migration to 2023.4 and fix lingering tasks and timers tests errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Apr 22, 2023
1 parent 429ff47 commit 4dd7c62
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/versatile_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,19 +601,19 @@ async def async_added_to_hass(self):
)
)

self.async_on_remove(self.async_remove_thermostat)
self.async_on_remove(self.remove_thermostat)

try:
await self.async_startup()
except UnknownEntity:
# Ingore this error which is possible if underlying climate is not found temporary
pass

def async_remove_thermostat(self):
async def remove_thermostat(self):
"""Called when the thermostat will be removed"""
_LOGGER.info("%s - Removing thermostat", self)
for under in self._underlyings:
under.remove_entity()
await under.remove_entity()

async def async_startup(self):
"""Triggered on startup, used to get old state and set internal states accordingly"""
Expand Down Expand Up @@ -1521,7 +1521,9 @@ async def try_motion_condition(_):
)
# We do not change the preset which is kept to ACTIVITY but only the target_temperature
# We take the presence into account
await self._async_internal_set_temperature(self.find_preset_temp(new_preset))
await self._async_internal_set_temperature(
self.find_preset_temp(new_preset)
)
self.recalculate()
await self._async_control_heating(force=True)

Expand Down
1 change: 1 addition & 0 deletions custom_components/versatile_thermostat/tests/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging
from unittest.mock import patch, MagicMock
import pytest

from homeassistant.core import HomeAssistant, Event, EVENT_STATE_CHANGED, State
from homeassistant.const import UnitOfTemperature, STATE_ON, STATE_OFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pytest_homeassistant_custom_component.common import MockConfigEntry

from .commons import *
from ..climate import VersatileThermostat
from ..binary_sensor import (
SecurityBinarySensor,
Expand All @@ -21,6 +22,8 @@
from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_security_binary_sensors(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -96,6 +99,8 @@ async def test_security_binary_sensors(
assert security_binary_sensor.state == STATE_OFF


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_overpowering_binary_sensors(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -178,6 +183,8 @@ async def test_overpowering_binary_sensors(
assert overpowering_binary_sensor.state == STATE_OFF


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_window_binary_sensors(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -264,6 +271,8 @@ async def test_window_binary_sensors(
assert window_binary_sensor.state == STATE_OFF


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_motion_binary_sensors(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -350,6 +359,8 @@ async def test_motion_binary_sensors(
assert motion_binary_sensor.state == STATE_OFF


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_presence_binary_sensors(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -432,6 +443,8 @@ async def test_presence_binary_sensors(
assert presence_binary_sensor.state == STATE_OFF


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_binary_sensors_over_climate_minimal(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down
8 changes: 8 additions & 0 deletions custom_components/versatile_thermostat/tests/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
logging.getLogger().setLevel(logging.DEBUG)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_bug_56(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -85,6 +87,8 @@ async def test_bug_56(
entity.update_custom_attributes()


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_bug_63(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -135,6 +139,8 @@ async def test_bug_63(

# Waiting for answer in https://github.com/jmcollin78/versatile_thermostat/issues/64
# Repro case not evident
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_bug_64(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -180,6 +186,8 @@ async def test_bug_64(
assert entity


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_bug_66(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down
13 changes: 13 additions & 0 deletions custom_components/versatile_thermostat/tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

from custom_components.versatile_thermostat.const import DOMAIN

from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import
from .const import * # pylint: disable=wildcard-import, unused-wildcard-import


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_show_form(hass: HomeAssistant) -> None:
"""Test that the form is served with no input"""
# Init the API
Expand All @@ -24,6 +27,8 @@ async def test_show_form(hass: HomeAssistant) -> None:
assert result["step_id"] == SOURCE_USER


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_user_config_flow_over_switch(hass: HomeAssistant, skip_hass_states_get):
"""Test the config flow with all thermostat_over_switch features"""
result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -121,6 +126,8 @@ async def test_user_config_flow_over_switch(hass: HomeAssistant, skip_hass_state
assert isinstance(result["result"], ConfigEntry)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_user_config_flow_over_climate(hass: HomeAssistant, skip_hass_states_get):
"""Test the config flow with all thermostat_over_climate features and no additional features"""
result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -206,6 +213,8 @@ async def test_user_config_flow_over_climate(hass: HomeAssistant, skip_hass_stat
assert isinstance(result["result"], ConfigEntry)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_user_config_flow_window_auto_ok(
hass: HomeAssistant, skip_hass_states_get, skip_control_heating
):
Expand Down Expand Up @@ -301,6 +310,8 @@ async def test_user_config_flow_window_auto_ok(
assert isinstance(result["result"], ConfigEntry)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_user_config_flow_window_auto_ko(
hass: HomeAssistant, skip_hass_states_get
):
Expand Down Expand Up @@ -371,6 +382,8 @@ async def test_user_config_flow_window_auto_ko(
}


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_user_config_flow_over_4_switches(
hass: HomeAssistant, skip_hass_states_get, skip_control_heating
):
Expand Down
6 changes: 6 additions & 0 deletions custom_components/versatile_thermostat/tests/test_movement.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
logging.getLogger().setLevel(logging.DEBUG)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_movement_management_time_not_enough(
hass: HomeAssistant, skip_hass_states_is_state
):
Expand Down Expand Up @@ -140,6 +142,8 @@ async def test_movement_management_time_not_enough(
assert mock_send_event.call_count == 0


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_movement_management_time_enough_and_presence(
hass: HomeAssistant, skip_hass_states_is_state
):
Expand Down Expand Up @@ -270,6 +274,8 @@ async def test_movement_management_time_enough_and_presence(
assert mock_send_event.call_count == 0


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_movement_management_time_enoughand_not_presence(
hass: HomeAssistant, skip_hass_states_is_state
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
logging.getLogger().setLevel(logging.DEBUG)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_one_switch_cycle(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -206,6 +208,8 @@ async def test_one_switch_cycle(
# assert entity.underlying_entity(0)._should_relaunch_control_heating is False


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_multiple_switchs(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down
8 changes: 8 additions & 0 deletions custom_components/versatile_thermostat/tests/test_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
logging.getLogger().setLevel(logging.DEBUG)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_power_management_hvac_off(
hass: HomeAssistant, skip_hass_states_is_state
):
Expand Down Expand Up @@ -96,6 +98,8 @@ async def test_power_management_hvac_off(
assert mock_heater_off.call_count == 0


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_power_management_hvac_on(hass: HomeAssistant, skip_hass_states_is_state):
"""Test the Power management"""

Expand Down Expand Up @@ -226,6 +230,8 @@ async def test_power_management_hvac_on(hass: HomeAssistant, skip_hass_states_is
assert mock_heater_off.call_count == 0


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_power_management_energy_over_switch(
hass: HomeAssistant, skip_hass_states_is_state
):
Expand Down Expand Up @@ -350,6 +356,8 @@ async def test_power_management_energy_over_switch(
assert round(entity.total_energy, 2) == round((2.0 + 0.6) * 100 * 5 / 60.0, 2)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_power_management_energy_over_climate(
hass: HomeAssistant, skip_hass_states_is_state
):
Expand Down
2 changes: 2 additions & 0 deletions custom_components/versatile_thermostat/tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
logging.getLogger().setLevel(logging.DEBUG)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_security_feature(hass: HomeAssistant, skip_hass_states_is_state):
"""Test the security feature and https://github.com/jmcollin78/versatile_thermostat/issues/49:
1. creates a thermostat and check that security is off
Expand Down
6 changes: 6 additions & 0 deletions custom_components/versatile_thermostat/tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_sensors_over_switch(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -182,6 +184,8 @@ async def test_sensors_over_switch(
cancel_switchs_cycles(entity)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_sensors_over_climate(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down Expand Up @@ -316,6 +320,8 @@ async def test_sensors_over_climate(
assert last_ext_temperature_sensor.device_class == SensorDeviceClass.TIMESTAMP


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_sensors_over_climate_minimal(
hass: HomeAssistant,
skip_hass_states_is_state,
Expand Down
6 changes: 6 additions & 0 deletions custom_components/versatile_thermostat/tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_over_switch_full_start(hass: HomeAssistant, skip_hass_states_is_state):
"""Test the normal full start of a thermostat in thermostat_over_switch type"""

Expand Down Expand Up @@ -76,6 +78,8 @@ def find_my_entity(entity_id) -> ClimateEntity:
)


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_over_climate_full_start(hass: HomeAssistant, skip_hass_states_is_state):
"""Test the normal full start of a thermostat in thermostat_over_climate type"""

Expand Down Expand Up @@ -143,6 +147,8 @@ def find_my_entity(entity_id) -> ClimateEntity:
mock_find_climate.assert_has_calls([call.find_underlying_entity()])


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_over_4switch_full_start(hass: HomeAssistant, skip_hass_states_is_state):
"""Test the normal full start of a thermostat in thermostat_over_switch with 4 switches type"""

Expand Down
2 changes: 2 additions & 0 deletions custom_components/versatile_thermostat/tests/test_tpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import


@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_tpi_calculation(hass: HomeAssistant, skip_hass_states_is_state):
"""Test the TPI calculation"""

Expand Down
Loading

0 comments on commit 4dd7c62

Please sign in to comment.