Improve meteo_france typing (#107863)

pull/107925/head
Marc Mueller 2024-01-12 12:56:40 +01:00 committed by GitHub
parent 7e28c788cb
commit b1f1ecb40a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import logging
from meteofrance_api.client import MeteoFranceClient
from meteofrance_api.helpers import is_valid_warning_department
from meteofrance_api.model import CurrentPhenomenons, Forecast, Rain
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
@ -79,17 +80,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
latitude = entry.data[CONF_LATITUDE]
longitude = entry.data[CONF_LONGITUDE]
async def _async_update_data_forecast_forecast():
async def _async_update_data_forecast_forecast() -> Forecast:
"""Fetch data from API endpoint."""
return await hass.async_add_executor_job(
client.get_forecast, latitude, longitude
)
async def _async_update_data_rain():
async def _async_update_data_rain() -> Rain:
"""Fetch data from API endpoint."""
return await hass.async_add_executor_job(client.get_rain, latitude, longitude)
async def _async_update_data_alert():
async def _async_update_data_alert() -> CurrentPhenomenons:
"""Fetch data from API endpoint."""
return await hass.async_add_executor_job(
client.get_warning_current_phenomenoms, department, 0, True
@ -136,7 +137,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.title,
department,
)
if is_valid_warning_department(department):
if department is not None and is_valid_warning_department(department):
if not hass.data[DOMAIN].get(department):
coordinator_alert = DataUpdateCoordinator(
hass,

View File

@ -2,14 +2,17 @@
from __future__ import annotations
import logging
from typing import Any
from meteofrance_api.client import MeteoFranceClient
from meteofrance_api.model import Place
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from .const import CONF_CITY, DOMAIN
@ -21,12 +24,16 @@ class MeteoFranceFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
def __init__(self):
def __init__(self) -> None:
"""Init MeteoFranceFlowHandler."""
self.places = []
self.places: list[Place] = []
@callback
def _show_setup_form(self, user_input=None, errors=None):
def _show_setup_form(
self,
user_input: dict[str, Any] | None = None,
errors: dict[str, str] | None = None,
) -> FlowResult:
"""Show the setup form to the user."""
if user_input is None:
@ -40,9 +47,11 @@ class MeteoFranceFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
errors=errors or {},
)
async def async_step_user(self, user_input=None):
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initiated by the user."""
errors = {}
errors: dict[str, str] = {}
if user_input is None:
return self._show_setup_form(user_input, errors)
@ -72,15 +81,17 @@ class MeteoFranceFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
data={CONF_LATITUDE: latitude, CONF_LONGITUDE: longitude},
)
async def async_step_import(self, user_input):
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
"""Import a config entry."""
return await self.async_step_user(user_input)
async def async_step_cities(self, user_input=None):
async def async_step_cities(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Step where the user choose the city from the API search results."""
if not user_input:
if len(self.places) > 1 and self.source != SOURCE_IMPORT:
places_for_form = {}
places_for_form: dict[str, str] = {}
for place in self.places:
places_for_form[_build_place_key(place)] = f"{place}"
@ -106,5 +117,5 @@ class MeteoFranceFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
)
def _build_place_key(place) -> str:
def _build_place_key(place: Place) -> str:
return f"{place};{place.latitude};{place.longitude}"

View File

@ -303,7 +303,7 @@ class MeteoFranceRainSensor(MeteoFranceSensor[Rain]):
return dt_util.utc_from_timestamp(next_rain["dt"]) if next_rain else None
@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
reference_dt = self.coordinator.data.forecast[0]["dt"]
return {
@ -330,7 +330,7 @@ class MeteoFranceAlertSensor(MeteoFranceSensor[CurrentPhenomenons]):
self._attr_unique_id = self._attr_name
@property
def native_value(self):
def native_value(self) -> str | None:
"""Return the state."""
return get_warning_text_status_from_indice_color(
self.coordinator.data.get_domain_max_color()

View File

@ -110,7 +110,7 @@ class MeteoFranceWeather(
)
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return the unique id of the sensor."""
return self._unique_id