-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"files.associations": { | ||
"*.yaml": "home-assistant" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,94 @@ | ||
"""config_flow.py""" | ||
|
||
# pylint: disable=too-few-public-methods | ||
# pylint: disable=too-few-public-methods, broad-exception-caught | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant import config_entries | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.typing import ConfigType | ||
from homeassistant.data_entry_flow import FlowResult | ||
|
||
from .const import DOMAIN | ||
|
||
|
||
@config_entries.HANDLERS.register(DOMAIN) | ||
class MeteoLtConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Handle a config flow for Meteo LT.""" | ||
"""Handle a config flow for Meteo.Lt.""" | ||
|
||
VERSION = 1 | ||
|
||
@callback | ||
def _show_config_form(self, step_id, user_input=None, errors=None): | ||
"""Show the configuration form.""" | ||
step_id = step_id or "user" | ||
user_input = user_input or {} | ||
data_schema = vol.Schema( | ||
{ | ||
vol.Required( | ||
"latitude", | ||
default=user_input.get("latitude", self.hass.config.latitude), | ||
): vol.Coerce(float), | ||
vol.Required( | ||
"longitude", | ||
default=user_input.get("longitude", self.hass.config.longitude), | ||
): vol.Coerce(float), | ||
} | ||
) | ||
return self.async_show_form( | ||
step_id=step_id, | ||
data_schema=data_schema, | ||
errors=errors, | ||
description_placeholders={"name": "Meteo.Lt"}, | ||
) | ||
|
||
async def async_step_user(self, user_input=None): | ||
"""Handle the initial step.""" | ||
errors = {} | ||
if user_input is not None: | ||
# Handle the user input and validation here | ||
await self.async_set_unique_id( | ||
f"{DOMAIN}-{user_input['latitude']}-{user_input['longitude']}" | ||
) | ||
self._abort_if_unique_id_configured() | ||
return self.async_create_entry(title="Meteo.Lt", data=user_input) | ||
return self._show_config_form("user", user_input, errors) | ||
|
||
# Get the default values from Home Assistant configuration | ||
hass_config = self.hass.config | ||
default_latitude = hass_config.latitude | ||
default_longitude = hass_config.longitude | ||
async def async_step_reconfigure(self, user_input=None) -> FlowResult: | ||
"""Handle the reconfiguration step.""" | ||
errors = {} | ||
|
||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema({ | ||
vol.Required("latitude", default=default_latitude): float, | ||
vol.Required("longitude", default=default_longitude): float, | ||
}) | ||
) | ||
if user_input is not None: | ||
entry_id = self.context["entry_id"] | ||
entry = self.hass.config_entries.async_get_entry(entry_id) | ||
if entry: | ||
new_data = dict(entry.data) | ||
new_data.update(user_input) | ||
self.hass.config_entries.async_update_entry(entry, data=new_data) | ||
return self.async_create_entry(title="Meteo.Lt", data=new_data) | ||
else: | ||
errors["base"] = "cannot_connect" | ||
|
||
entry_id = self.context["entry_id"] | ||
entry = self.hass.config_entries.async_get_entry(entry_id) | ||
if entry: | ||
current_config = entry.data | ||
default_latitude = current_config.get("latitude", self.hass.config.latitude) | ||
default_longitude = current_config.get( | ||
"longitude", self.hass.config.longitude | ||
) | ||
else: | ||
default_latitude = self.hass.config.latitude | ||
default_longitude = self.hass.config.longitude | ||
|
||
return self._show_config_form( | ||
"reconfigure", | ||
{"latitude": default_latitude, "longitude": default_longitude}, | ||
errors, | ||
) | ||
|
||
async def async_step_reconfigure_confirm(self, user_input=None) -> FlowResult: | ||
"""Handle confirmation of reconfiguration.""" | ||
if user_input is not None: | ||
return await self.async_step_reconfigure() | ||
|
||
return self._show_config_form("reconfigure_confirm") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"title": "Meteo.Lt Integration", | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Configure Meteo.Lt", | ||
"description": "Set up Meteo.Lt integration with your Home Assistant.", | ||
"data": { | ||
"latitude": "Latitude", | ||
"longitude": "Longitude" | ||
} | ||
}, | ||
"reconfigure": { | ||
"title": "Reconfigure Meteo.Lt", | ||
"description": "Update your Meteo.Lt integration settings.", | ||
"data": { | ||
"latitude": "Latitude", | ||
"longitude": "Longitude" | ||
} | ||
}, | ||
"reconfigure_confirm": { | ||
"title": "Confirm Reconfiguration", | ||
"description": "Are you sure you want to reconfigure the Meteo.Lt integration?" | ||
} | ||
} | ||
} | ||
} |