mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add test case for binary sensors in ViCare (#108769)
Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
parent
db0486c5e1
commit
7e3a459c2f
@ -1517,7 +1517,6 @@ omit =
|
|||||||
homeassistant/components/vesync/switch.py
|
homeassistant/components/vesync/switch.py
|
||||||
homeassistant/components/viaggiatreno/sensor.py
|
homeassistant/components/viaggiatreno/sensor.py
|
||||||
homeassistant/components/vicare/__init__.py
|
homeassistant/components/vicare/__init__.py
|
||||||
homeassistant/components/vicare/binary_sensor.py
|
|
||||||
homeassistant/components/vicare/button.py
|
homeassistant/components/vicare/button.py
|
||||||
homeassistant/components/vicare/climate.py
|
homeassistant/components/vicare/climate.py
|
||||||
homeassistant/components/vicare/entity.py
|
homeassistant/components/vicare/entity.py
|
||||||
|
@ -2,10 +2,12 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import AsyncGenerator, Generator
|
from collections.abc import AsyncGenerator, Generator
|
||||||
|
from dataclasses import dataclass
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||||
|
from PyViCare.PyViCareService import ViCareDeviceAccessor, readFeature
|
||||||
|
|
||||||
from homeassistant.components.vicare.const import DOMAIN
|
from homeassistant.components.vicare.const import DOMAIN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -15,16 +17,26 @@ from . import ENTRY_CONFIG, MODULE
|
|||||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
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:
|
class MockPyViCare:
|
||||||
"""Mocked PyVicare class based on a json dump."""
|
"""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."""
|
"""Init a single device from json dump."""
|
||||||
self.devices = []
|
self.devices = []
|
||||||
for idx, fixture in enumerate(fixtures):
|
for idx, fixture in enumerate(fixtures):
|
||||||
self.devices.append(
|
self.devices.append(
|
||||||
PyViCareDeviceConfig(
|
PyViCareDeviceConfig(
|
||||||
MockViCareService(fixture),
|
MockViCareService(
|
||||||
|
f"installation{idx}", f"gateway{idx}", f"device{idx}", fixture
|
||||||
|
),
|
||||||
f"deviceId{idx}",
|
f"deviceId{idx}",
|
||||||
f"model{idx}",
|
f"model{idx}",
|
||||||
f"online{idx}",
|
f"online{idx}",
|
||||||
@ -35,10 +47,22 @@ class MockPyViCare:
|
|||||||
class MockViCareService:
|
class MockViCareService:
|
||||||
"""PyVicareService mock using a json dump."""
|
"""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."""
|
"""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.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
|
@pytest.fixture
|
||||||
@ -57,7 +81,7 @@ async def mock_vicare_gas_boiler(
|
|||||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||||
) -> AsyncGenerator[MockConfigEntry, None]:
|
) -> AsyncGenerator[MockConfigEntry, None]:
|
||||||
"""Return a mocked ViCare API representing a single gas boiler device."""
|
"""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(
|
with patch(
|
||||||
f"{MODULE}.vicare_login",
|
f"{MODULE}.vicare_login",
|
||||||
return_value=MockPyViCare(fixtures),
|
return_value=MockPyViCare(fixtures),
|
||||||
|
42
tests/components/vicare/snapshots/test_binary_sensor.ambr
Normal file
42
tests/components/vicare/snapshots/test_binary_sensor.ambr
Normal 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',
|
||||||
|
})
|
||||||
|
# ---
|
26
tests/components/vicare/test_binary_sensor.py
Normal file
26
tests/components/vicare/test_binary_sensor.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user