diff --git a/custom_components/dummy_tracker/__init__.py b/custom_components/dummy_tracker/__init__.py index a963b2b..420fec1 100644 --- a/custom_components/dummy_tracker/__init__.py +++ b/custom_components/dummy_tracker/__init__.py @@ -5,13 +5,21 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant +DOMAIN = "dummy_tracker" PLATFORMS: list[Platform] = [Platform.DEVICE_TRACKER] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Dummy Device Tracker from a config entry.""" + # Register update listener + entry.async_on_unload(entry.add_update_listener(update_listener)) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True +async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Handle options update.""" + await hass.config_entries.async_reload(entry.entry_id) + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) \ No newline at end of file diff --git a/custom_components/dummy_tracker/config_flow.py b/custom_components/dummy_tracker/config_flow.py index be3ebbc..bf56ab6 100644 --- a/custom_components/dummy_tracker/config_flow.py +++ b/custom_components/dummy_tracker/config_flow.py @@ -12,6 +12,8 @@ DOMAIN = "dummy_tracker" class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): + """Handle a config flow for Dummy Device Tracker.""" + VERSION = 1 async def async_step_user( @@ -51,3 +53,9 @@ async def async_step_user( } ), ) + + @staticmethod + def async_get_options_flow(config_entry): + """Create the options flow.""" + from .options_flow import OptionsFlowHandler + return OptionsFlowHandler(config_entry) \ No newline at end of file diff --git a/custom_components/dummy_tracker/options_flow.py b/custom_components/dummy_tracker/options_flow.py new file mode 100644 index 0000000..a3c1926 --- /dev/null +++ b/custom_components/dummy_tracker/options_flow.py @@ -0,0 +1,58 @@ +"""Options flow for Dummy Device Tracker integration.""" +from __future__ import annotations + +from typing import Any + +import voluptuous as vol + +from homeassistant import config_entries +from homeassistant.data_entry_flow import FlowResult +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_LOCATION +from homeassistant.helpers.selector import LocationSelector + +DOMAIN = "dummy_tracker" + +class OptionsFlowHandler(config_entries.OptionsFlow): + """Handle options flow for Dummy Device Tracker.""" + + def __init__(self, config_entry): + """Initialize options flow.""" + self.config_entry = config_entry + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: + """Manage the options.""" + if user_input is not None: + lat: float = user_input[CONF_LOCATION][CONF_LATITUDE] + lon: float = user_input[CONF_LOCATION][CONF_LONGITUDE] + + # Preserve the original entity name + entity_name = self.config_entry.data.get("entity_name", f"location_{lat}_{lon}") + + return self.async_create_entry( + title=entity_name, + data={ + CONF_LATITUDE: lat, + CONF_LONGITUDE: lon, + "entity_name": entity_name, + } + ) + + # Get current location from config entry + current_location = { + CONF_LATITUDE: self.config_entry.data[CONF_LATITUDE], + CONF_LONGITUDE: self.config_entry.data[CONF_LONGITUDE], + } + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema( + { + vol.Required( + CONF_LOCATION, + default=current_location + ): LocationSelector(), + } + ), + ) \ No newline at end of file