Refactor Météo-France to use API instead of web scraping (#37737)
* Add new python library * Update requirements * Remove old libs * config flow with client.search_places * WIP: UI config + weather OK * WIP: sensors * WIP: add pressure to weather + available to sensor * WIP: coordinator next_rain + alert * Make import step working * migrate to meteofrance-api v0.0.3 * Create coordinator for rain only if data available in API * Fix avoid creation of rain sensor when not available. * Add options flow for forecast mode * Fix import config causing bug with UI * Add alert sensor * Add coastal alerts when available (#5) * Use meteofrance-api feature branch on Github * Update unit of next_rain sensor * Test different type of attibutes * Typo for attribute * Next rain sensor device class as timestamp * Better design for rain entity attributes * use master branch for meteofrance-api * time displayed in the HA server timezone. * fix bug when next_rain_date_locale is None * Add precipitation and cloud cover sensors * Add variable to avoid repeating computing * Apply suggestions from code review Co-authored-by: Quentame <polletquentin74@me.com> * Attributes names in const. * Cleaning * Cleaning: use current_forecast and today_forecast * Write state to HA after fetch * Refactor, Log messages and bug fix. (#6) * Add messages in log * Refactor using 'current_forecast'. * Use % string format with _LOGGER * Remove inconsistent path * Secure timestamp value and get current day forecast * new unique_id * Change Log message to debug * Log messages improvement * Don't try to create weather alert sensor if not in covered zone. * convert wind speed in km/h * Better list of city in config_flow * Manage initial CONF_MODE as None * Review correction * Review coorections * unique id correction * Migrate from previous config * Make config name detailed * Fix weather alert sensor unload (#7) * Unload weather alert platform * Revert "Unload weather alert platform" This reverts commit 95259fdee84f30a5be915eb1fbb2e19fcddc97e4. * second try in async_unload_entry * Make it work * isort modification * remove weather alert logic in sensor.py * Refactor to avoid too long code lines Co-authored-by: Quentin POLLET <polletquentin74@me.com> * Update config tests to Meteo-France (#18) * Update meteo_france exception name * Update MeteoFranceClient name used in tests * Update 'test_user' * Make test_user works * Add test test_user_list * Make test_import works * Quick & Dirty fix on exception managment. WIP * allow to catch MeteoFranceClient() exceptions * remove test_abort_if_already_setup_district * bump meteofrance-api version * We do not need to test Exception in flow yet * Remove unused data * Change client1 fixture name * Change client2 fixture name * Finish cities step * Test import with multiple choice * refactor places * Add option flow test Co-authored-by: Quentin POLLET <polletquentin74@me.com> * Fix errors due to missing data in the API (#22) * fix case where probability_forecast it not in API * Workaround for probabilty_forecast data null value * Fix weather alert sensor added when shouldn't * Add a partlycloudy and cloudy value options in condition map * Enable snow chance entity * fix from review * remove summary * Other fix from PR review * WIP: error if no results in city search * Add test for config_flow when no result in search * Lint fix * generate en.json * Update homeassistant/components/meteo_france/__init__.py * Update homeassistant/components/meteo_france/__init__.py * Update homeassistant/components/meteo_france/__init__.py * Update homeassistant/components/meteo_france/sensor.py * Update homeassistant/components/meteo_france/__init__.py * Update homeassistant/components/meteo_france/__init__.py * string: city input --> city field Co-authored-by: Quentin POLLET <polletquentin74@me.com>
This commit is contained in:
1 parent
607ba08e23
commit
6b85e23408
13 files changed
+827
-371
No files matched your search
@@ -7,10 +7,7 @@ from tests.async_mock import patch
|
||||
@pytest.fixture(autouse=True)
|
||||
def patch_requests():
|
||||
"""Stub out services that makes requests."""
|
||||
patch_client = patch("homeassistant.components.meteo_france.meteofranceClient")
|
||||
patch_weather_alert = patch(
|
||||
"homeassistant.components.meteo_france.VigilanceMeteoFranceProxy"
|
||||
)
|
||||
patch_client = patch("homeassistant.components.meteo_france.MeteoFranceClient")
|
||||
|
||||
with patch_client, patch_weather_alert:
|
||||
with patch_client:
|
||||
yield
|
||||
@@ -1,29 +1,82 @@
|
||||
"""Tests for the Meteo-France config flow."""
|
||||
from meteofrance.client import meteofranceError
|
||||
from meteofrance.model import Place
|
||||
import pytest
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.meteo_france.const import CONF_CITY, DOMAIN
|
||||
from homeassistant.components.meteo_france.const import (
|
||||
CONF_CITY,
|
||||
DOMAIN,
|
||||
FORECAST_MODE_DAILY,
|
||||
FORECAST_MODE_HOURLY,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
CITY_1_POSTAL = "74220"
|
||||
CITY_1_NAME = "La Clusaz"
|
||||
CITY_2_POSTAL_DISTRICT_1 = "69001"
|
||||
CITY_2_POSTAL_DISTRICT_4 = "69004"
|
||||
CITY_2_NAME = "Lyon"
|
||||
CITY_1_LAT = 45.90417
|
||||
CITY_1_LON = 6.42306
|
||||
CITY_1_COUNTRY = "FR"
|
||||
CITY_1_ADMIN = "Rhône-Alpes"
|
||||
CITY_1_ADMIN2 = "74"
|
||||
CITY_1 = Place(
|
||||
{
|
||||
"name": CITY_1_NAME,
|
||||
"lat": CITY_1_LAT,
|
||||
"lon": CITY_1_LON,
|
||||
"country": CITY_1_COUNTRY,
|
||||
"admin": CITY_1_ADMIN,
|
||||
"admin2": CITY_1_ADMIN2,
|
||||
}
|
||||
)
|
||||
|
||||
CITY_2_NAME = "Auch"
|
||||
CITY_2_LAT = 43.64528
|
||||
CITY_2_LON = 0.58861
|
||||
CITY_2_COUNTRY = "FR"
|
||||
CITY_2_ADMIN = "Midi-Pyrénées"
|
||||
CITY_2_ADMIN2 = "32"
|
||||
CITY_2 = Place(
|
||||
{
|
||||
"name": CITY_2_NAME,
|
||||
"lat": CITY_2_LAT,
|
||||
"lon": CITY_2_LON,
|
||||
"country": CITY_2_COUNTRY,
|
||||
"admin": CITY_2_ADMIN,
|
||||
"admin2": CITY_2_ADMIN2,
|
||||
}
|
||||
)
|
||||
|
||||
CITY_3_NAME = "Auchel"
|
||||
CITY_3_LAT = 50.50833
|
||||
CITY_3_LON = 2.47361
|
||||
CITY_3_COUNTRY = "FR"
|
||||
CITY_3_ADMIN = "Nord-Pas-de-Calais"
|
||||
CITY_3_ADMIN2 = "62"
|
||||
CITY_3 = Place(
|
||||
{
|
||||
"name": CITY_3_NAME,
|
||||
"lat": CITY_3_LAT,
|
||||
"lon": CITY_3_LON,
|
||||
"country": CITY_3_COUNTRY,
|
||||
"admin": CITY_3_ADMIN,
|
||||
"admin2": CITY_3_ADMIN2,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="client_1")
|
||||
def mock_controller_client_1():
|
||||
@pytest.fixture(name="client_single")
|
||||
def mock_controller_client_single():
|
||||
"""Mock a successful client."""
|
||||
with patch(
|
||||
"homeassistant.components.meteo_france.config_flow.meteofranceClient",
|
||||
"homeassistant.components.meteo_france.config_flow.MeteoFranceClient",
|
||||
update=False,
|
||||
) as service_mock:
|
||||
service_mock.return_value.get_data.return_value = {"name": CITY_1_NAME}
|
||||
service_mock.return_value.search_places.return_value = [CITY_1]
|
||||
yield service_mock
|
||||
|
||||
|
||||
@@ -38,18 +91,29 @@ def mock_setup():
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="client_2")
|
||||
def mock_controller_client_2():
|
||||
@pytest.fixture(name="client_multiple")
|
||||
def mock_controller_client_multiple():
|
||||
"""Mock a successful client."""
|
||||
with patch(
|
||||
"homeassistant.components.meteo_france.config_flow.meteofranceClient",
|
||||
"homeassistant.components.meteo_france.config_flow.MeteoFranceClient",
|
||||
update=False,
|
||||
) as service_mock:
|
||||
service_mock.return_value.get_data.return_value = {"name": CITY_2_NAME}
|
||||
service_mock.return_value.search_places.return_value = [CITY_2, CITY_3]
|
||||
yield service_mock
|
||||
|
||||
|
||||
async def test_user(hass, client_1):
|
||||
@pytest.fixture(name="client_empty")
|
||||
def mock_controller_client_empty():
|
||||
"""Mock a successful client."""
|
||||
with patch(
|
||||
"homeassistant.components.meteo_france.config_flow.MeteoFranceClient",
|
||||
update=False,
|
||||
) as service_mock:
|
||||
service_mock.return_value.search_places.return_value = []
|
||||
yield service_mock
|
||||
|
||||
|
||||
async def test_user(hass, client_single):
|
||||
"""Test user config."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
@@ -57,32 +121,67 @@ async def test_user(hass, client_1):
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# test with all provided
|
||||
# test with all provided with search returning only 1 place
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={CONF_CITY: CITY_1_POSTAL},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["result"].unique_id == CITY_1_NAME
|
||||
assert result["title"] == CITY_1_NAME
|
||||
assert result["data"][CONF_CITY] == CITY_1_POSTAL
|
||||
assert result["result"].unique_id == f"{CITY_1_LAT}, {CITY_1_LON}"
|
||||
assert result["title"] == f"{CITY_1}"
|
||||
assert result["data"][CONF_LATITUDE] == str(CITY_1_LAT)
|
||||
assert result["data"][CONF_LONGITUDE] == str(CITY_1_LON)
|
||||
|
||||
|
||||
async def test_import(hass, client_1):
|
||||
async def test_user_list(hass, client_multiple):
|
||||
"""Test user config."""
|
||||
|
||||
# test with all provided with search returning more than 1 place
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={CONF_CITY: CITY_2_NAME},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "cities"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_CITY: f"{CITY_3};{CITY_3_LAT};{CITY_3_LON}"},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["result"].unique_id == f"{CITY_3_LAT}, {CITY_3_LON}"
|
||||
assert result["title"] == f"{CITY_3}"
|
||||
assert result["data"][CONF_LATITUDE] == str(CITY_3_LAT)
|
||||
assert result["data"][CONF_LONGITUDE] == str(CITY_3_LON)
|
||||
|
||||
|
||||
async def test_import(hass, client_multiple):
|
||||
"""Test import step."""
|
||||
# import with all
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data={CONF_CITY: CITY_1_POSTAL},
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data={CONF_CITY: CITY_2_NAME},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["result"].unique_id == CITY_1_NAME
|
||||
assert result["title"] == CITY_1_NAME
|
||||
assert result["data"][CONF_CITY] == CITY_1_POSTAL
|
||||
assert result["result"].unique_id == f"{CITY_2_LAT}, {CITY_2_LON}"
|
||||
assert result["title"] == f"{CITY_2}"
|
||||
assert result["data"][CONF_LATITUDE] == str(CITY_2_LAT)
|
||||
assert result["data"][CONF_LONGITUDE] == str(CITY_2_LON)
|
||||
|
||||
|
||||
async def test_abort_if_already_setup(hass, client_1):
|
||||
async def test_search_failed(hass, client_empty):
|
||||
"""Test error displayed if no result in search."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={CONF_CITY: CITY_1_POSTAL},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {CONF_CITY: "empty"}
|
||||
|
||||
|
||||
async def test_abort_if_already_setup(hass, client_single):
|
||||
"""Test we abort if already setup."""
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, data={CONF_CITY: CITY_1_POSTAL}, unique_id=CITY_1_NAME
|
||||
domain=DOMAIN,
|
||||
data={CONF_LATITUDE: CITY_1_LAT, CONF_LONGITUDE: CITY_1_LON},
|
||||
unique_id=f"{CITY_1_LAT}, {CITY_1_LON}",
|
||||
).add_to_hass(hass)
|
||||
|
||||
# Should fail, same CITY same postal code (import)
|
||||
@@ -100,39 +199,32 @@ async def test_abort_if_already_setup(hass, client_1):
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_abort_if_already_setup_district(hass, client_2):
|
||||
"""Test we abort if already setup."""
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, data={CONF_CITY: CITY_2_POSTAL_DISTRICT_1}, unique_id=CITY_2_NAME
|
||||
).add_to_hass(hass)
|
||||
|
||||
# Should fail, same CITY different postal code (import)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={CONF_CITY: CITY_2_POSTAL_DISTRICT_4},
|
||||
async def test_options_flow(hass: HomeAssistantType):
|
||||
"""Test config flow options."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_LATITUDE: CITY_1_LAT, CONF_LONGITUDE: CITY_1_LON},
|
||||
unique_id=f"{CITY_1_LAT}, {CITY_1_LON}",
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
# Should fail, same CITY different postal code (flow)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_CITY: CITY_2_POSTAL_DISTRICT_4},
|
||||
assert config_entry.options == {}
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# Default
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert config_entry.options[CONF_MODE] == FORECAST_MODE_DAILY
|
||||
|
||||
|
||||
async def test_client_failed(hass):
|
||||
"""Test when we have errors during client fetch."""
|
||||
with patch(
|
||||
"homeassistant.components.meteo_france.config_flow.meteofranceClient",
|
||||
side_effect=meteofranceError(),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={CONF_CITY: CITY_1_POSTAL},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "unknown"
|
||||
# Manual
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={CONF_MODE: FORECAST_MODE_HOURLY},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert config_entry.options[CONF_MODE] == FORECAST_MODE_HOURLY
|
||||
Reference in new issue
Block a user