Skip to content

Commit

Permalink
Configuration and translations WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunas committed Jul 22, 2024
1 parent d2d864b commit b938772
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"*.yaml": "home-assistant"
}
}
92 changes: 76 additions & 16 deletions custom_components/meteo_lt/config_flow.py
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")
4 changes: 2 additions & 2 deletions custom_components/meteo_lt/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"domain": "meteo_lt",
"name": "Meteo.LT",
"version": "0.1.3",
"version": "0.1.4",
"codeowners": ["@Brunas"],
"config_flow": true,
"documentation": "https://github.com/Brunas/meteo_lt",
"iot_class": "cloud_polling",
"integration_type": "device",
"integration_type": "service",
"loggers": ["meteo_lt"],
"requirements": ["meteo_lt-pkg==0.1.4"],
"dependencies": [],
Expand Down
27 changes: 27 additions & 0 deletions custom_components/meteo_lt/translations/en.json
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?"
}
}
}
}

0 comments on commit b938772

Please sign in to comment.