Skip to content

Commit

Permalink
feat: config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
chilikla authored Oct 19, 2024
1 parent cf2dbf3 commit 33a5d8b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
26 changes: 26 additions & 0 deletions custom_components/yerushamayim/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""The Yerushamayim integration."""
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant

from .const import DOMAIN

PLATFORMS: list[Platform] = [Platform.WEATHER, Platform.SENSOR]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Yerushamayim from a config entry."""
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry.data

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
52 changes: 9 additions & 43 deletions custom_components/yerushamayim/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,15 @@

from typing import Any

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN

STEP_USER_DATA_SCHEMA = vol.Schema({})

async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
"""Validate the user input allows us to connect."""
# TODO: Validate the data can be used to set up a connection.

# If your PyPI package is not built with async, pass your methods
# to the executor:
# await hass.async_add_executor_job(
# your_validate_func, data["username"], data["password"]
# )

# If you cannot connect:
# throw CannotConnect

# If the authentication is wrong:
# throw InvalidAuth

# Return info that you want to store in the config entry.
# TODO: Add any validation logic here if needed in the future
return {"title": "Yerushamayim Weather"}

class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
Expand All @@ -44,29 +25,14 @@ async def async_step_user(
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA
step_id="user",
data_schema=None,
description_placeholders={
"name": "Yerushamayim"
},
)

errors = {}

try:
info = await validate_input(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_create_entry(title=info["title"], data=user_input)

return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)

class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""
await self.async_set_unique_id(DOMAIN)
self._abort_if_unique_id_configured()

class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""
return self.async_create_entry(title="Yerushamayim Weather", data={})
3 changes: 2 additions & 1 deletion custom_components/yerushamayim/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"domain": "yerushamayim",
"name": "Yerushamayim Weather",
"config_flow": true,
"integration_type": "hub",
"documentation": "https://github.com/chilikla/yerushamayim",
"issue_tracker": "https://github.com/chilikla/yerushamayim/issues",
"requirements": ["beautifulsoup4==4.10.0"],
Expand All @@ -12,5 +13,5 @@
"after_dependencies": ["rest"],
"codeowners": ["chilikla"],
"iot_class": "cloud_polling",
"version": "0.0.14"
"version": "0.0.0"
}
14 changes: 14 additions & 0 deletions custom_components/yerushamayim/strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Yerushamayim Weather",
"config": {
"step": {
"user": {
"title": "[%key:component::yerushamayim::title%]",
"description": "Do you want to add Yerushamayim to Home Assistant?"
}
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
}
}
}

0 comments on commit 33a5d8b

Please sign in to comment.