Skip to content

Commit

Permalink
Updates to 1.0.9
Browse files Browse the repository at this point in the history
Bug fix in upgrade path and versioning
  • Loading branch information
ITSpecialist111 committed Nov 4, 2024
1 parent e64bf29 commit 787ed58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions custom_components/ai_automation_suggester/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# custom_components/ai_automation_suggester/__init__.py

"""The AI Automation Suggester integration."""
import logging
from homeassistant.config_entries import ConfigEntry
Expand All @@ -19,28 +17,36 @@

_LOGGER = logging.getLogger(__name__)

CURRENT_VERSION = 2 # Internal version number (1.0.9 = version 2)

async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version)

if config_entry.version == 1.07:
# Already up to date
return True
# Convert old decimal versions to new integer version
if isinstance(config_entry.version, float):
old_version = config_entry.version
if old_version <= 1.08:
config_entry.version = 1
_LOGGER.debug("Converted decimal version %s to integer version %s",
old_version, config_entry.version)

# Handle migration from version 1.06 or earlier
if config_entry.version <= 1.07:
if config_entry.version == 1:
_LOGGER.debug("Migrating config entry from version 1 to version 2")
new_data = {**config_entry.data}

# Add any new required fields with defaults
if "scan_frequency" not in new_data:
new_data["scan_frequency"] = DEFAULT_SCAN_FREQUENCY
if "initial_lag_time" not in new_data:
new_data["initial_lag_time"] = DEFAULT_INITIAL_LAG_TIME

config_entry.version = 1.08
# Remove old scheduling fields that are no longer used
new_data.pop('scan_frequency', None)
new_data.pop('initial_lag_time', None)

# Update to current version
config_entry.version = CURRENT_VERSION
hass.config_entries.async_update_entry(config_entry, data=new_data)

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

_LOGGER.debug("No migration required for version %s", config_entry.version)
return True

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
Expand All @@ -57,6 +63,7 @@ async def handle_generate_suggestions(call: ServiceCall) -> None:
if provider_config:
coordinator = hass.data[DOMAIN][provider_config]
else:
# Find first available coordinator if no specific one is specified
for entry_id, coord in hass.data[DOMAIN].items():
if isinstance(coord, AIAutomationCoordinator):
coordinator = coord
Expand Down Expand Up @@ -92,15 +99,12 @@ async def handle_generate_suggestions(call: ServiceCall) -> None:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up AI Automation Suggester from a config entry."""
try:
# Ensure required config values are present
if CONF_PROVIDER not in entry.data:
raise ConfigEntryNotReady("Provider not specified in config")

# Create and store coordinator
coordinator = AIAutomationCoordinator(hass, entry)
hass.data[DOMAIN][entry.entry_id] = coordinator

# Set up platforms
for platform in PLATFORMS:
try:
await hass.config_entries.async_forward_entry_setup(entry, platform)
Expand All @@ -125,11 +129,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
try:
# Unload platforms
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

if unload_ok:
# Clean up coordinator
coordinator = hass.data[DOMAIN].pop(entry.entry_id)
await coordinator.async_shutdown()

Expand All @@ -142,4 +144,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
await async_setup_entry(hass, entry)
2 changes: 1 addition & 1 deletion custom_components/ai_automation_suggester/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"pyyaml>=6.0",
"voluptuous>=0.13.1"
],
"version": "1.08"
"version": "1.0.9"
}

0 comments on commit 787ed58

Please sign in to comment.