hass-core/tests/components/weatherflow_cloud/conftest.py
Jeef de18be235d
Add Sensors to Weatherflow Cloud (#111651)
* continue

* Rebase dev

* signle function to generate attr_entity info

* rewrite icon determination as an if block

* handling PR

* Removing wind sensors for now - separate future PR

* ruff

* Update coordinator.py

Thought i already did this

* Update sensor.py

* Update icons.json

* Update sensor.py

* Update homeassistant/components/weatherflow_cloud/entity.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* working on a unified entity

* working on a unified entity

* sensor refactor

* addressing entity comment

* Update homeassistant/components/weatherflow_cloud/entity.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/weatherflow_cloud/sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* doc

* pr comments again

* fixing PR

* fixing PR

* applying entity class in sensor

* Update homeassistant/components/weatherflow_cloud/sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Cleaning up weather.py

* station id cleanup for weather class

* rewrite adding sensors the correct way

* Adding snapshot testing

* snapshot update

* added total class

* updated snapshots

* minor tweak

* snapshot away

* adding more coverage

* switch back to total

* Apply suggestions from code review

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-07-19 10:30:01 +02:00

115 lines
3.4 KiB
Python

"""Common fixtures for the WeatherflowCloud tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, Mock, patch
from aiohttp import ClientResponseError
import pytest
from weatherflow4py.models.rest.forecast import WeatherDataForecastREST
from weatherflow4py.models.rest.observation import ObservationStationREST
from weatherflow4py.models.rest.stations import StationsResponseREST
from weatherflow4py.models.rest.unified import WeatherFlowDataREST
from homeassistant.components.weatherflow_cloud.const import DOMAIN
from homeassistant.const import CONF_API_TOKEN
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.weatherflow_cloud.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_get_stations() -> Generator[AsyncMock]:
"""Mock get_stations with a sequence of responses."""
side_effects = [
True,
]
with patch(
"weatherflow4py.api.WeatherFlowRestAPI.async_get_stations",
side_effect=side_effects,
) as mock_get_stations:
yield mock_get_stations
@pytest.fixture
def mock_get_stations_500_error() -> Generator[AsyncMock]:
"""Mock get_stations with a sequence of responses."""
side_effects = [
ClientResponseError(Mock(), (), status=500),
True,
]
with patch(
"weatherflow4py.api.WeatherFlowRestAPI.async_get_stations",
side_effect=side_effects,
) as mock_get_stations:
yield mock_get_stations
@pytest.fixture
def mock_get_stations_401_error() -> Generator[AsyncMock]:
"""Mock get_stations with a sequence of responses."""
side_effects = [ClientResponseError(Mock(), (), status=401), True, True, True]
with patch(
"weatherflow4py.api.WeatherFlowRestAPI.async_get_stations",
side_effect=side_effects,
) as mock_get_stations:
yield mock_get_stations
MOCK_API_TOKEN = "1234567890"
@pytest.fixture
async def mock_config_entry() -> MockConfigEntry:
"""Fixture for MockConfigEntry."""
return MockConfigEntry(
domain=DOMAIN,
data={CONF_API_TOKEN: MOCK_API_TOKEN},
version=1,
)
@pytest.fixture
def mock_api():
"""Fixture for Mock WeatherFlowRestAPI."""
get_stations_response_data = StationsResponseREST.from_json(
load_fixture("stations.json", DOMAIN)
)
get_forecast_response_data = WeatherDataForecastREST.from_json(
load_fixture("forecast.json", DOMAIN)
)
get_observation_response_data = ObservationStationREST.from_json(
load_fixture("station_observation.json", DOMAIN)
)
data = {
24432: WeatherFlowDataREST(
weather=get_forecast_response_data,
observation=get_observation_response_data,
station=get_stations_response_data.stations[0],
device_observations=None,
)
}
with patch(
"homeassistant.components.weatherflow_cloud.coordinator.WeatherFlowRestAPI",
autospec=True,
) as mock_api_class:
# Create an instance of AsyncMock for the API
mock_api = AsyncMock()
mock_api.get_all_data.return_value = data
# Patch the class to return our mock_api instance
mock_api_class.return_value = mock_api
yield mock_api