From 22a3b646aaf3b26cf6b0b3279e5717a40fd152a2 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Wed, 29 Jan 2025 18:57:09 +0000 Subject: [PATCH] Add github copilot Add first test ok for UnderlyingSwitch --- .devcontainer/devcontainer.json | 4 +- .../versatile_thermostat/underlyings.py | 2 +- tests/test_virtual_switch.py | 62 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/test_virtual_switch.py diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 147f824..216abbf 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -37,7 +37,9 @@ "yzhang.markdown-all-in-one", "github.vscode-github-actions", "azuretools.vscode-docker", - "huizhou.githd" + "huizhou.githd", + "github.copilot", + "github.copilot-chat" ], "settings": { "files.eol": "\n", diff --git a/custom_components/versatile_thermostat/underlyings.py b/custom_components/versatile_thermostat/underlyings.py index 866dd93..bfe1914 100644 --- a/custom_components/versatile_thermostat/underlyings.py +++ b/custom_components/versatile_thermostat/underlyings.py @@ -244,7 +244,7 @@ class UnderlyingSwitch(UnderlyingEntity): @overrides @property - def is_inversed(self): + def is_inversed(self) -> bool: """Tells if the switch command should be inversed""" return self._thermostat.is_inversed diff --git a/tests/test_virtual_switch.py b/tests/test_virtual_switch.py new file mode 100644 index 0000000..6376dc8 --- /dev/null +++ b/tests/test_virtual_switch.py @@ -0,0 +1,62 @@ +""" Test of virtual switch """ + +# pylint: disable=wildcard-import, unused-wildcard-import, protected-access + +import pytest +from .commons import * +from unittest.mock import patch, call, MagicMock, PropertyMock + +from homeassistant.const import STATE_ON, STATE_OFF + +from custom_components.versatile_thermostat.underlyings import UnderlyingSwitch +from custom_components.versatile_thermostat.thermostat_switch import ThermostatOverSwitch + + +@pytest.mark.parametrize( + "is_inversed, vswitch_on_command, vswitch_off_command, expected_command_on, expected_data_on, expected_state_on, expected_command_off, expected_data_off, expected_state_off", + [ + # Select + ( + False, + "select_option/option:comfort", + "select_option/option:frost", + "select_option", + {"entity_id": "switch.test", "option": "comfort"}, + PRESET_COMFORT, + "select_option", + {"entity_id": "switch.test", "option": "frost"}, + PRESET_FROST_PROTECTION, + ), + # switch + (False, "turn_on/:on", "turn_off/:off", "turn_on", {"entity_id": "switch.test", None: None}, STATE_ON, "turn_off", {"entity_id": "switch.test", None: None}, STATE_OFF), + ], +) +def test_build_command( + hass, + is_inversed, + vswitch_on_command, + vswitch_off_command, + expected_command_on, + expected_data_on, + expected_state_on, + expected_command_off, + expected_data_off, + expected_state_off, +): + """Test the initialisation of a UnderlyingSwitch with some personnalisations commands""" + + vtherm = MagicMock(spec=ThermostatOverSwitch) + type(vtherm).is_inversed = PropertyMock(return_value=is_inversed) + + assert vtherm.is_inversed == is_inversed + under = UnderlyingSwitch(hass, vtherm, "switch.test", 0, 0, vswitch_on_command, vswitch_off_command) + + assert under.is_inversed == is_inversed + + assert under._on_command.get("command") == expected_command_on + assert under._on_command.get("data") == expected_data_on + assert under._on_command.get("state") == expected_state_on + + assert under._off_command.get("command") == expected_command_off + assert under._off_command.get("data") == expected_data_off + assert under._off_command.get("state") == expected_state_off