Skip to content

Commit

Permalink
fix: handle unit of measure toggle option (#15)
Browse files Browse the repository at this point in the history
* fix: handle unit of measure toggle option

* formatting
  • Loading branch information
firstof9 authored Dec 13, 2023
1 parent b6d7a94 commit 8f210ec
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
32 changes: 32 additions & 0 deletions custom_components/gasbuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .const import (
CONF_INTERVAL,
CONF_STATION_ID,
CONF_UOM,
COORDINATOR,
DOMAIN,
ISSUE_URL,
Expand All @@ -43,6 +44,14 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
ISSUE_URL,
)

# Some sanity checks
updated_config = config_entry.data.copy()
if CONF_UOM not in config_entry.data.keys():
updated_config[CONF_UOM] = True

if updated_config != config_entry.data:
hass.config_entries.async_update_entry(config_entry, data=updated_config)

config_entry.add_update_listener(update_listener)
interval = config_entry.data.get(CONF_INTERVAL)
coordinator = GasBuddyUpdateCoordinator(hass, interval, config_entry)
Expand Down Expand Up @@ -72,6 +81,7 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non
return

config_entry.data[CONF_INTERVAL] = config_entry.options[CONF_INTERVAL]
config_entry.data[CONF_UOM] = config_entry.options[CONF_UOM]

hass.config_entries.async_update_entry(
entry=config_entry,
Expand All @@ -81,6 +91,28 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non
await hass.config_entries.async_reload(config_entry.entry_id)


async def async_migrate_entry(hass, config_entry) -> bool:
"""Migrate an old config entry."""
version = config_entry.version

# 1 -> 2: Migrate format
if version == 1:
_LOGGER.debug("Migrating from version %s", version)
updated_config = config_entry.data.copy()

# Add default unit of measure setting if missing
if CONF_UOM not in updated_config.keys():
updated_config[CONF_UOM] = True

if updated_config != config_entry.data:
hass.config_entries.async_update_entry(config_entry, data=updated_config)

config_entry.version = 4
_LOGGER.debug("Migration to version %s complete", config_entry.version)

return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
_LOGGER.debug("Attempting to unload entities from the %s integration", DOMAIN)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/gasbuddy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
class GasBuddyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for GasBuddy."""

VERSION = 1
VERSION = 2
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

def __init__(self):
Expand Down
1 change: 0 additions & 1 deletion tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
CONF_NAME: "Gas Station",
CONF_INTERVAL: 3600,
CONF_STATION_ID: 208656,
CONF_UOM: True,
}

STATION_LIST = {
Expand Down
4 changes: 1 addition & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
async def test_setup_and_unload_entry(hass, mock_gasbuddy):
"""Test setup_entry."""
entry = MockConfigEntry(
domain=DOMAIN,
title="gas_station",
data=CONFIG_DATA,
domain=DOMAIN, title="gas_station", data=CONFIG_DATA, version=1
)

entry.add_to_hass(hass)
Expand Down

0 comments on commit 8f210ec

Please sign in to comment.