Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config flow updates, settings for fetch intervals #52

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions custom_components/garo_wallbox/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
"""Config flow for the Garo Wallbox platform."""
import asyncio
import logging
from typing import Any, Dict, Optional

from aiohttp import ClientConnectionError
from async_timeout import timeout
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession


from .const import TIMEOUT
from .const import TIMEOUT, DOMAIN, CONF_DEVICE_FETCH_INTERVAL, CONF_METER_FETCH_INTERVAL, DEFAULT_DEVICE_FETCH_INTERVAL, DEFAULT_METER_FETCH_INTERVAL
from .garo import ApiClient
from . import GaroConfigEntry

_LOGGER = logging.getLogger(__name__)

@config_entries.HANDLERS.register("garo_wallbox")
class FlowHandler(config_entries.ConfigFlow):
class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

async def _create_entry(self, host, name):
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return GaroOptionsFlowHandler(config_entry)

async def _create_entry(self, host, name, device_fetch_interval = None, meter_fetch_interval = None):
"""Register new entry."""
# Check if ip already is registered
for entry in self._async_current_entries():
if entry.data[CONF_HOST] == host:
return self.async_abort(reason="already_configured")

return self.async_create_entry(title=host, data={CONF_HOST: host, CONF_NAME: name})
return self.async_create_entry(
title=host,
data={
CONF_HOST: host,
CONF_NAME: name,
},
options={
CONF_DEVICE_FETCH_INTERVAL: device_fetch_interval or DEFAULT_DEVICE_FETCH_INTERVAL,
CONF_METER_FETCH_INTERVAL: meter_fetch_interval or DEFAULT_METER_FETCH_INTERVAL
})

async def _create_device(self, host, name):
async def _create_device(self, host, name, device_fetch_interval = None, meter_fetch_interval= None):
"""Create device."""
session = async_get_clientsession(self.hass)
api_client = ApiClient(session, host)
try:
with timeout(TIMEOUT):
await api_client.async_get_configuration()
return await self._create_entry(host, name)
return await self._create_entry(host, name,device_fetch_interval, meter_fetch_interval)
except asyncio.TimeoutError:
_LOGGER.debug("Connection to %s timed out", host)
return self.async_abort(reason="device_timeout")
Expand All @@ -56,10 +73,22 @@ async def async_step_user(self, user_input=None):
return self.async_show_form(
step_id="user", data_schema=vol.Schema({
vol.Required(CONF_HOST): str,
vol.Optional(CONF_NAME): str
})
vol.Optional(CONF_NAME): str,
vol.Optional(
CONF_DEVICE_FETCH_INTERVAL,
default=DEFAULT_DEVICE_FETCH_INTERVAL,
): int,
vol.Optional(
CONF_METER_FETCH_INTERVAL,
default=DEFAULT_METER_FETCH_INTERVAL,
): int,
})
)
return await self._create_device(user_input[CONF_HOST], user_input[CONF_NAME])
return await self._create_device(
user_input[CONF_HOST],
user_input[CONF_NAME],
user_input[CONF_DEVICE_FETCH_INTERVAL],
user_input[CONF_METER_FETCH_INTERVAL])

async def async_step_import(self, user_input):
"""Import a config entry."""
Expand All @@ -72,3 +101,45 @@ async def async_step_discovery(self, user_input):
"""Initialize step from discovery."""
_LOGGER.info("Discovered device: %s", user_input)
return await self._create_entry(user_input[CONF_HOST], None)

class GaroOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle Garo options."""

def __init__(self, config_entry: GaroConfigEntry):
"""Initialize Garo options flow."""
self.config_entry = config_entry

async def async_step_init(
self, user_input: Optional[Dict[str, Any]] = None
):
"""Manage Garo options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
CONF_HOST,
default=self.config_entry.data.get(CONF_HOST),
): str,
vol.Optional(
CONF_NAME,
default=self.config_entry.data.get(CONF_NAME),
): str,
vol.Optional(
CONF_DEVICE_FETCH_INTERVAL,
default=self.config_entry.options.get(
CONF_DEVICE_FETCH_INTERVAL, DEFAULT_DEVICE_FETCH_INTERVAL
),
): int,
vol.Optional(
CONF_METER_FETCH_INTERVAL,
default=self.config_entry.options.get(
CONF_METER_FETCH_INTERVAL, DEFAULT_METER_FETCH_INTERVAL
),
): int,
}
),
)
16 changes: 15 additions & 1 deletion custom_components/garo_wallbox/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"description": "Enter IP address of your Garo Wallbox.",
"data": {
"host": "Host",
"name": "Friendly name"
"name": "Friendly name",
"device_fetch_interval": "Fetch interval (seconds)",
"meter_fetch_interval": "Meter fetch interval (seconds)"
}
}
},
Expand All @@ -16,6 +18,18 @@
"already_configured": "Device is already configured"
}
},
"options": {
"step": {
"init": {
"data": {
"host": "Host",
"name": "Friendly name",
"device_fetch_interval": "Fetch interval (seconds)",
"meter_fetch_interval": "Meter fetch interval (seconds)"
}
}
}
},
"entity": {
"sensor": {
"sensor": {
Expand Down
16 changes: 15 additions & 1 deletion custom_components/garo_wallbox/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"description": "Enter IP address of your Garo Wallbox.",
"data": {
"host": "Host",
"name": "Friendly name"
"name": "Friendly name",
"device_fetch_interval": "Fetch interval (seconds)",
"meter_fetch_interval": "Meter fetch interval (seconds)"
}
}
},
Expand All @@ -16,6 +18,18 @@
"already_configured": "Device is already configured"
}
},
"options": {
"step": {
"init": {
"data": {
"host": "Host",
"name": "Friendly name",
"device_fetch_interval": "Fetch interval (seconds)",
"meter_fetch_interval": "Meter fetch interval (seconds)"
}
}
}
},
"entity": {
"sensor": {
"sensor": {
Expand Down