Skip to content

Commit

Permalink
Merge pull request #56 from RobertD502/add_polling_option
Browse files Browse the repository at this point in the history
Add polling option
  • Loading branch information
RobertD502 authored Jan 1, 2025
2 parents 446450f + 1c327f0 commit 0caf9a2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 22 deletions.
29 changes: 24 additions & 5 deletions custom_components/coway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DOMAIN,
LOGGER,
PLATFORMS,
POLLING_INTERVAL,
SKIP_PASSWORD_CHANGE,
UPDATE_LISTENER,
)
Expand Down Expand Up @@ -53,25 +54,43 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
password = entry.data[CONF_PASSWORD]

LOGGER.debug(f'Migrating Coway config entry unique id to {username}')
entry.version = 3

hass.config_entries.async_update_entry(
entry,
version=4,
data={
CONF_USERNAME: username,
CONF_PASSWORD: password,
},
options={SKIP_PASSWORD_CHANGE: False},
options={
SKIP_PASSWORD_CHANGE: False,
POLLING_INTERVAL: 120
},
unique_id=username,
)
if entry.version == 2:
LOGGER.debug('Migrating Coway config and disabling skipping password change.')
entry.version = 3
LOGGER.debug('Migrating Coway config: disabling skipping password change and setting polling interval of 120.')

hass.config_entries.async_update_entry(
entry,
options={SKIP_PASSWORD_CHANGE: False},
version=4,
options={
SKIP_PASSWORD_CHANGE: False,
POLLING_INTERVAL: 120
},
)
if entry.version == 3:
LOGGER.debug('Migrating Coway config and setting polling interval to 120 seconds.')

hass.config_entries.async_update_entry(
entry,
version=4,
options={
SKIP_PASSWORD_CHANGE: entry.options[SKIP_PASSWORD_CHANGE],
POLLING_INTERVAL: 120
},
)

return True

async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
Expand Down
26 changes: 17 additions & 9 deletions custom_components/coway/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from homeassistant.helpers.aiohttp_client import async_create_clientsession
import homeassistant.helpers.config_validation as cv

from .const import DEFAULT_NAME, DOMAIN, SKIP_PASSWORD_CHANGE
from .const import DEFAULT_NAME, DOMAIN, POLLING_INTERVAL, SKIP_PASSWORD_CHANGE
from .util import async_validate_api, NoPurifiersError


Expand All @@ -30,7 +30,7 @@
class CowayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Coway integration."""

VERSION = 3
VERSION = 4

entry: config_entries.ConfigEntry | None

Expand All @@ -40,7 +40,7 @@ def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> CowayOptionsFlowHandler:
"""Get the options flow for this handler."""
return CowayOptionsFlowHandler(config_entry)
return CowayOptionsFlowHandler()

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Handle re-authentication with Coway."""
Expand Down Expand Up @@ -80,7 +80,10 @@ async def async_step_reauth_confirm(
CONF_USERNAME: username,
CONF_PASSWORD: password,
},
options={SKIP_PASSWORD_CHANGE: skip_password_change},
options={
SKIP_PASSWORD_CHANGE: skip_password_change,
POLLING_INTERVAL: self.entry.options[POLLING_INTERVAL],
},
)
await self.hass.config_entries.async_reload(self.entry.entry_id)
return self.async_abort(reason="reauth_successful")
Expand Down Expand Up @@ -123,7 +126,10 @@ async def async_step_user(
CONF_USERNAME: username,
CONF_PASSWORD: password,
},
options={SKIP_PASSWORD_CHANGE: skip_password_change},
options={
SKIP_PASSWORD_CHANGE: skip_password_change,
POLLING_INTERVAL: 120,
},
)

return self.async_show_form(
Expand All @@ -136,10 +142,6 @@ async def async_step_user(
class CowayOptionsFlowHandler(config_entries.OptionsFlow):
""" Handle Coway account options. """

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
""" Manage options. """
return await self.async_step_coway_account_settings()
Expand All @@ -156,6 +158,12 @@ async def async_step_coway_account_settings(self, user_input=None):
SKIP_PASSWORD_CHANGE, False
),
): bool,
vol.Required(
POLLING_INTERVAL,
default=self.config_entry.options.get(
POLLING_INTERVAL, 120
),
): int,
}

return self.async_show_form(step_id="coway_account_settings", data_schema=vol.Schema(options))
4 changes: 2 additions & 2 deletions custom_components/coway/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

LOGGER = logging.getLogger(__package__)

DEFAULT_SCAN_INTERVAL = 30
TIMEOUT = 20
DEFAULT_NAME = "Coway IoCare"
DOMAIN = "coway"

COWAY_COORDINATOR = "coway_coordinator"
UPDATE_LISTENER = "update_listener"
POLLING_INTERVAL = "polling_interval"
SKIP_PASSWORD_CHANGE = "skip_password_change"
UPDATE_LISTENER = "update_listener"

PLATFORMS = [
Platform.FAN,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/coway/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, SKIP_PASSWORD_CHANGE, TIMEOUT
from .const import DOMAIN, LOGGER, POLLING_INTERVAL, SKIP_PASSWORD_CHANGE, TIMEOUT


class CowayDataUpdateCoordinator(DataUpdateCoordinator):
Expand All @@ -37,7 +37,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
hass,
LOGGER,
name=DOMAIN,
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
update_interval=timedelta(seconds=entry.options[POLLING_INTERVAL]),
)

async def _async_update_data(self) -> PurifierData:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/coway/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"requirements": [
"cowayaio==0.1.12"
],
"version": "0.4.3.3"
"version": "0.4.4"
}
3 changes: 2 additions & 1 deletion custom_components/coway/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"coway_account_settings": {
"description": "Options for Coway IoCare Account",
"data": {
"skip_password_change": "Skip password change"
"skip_password_change": "Skip password change",
"polling_interval": "Polling interval (seconds)"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/coway/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"step": {
"coway_account_settings": {
"data": {
"skip_password_change": "Skip password change"
"skip_password_change": "Skip password change",
"polling_interval": "Polling interval (seconds)"
},
"description": "Options for Coway IoCare Account"
}
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Coway IoCare",
"render_readme": true,
"country": "US",
"homeassistant": "2024.8.0b0",
"homeassistant": "2024.12.0b0",
"zip_release": true,
"filename": "coway.zip"
}

0 comments on commit 0caf9a2

Please sign in to comment.