Skip to content

Commit

Permalink
Tweaks for US API support
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpuk committed Jul 10, 2022
1 parent de10581 commit c67160d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ This custom component integrates with the Bestway cloud API, providing control o

You must have an account with the Bestway mobile app ([Android][bestway-android]/[iOS][bestway-ios]). Lay-Z-Spa app credentials will not work. Both apps appear to have identical feature sets.

Bestway uses different API endpoints for EU and US. If you get an error stating account could not be found, try using the other api. If this does not help, then create a new account under a supported country.

Confirmed working countries:

* Germany (euapi)
* United Kingdom (euapi)
Bestway uses different API endpoints for EU and US. If you get an error stating account could not be found, try using the other endpoint. If this does not help, then create a new account under a supported country.


## Device Support
Expand Down
21 changes: 21 additions & 0 deletions custom_components/bestway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .bestway import BestwayApi, BestwayDeviceReport
from .const import (
CONF_API_ROOT,
CONF_API_ROOT_EU,
CONF_PASSWORD,
CONF_USER_TOKEN,
CONF_USER_TOKEN_EXPIRY,
Expand Down Expand Up @@ -90,6 +91,26 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
await async_setup_entry(hass, entry)


async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrates old config versions to the latest."""

_LOGGER.debug("Migrating from version %s", entry.version)

if entry.version == 1:
# API root needs to be set
# In version 1, this was hard coded to the EU endpoint
new = {**entry.data}
new[CONF_API_ROOT] = CONF_API_ROOT_EU
entry.version = 2
hass.config_entries.async_update_entry(entry, data=new)

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

_LOGGER.error("Existing schema version %s is not supported", entry.version)
return False


class BestwayUpdateCoordinator(DataUpdateCoordinator[dict[str, BestwayDeviceReport]]):
"""Update coordinator that polls the device status for all devices in an account."""

Expand Down
30 changes: 18 additions & 12 deletions custom_components/bestway/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import selector
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import selector
import voluptuous as vol

from custom_components.bestway.bestway import (
Expand All @@ -32,14 +32,20 @@
)

_LOGGER = getLogger(__name__)


def _get_user_data_schema():
data_schema = {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
data_schema[CONF_API_ROOT] = selector(
{"select": {"options": [CONF_API_ROOT_EU, CONF_API_ROOT_US]}}
)
return vol.Schema(data_schema)
_STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_API_ROOT): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
selector.SelectOptionDict(value=CONF_API_ROOT_EU, label="EU"),
selector.SelectOptionDict(value=CONF_API_ROOT_US, label="US"),
]
)
),
}
)


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
Expand All @@ -66,15 +72,15 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
class BestwayConfigFlow(ConfigFlow, domain=DOMAIN): # type: ignore[call-arg]
"""Handle a config flow for bestway."""

VERSION = 1
VERSION = 2

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=_get_user_data_schema()
step_id="user", data_schema=_STEP_USER_DATA_SCHEMA
)

errors = {}
Expand All @@ -94,7 +100,7 @@ async def async_step_user(
return self.async_create_entry(title=info["title"], data=user_input)

return self.async_show_form(
step_id="user", data_schema=_get_user_data_schema(), errors=errors
step_id="user", data_schema=_STEP_USER_DATA_SCHEMA, errors=errors
)


Expand Down
1 change: 1 addition & 0 deletions custom_components/bestway/entity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Home Assistant entity descriptions."""
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down
3 changes: 2 additions & 1 deletion custom_components/bestway/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"title": "Bestway Login",
"data": {
"username": "Username (e-mail address)",
"password": "Password"
"password": "Password",
"apiroot": "API location"
}
}
},
Expand Down

2 comments on commit c67160d

@johnthackers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi do they have a plug-in / integration for Domoticz instead of Ha

@cdpuk
Copy link
Owner Author

@cdpuk cdpuk commented on c67160d Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi do they have a plug-in / integration for Domoticz instead of Ha

This is a custom (community driven) project for HA integration only. I'm not familiar with Domoticz but I guess someone would need to create something similar.

Please sign in to comment.