Skip to content

Commit

Permalink
Fix configuration data/options (related to #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ylabonte committed Feb 11, 2024
1 parent b6d8337 commit 877deea
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 91 deletions.
76 changes: 68 additions & 8 deletions custom_components/proconip_pool_controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
For more details about this integration, please refer to
https://github.com/ylabonte/proconip-hass
"""

from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_NAME,
CONF_URL,
CONF_USERNAME,
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
Platform,
)
from homeassistant.core import HomeAssistant

from .api import ProconipApiClient
from .const import DOMAIN
from .const import DOMAIN, LOGGER
from .coordinator import ProconipPoolControllerDataUpdateCoordinator

PLATFORMS: list[Platform] = [
Expand Down Expand Up @@ -46,20 +48,78 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities
await hass.data[DOMAIN][entry.entry_id].async_config_entry_first_refresh()

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
await hass.config_entries.async_forward_entry_setups(
entry=entry, platforms=PLATFORMS
)
entry.async_on_unload(func=entry.add_update_listener(listener=async_reload_entry))

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
if unloaded := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
if unloaded := await hass.config_entries.async_unload_platforms(
entry=entry, platforms=PLATFORMS
):
hass.data[DOMAIN].pop(entry.entry_id)
return unloaded


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_unload_entry(hass=hass, entry=entry)
await async_setup_entry(hass=hass, entry=entry)


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

if config_entry.version > 2:
# This means the user has downgraded from a future version
return False

if config_entry.version == 1:
if config_entry.minor_version < 2:
newData = {**config_entry.data}
newOptions = {**config_entry.options}
"""Seperate data and options that were accidentially mixed up."""
newData[CONF_NAME] = (
newData[CONF_NAME] if CONF_NAME in newData else config_entry.title
)
newOptions[CONF_URL] = (
newOptions[CONF_URL] if CONF_URL in newOptions else newData[CONF_URL]
)
newOptions[CONF_USERNAME] = (
newOptions[CONF_USERNAME]
if CONF_USERNAME in newOptions
else newData[CONF_USERNAME]
)
newOptions[CONF_PASSWORD] = (
newOptions[CONF_PASSWORD]
if CONF_PASSWORD in newOptions
else newData[CONF_PASSWORD]
)
newOptions[CONF_SCAN_INTERVAL] = (
newOptions[CONF_SCAN_INTERVAL]
if CONF_SCAN_INTERVAL in newOptions
else newData[CONF_SCAN_INTERVAL] if CONF_SCAN_INTERVAL in newData else 3
)
if CONF_URL in newData:
del newData[CONF_URL]
if CONF_USERNAME in newData:
del newData[CONF_USERNAME]
if CONF_PASSWORD in newData:
del newData[CONF_PASSWORD]
if CONF_SCAN_INTERVAL in newData:
del newData[CONF_SCAN_INTERVAL]

config_entry.version = 1
config_entry.minor_version = 2
hass.config_entries.async_update_entry(
entry=config_entry, data=newData, options=newOptions
)

LOGGER.debug("Migration to version %s successful", config_entry.version)

return True
35 changes: 31 additions & 4 deletions custom_components/proconip_pool_controller/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""ProCon.IP API Client."""

from __future__ import annotations

from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -31,15 +32,21 @@ def __init__(
self._base_url = base_url
self._username = username
self._password = password
self._session = async_get_clientsession(hass)
self._session = async_get_clientsession(hass=hass)
self._api_config = ConfigObject(
base_url=self._base_url,
username=self._username,
password=self._password,
)
self._get_state_api = GetState(self._session, self._api_config)
self._relay_switch_api = RelaySwitch(self._session, self._api_config)
self._dosage_control_api = DosageControl(self._session, self._api_config)
self._get_state_api = GetState(
client_session=self._session, config=self._api_config
)
self._relay_switch_api = RelaySwitch(
client_session=self._session, config=self._api_config
)
self._dosage_control_api = DosageControl(
client_session=self._session, config=self._api_config
)
self._most_recent_data = None

async def async_get_data(
Expand Down Expand Up @@ -120,3 +127,23 @@ async def async_start_ph_plus_dosage(
return await self._dosage_control_api.async_ph_plus_dosage(
dosage_duration=duration_in_seconds,
)


class ProconipConnectionTester:
"""Helper class for connection testing."""

def __init__(self, hass: HomeAssistant) -> None:
"""Initialize connection tester."""
self.hass = hass

async def async_test_credentials(
self, url: str, username: str, password: str
) -> None:
"""Validate base url and credentials."""
client = ProconipApiClient(
base_url=url,
username=username,
password=password,
hass=self.hass,
)
await client.async_get_data()
Loading

0 comments on commit 877deea

Please sign in to comment.