Files
core/tests/components/altruist/conftest.py
Alena Bugrova f8267b13d7 Add Altruist integration to Core (#146158)
* add altruist integration and tests

* requested fixes + remove some deprecated sensors

* add tests for unknown sensor and device attribute in config_flow

* use CONF_ in data_schema

* suggested fixes

* remove test_setup_entry_success

* create ZeroconfServiceInfo in tests

* use CONF_IP_ADDRESS in tests

* add unique id assert

* add integration to strict-typing, set unavailable if no sensor key in data, change device name

* use add_suggested_values_to_schema, mmHg for pressure

* update snapshots and config entry name in tests

* remove changes in devcontainer config

* fixture for create client error, typing in tests, remove "Altruist" from device name

* change native_value_fn return type

* change sensor.py docstring

* remove device id from entry data, fix docstrings

* remove checks for client and device attributes

* use less variables in tests

* change creating AltruistSensor, remove device from arguments

* Update homeassistant/components/altruist/sensor.py

* Update homeassistant/components/altruist/quality_scale.yaml

* Update homeassistant/components/altruist/quality_scale.yaml

* Update quality_scale.yaml

* hassfest run

* suggested fixes

* set suggested_unit_of_measurement for pressure

* use mock_config_entry, update snapshots

* abort if cant create client on zeroconf step

* move sensor names in translatin placeholders

---------

Co-authored-by: Josef Zweck <josef@zweck.dev>
2025-06-23 16:57:51 +02:00

83 lines
2.4 KiB
Python

"""Altruist tests configuration."""
from collections.abc import Generator
import json
from unittest.mock import AsyncMock, Mock, patch
from altruistclient import AltruistDeviceModel, AltruistError
import pytest
from homeassistant.components.altruist.const import CONF_HOST, DOMAIN
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.altruist.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return a mock config entry."""
return MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.1.100"},
unique_id="5366960e8b18",
title="5366960e8b18",
)
@pytest.fixture
def mock_altruist_device() -> Mock:
"""Return a mock AltruistDeviceModel."""
device = Mock(spec=AltruistDeviceModel)
device.id = "5366960e8b18"
device.name = "Altruist Sensor"
device.ip_address = "192.168.1.100"
device.fw_version = "R_2025-03"
return device
@pytest.fixture
def mock_altruist_client(mock_altruist_device: Mock) -> Generator[AsyncMock]:
"""Return a mock AltruistClient."""
with (
patch(
"homeassistant.components.altruist.coordinator.AltruistClient",
autospec=True,
) as mock_client_class,
patch(
"homeassistant.components.altruist.config_flow.AltruistClient",
new=mock_client_class,
),
):
mock_instance = AsyncMock()
mock_instance.device = mock_altruist_device
mock_instance.device_id = mock_altruist_device.id
mock_instance.sensor_names = json.loads(
load_fixture("sensor_names.json", DOMAIN)
)
mock_instance.fetch_data.return_value = json.loads(
load_fixture("real_data.json", DOMAIN)
)
mock_client_class.from_ip_address = AsyncMock(return_value=mock_instance)
yield mock_instance
@pytest.fixture
def mock_altruist_client_fails_once(mock_altruist_client: AsyncMock) -> Generator[None]:
"""Patch AltruistClient to fail once and then succeed."""
with patch(
"homeassistant.components.altruist.config_flow.AltruistClient.from_ip_address",
side_effect=[AltruistError("Connection failed"), mock_altruist_client],
):
yield