1 """Config flow to configure SMHI component."""
3 from __future__
import annotations
7 from smhi.smhi_lib
import Smhi, SmhiForecastException
8 import voluptuous
as vol
16 device_registry
as dr,
17 entity_registry
as er,
21 from .const
import DEFAULT_NAME, DOMAIN, HOME_LOCATION_NAME
25 hass: HomeAssistant, longitude: float, latitude: float
27 """Return true if location is ok."""
28 session = aiohttp_client.async_get_clientsession(hass)
29 smhi_api = Smhi(longitude, latitude, session=session)
31 await smhi_api.async_get_forecast()
32 except SmhiForecastException:
39 """Config flow for SMHI component."""
44 self, user_input: dict[str, Any] |
None =
None
45 ) -> ConfigFlowResult:
46 """Handle a flow initialized by the user."""
48 errors: dict[str, str] = {}
50 if user_input
is not None:
51 lat: float = user_input[CONF_LOCATION][CONF_LATITUDE]
52 lon: float = user_input[CONF_LOCATION][CONF_LONGITUDE]
54 name = f
"{DEFAULT_NAME} {round(lat, 6)} {round(lon, 6)}"
56 lat == self.hass.config.latitude
57 and lon == self.hass.config.longitude
59 name = HOME_LOCATION_NAME
61 user_input[CONF_NAME] = (
62 HOME_LOCATION_NAME
if name == HOME_LOCATION_NAME
else DEFAULT_NAME
69 errors[
"base"] =
"wrong_location"
72 CONF_LATITUDE: self.hass.config.latitude,
73 CONF_LONGITUDE: self.hass.config.longitude,
77 data_schema=vol.Schema(
84 self, user_input: dict[str, Any] |
None =
None
85 ) -> ConfigFlowResult:
86 """Handle a reconfiguration flow initialized by the user."""
87 errors: dict[str, str] = {}
90 if user_input
is not None:
91 lat: float = user_input[CONF_LOCATION][CONF_LATITUDE]
92 lon: float = user_input[CONF_LOCATION][CONF_LONGITUDE]
94 unique_id = f
"{lat}-{lon}"
98 old_lat = reconfigure_entry.data[CONF_LOCATION][CONF_LATITUDE]
99 old_lon = reconfigure_entry.data[CONF_LOCATION][CONF_LONGITUDE]
101 entity_reg = er.async_get(self.hass)
102 if entity := entity_reg.async_get_entity_id(
103 WEATHER_DOMAIN, DOMAIN, f
"{old_lat}, {old_lon}"
105 entity_reg.async_update_entity(
106 entity, new_unique_id=f
"{lat}, {lon}"
109 device_reg = dr.async_get(self.hass)
110 if device := device_reg.async_get_device(
111 identifiers={(DOMAIN, f
"{old_lat}, {old_lon}")}
113 device_reg.async_update_device(
114 device.id, new_identifiers={(DOMAIN, f
"{lat}, {lon}")}
120 data_updates=user_input,
122 errors[
"base"] =
"wrong_location"
126 reconfigure_entry.data,
129 step_id=
"reconfigure", data_schema=schema, errors=errors
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigEntry _get_reconfigure_entry(self)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
bool async_check_location(HomeAssistant hass, float longitude, float latitude)