From 7e3a459c2ff8647cb8d926346fbaf561d41994c0 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:30:51 +0100 Subject: [PATCH] Add test case for binary sensors in ViCare (#108769) Co-authored-by: Robert Resch --- .coveragerc | 1 - tests/components/vicare/conftest.py | 34 ++++++++++++--- .../vicare/snapshots/test_binary_sensor.ambr | 42 +++++++++++++++++++ tests/components/vicare/test_binary_sensor.py | 26 ++++++++++++ 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 tests/components/vicare/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/vicare/test_binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 5d0db972ca9..184617a2882 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1517,7 +1517,6 @@ omit = homeassistant/components/vesync/switch.py homeassistant/components/viaggiatreno/sensor.py homeassistant/components/vicare/__init__.py - homeassistant/components/vicare/binary_sensor.py homeassistant/components/vicare/button.py homeassistant/components/vicare/climate.py homeassistant/components/vicare/entity.py diff --git a/tests/components/vicare/conftest.py b/tests/components/vicare/conftest.py index 5085ff6661d..46d90960f4e 100644 --- a/tests/components/vicare/conftest.py +++ b/tests/components/vicare/conftest.py @@ -2,10 +2,12 @@ from __future__ import annotations from collections.abc import AsyncGenerator, Generator +from dataclasses import dataclass from unittest.mock import AsyncMock, Mock, patch import pytest from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig +from PyViCare.PyViCareService import ViCareDeviceAccessor, readFeature from homeassistant.components.vicare.const import DOMAIN from homeassistant.core import HomeAssistant @@ -15,16 +17,26 @@ from . import ENTRY_CONFIG, MODULE from tests.common import MockConfigEntry, load_json_object_fixture +@dataclass +class Fixture: + """Fixture representation with the assigned roles and dummy data location.""" + + roles: set[str] + data_file: str + + class MockPyViCare: """Mocked PyVicare class based on a json dump.""" - def __init__(self, fixtures: list[str]) -> None: + def __init__(self, fixtures: list[Fixture]) -> None: """Init a single device from json dump.""" self.devices = [] for idx, fixture in enumerate(fixtures): self.devices.append( PyViCareDeviceConfig( - MockViCareService(fixture), + MockViCareService( + f"installation{idx}", f"gateway{idx}", f"device{idx}", fixture + ), f"deviceId{idx}", f"model{idx}", f"online{idx}", @@ -35,10 +47,22 @@ class MockPyViCare: class MockViCareService: """PyVicareService mock using a json dump.""" - def __init__(self, fixture: str) -> None: + def __init__( + self, installation_id: str, gateway_id: str, device_id: str, fixture: Fixture + ) -> None: """Initialize the mock from a json dump.""" - self._test_data = load_json_object_fixture(fixture) + self._test_data = load_json_object_fixture(fixture.data_file) self.fetch_all_features = Mock(return_value=self._test_data) + self.roles = fixture.roles + self.accessor = ViCareDeviceAccessor(installation_id, gateway_id, device_id) + + def hasRoles(self, requested_roles: list[str]) -> bool: + """Return true if requested roles are assigned.""" + return requested_roles and set(requested_roles).issubset(self.roles) + + def getProperty(self, property_name: str): + """Read a property from json dump.""" + return readFeature(self._test_data["data"], property_name) @pytest.fixture @@ -57,7 +81,7 @@ async def mock_vicare_gas_boiler( hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> AsyncGenerator[MockConfigEntry, None]: """Return a mocked ViCare API representing a single gas boiler device.""" - fixtures = ["vicare/Vitodens300W.json"] + fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")] with patch( f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures), diff --git a/tests/components/vicare/snapshots/test_binary_sensor.ambr b/tests/components/vicare/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..2d08a50bf3f --- /dev/null +++ b/tests/components/vicare/snapshots/test_binary_sensor.ambr @@ -0,0 +1,42 @@ +# serializer version: 1 +# name: test_binary_sensors[burner] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'running', + 'friendly_name': 'model0 Burner', + 'icon': 'mdi:gas-burner', + }), + 'context': , + 'entity_id': 'binary_sensor.model0_burner', + 'last_changed': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- +# name: test_binary_sensors[circulation_pump] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'running', + 'friendly_name': 'model0 Circulation pump', + 'icon': 'mdi:pump', + }), + 'context': , + 'entity_id': 'binary_sensor.model0_circulation_pump', + 'last_changed': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- +# name: test_binary_sensors[frost_protection] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'model0 Frost protection', + 'icon': 'mdi:snowflake', + }), + 'context': , + 'entity_id': 'binary_sensor.model0_frost_protection', + 'last_changed': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- diff --git a/tests/components/vicare/test_binary_sensor.py b/tests/components/vicare/test_binary_sensor.py new file mode 100644 index 00000000000..79ce91642af --- /dev/null +++ b/tests/components/vicare/test_binary_sensor.py @@ -0,0 +1,26 @@ +"""Test ViCare binary sensors.""" + +from unittest.mock import MagicMock + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.core import HomeAssistant + + +@pytest.mark.parametrize( + "entity_id", + [ + "burner", + "circulation_pump", + "frost_protection", + ], +) +async def test_binary_sensors( + hass: HomeAssistant, + mock_vicare_gas_boiler: MagicMock, + snapshot: SnapshotAssertion, + entity_id: str, +) -> None: + """Test the ViCare binary sensor.""" + assert hass.states.get(f"binary_sensor.model0_{entity_id}") == snapshot