Add test case for binary sensors in ViCare (#108769)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Christopher Fenner 2024-01-31 09:30:51 +01:00 committed by GitHub
parent db0486c5e1
commit 7e3a459c2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 97 additions and 6 deletions

View file

@ -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

View file

@ -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),

View file

@ -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': <ANY>,
'entity_id': 'binary_sensor.model0_burner',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---
# name: test_binary_sensors[circulation_pump]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'running',
'friendly_name': 'model0 Circulation pump',
'icon': 'mdi:pump',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.model0_circulation_pump',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---
# name: test_binary_sensors[frost_protection]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'model0 Frost protection',
'icon': 'mdi:snowflake',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.model0_frost_protection',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---

View file

@ -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