Skip to content

Commit

Permalink
Rework of config flow: First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jul 4, 2023
1 parent 5015af0 commit 5866750
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 302 deletions.
36 changes: 14 additions & 22 deletions custom_components/dwd_weather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
Expand All @@ -23,7 +22,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS = ["sensor", "weather"]
PLATFORMS = ["weather", "sensor"]


async def async_setup(hass: HomeAssistant, config: Config) -> bool:
Expand All @@ -33,47 +32,34 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up DWD Weather as config entry."""
_LOGGER.debug("Setup with data {}".format(entry.data))

# Load values from settings
latitude = entry.data[CONF_LATITUDE]
longitude = entry.data[CONF_LONGITUDE]
site_name = entry.data[CONF_NAME]
weather_interval = entry.data[CONF_WEATHER_INTERVAL]
wind_direction_type = entry.data[CONF_WIND_DIRECTION_TYPE]
station_id = entry.data[CONF_STATION_ID]

dwd_weather_data = DWDWeatherData(
hass, latitude, longitude, station_id, weather_interval, wind_direction_type
)

# Update data initially
# await dwd_weather_data.async_update()
# if dwd_weather_data.weather_data.get_station_name(False) == '':
# raise ConfigEntryNotReady()
# TODO only if station was configured
dwd_weather_data = DWDWeatherData(hass, entry)

# Coordinator checks for new updates
dwdweather_coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=f"DWD Weather Coordinator for {site_name}",
name=f"DWD Weather Coordinator for {entry.data[CONF_STATION_ID]}",
update_method=dwd_weather_data.async_update,
update_interval=DEFAULT_SCAN_INTERVAL,
)

# Fetch initial data so we have data when entities subscribe
await dwdweather_coordinator.async_refresh()
if dwd_weather_data.dwd_weather.get_station_name == "":
if dwd_weather_data.dwd_weather.forecast_data is None:
_LOGGER.debug("ConfigEntryNotReady")
raise ConfigEntryNotReady()

# Save the data
dwdweather_hass_data = hass.data.setdefault(DOMAIN, {})
dwdweather_hass_data[entry.entry_id] = {
DWDWEATHER_DATA: dwd_weather_data,
DWDWEATHER_COORDINATOR: dwdweather_coordinator,
CONF_WEATHER_INTERVAL: weather_interval,
CONF_WIND_DIRECTION_TYPE: wind_direction_type,
}

# Setup weather and sensor platforms
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
Expand All @@ -98,6 +84,12 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
config_entry.data = {**new}
config_entry.version = 3

if config_entry.version == 3:
new = {**config_entry.data}
# TODO altes Format in neues uebersetzen
config_entry.data = {**new}
config_entry.version = 4

_LOGGER.info("Migration to version %s successful", config_entry.version)
return True

Expand Down
Loading

0 comments on commit 5866750

Please sign in to comment.