Skip to content

Commit

Permalink
Use latest withings_api module (home-assistant#27817)
Browse files Browse the repository at this point in the history
* Using latest winthings_api module.
Drastically reduced complexity of tests.

* Removing import source.

* Fixing test requirements.

* Using requests_mock instead of responses module.

* Updating file formatting.

* Removing unused method.

* Adding support for new OAuth2 config flow.

* Addressing PR feedback.
Removing unecessary base_url from config, this is a potential breaking change.

* Addressing PR feedback.
  • Loading branch information
vangorra authored and balloob committed Oct 24, 2019
1 parent fc09702 commit 15bedd8
Show file tree
Hide file tree
Showing 16 changed files with 995 additions and 1,569 deletions.
75 changes: 45 additions & 30 deletions homeassistant/components/withings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
For more details about this platform, please refer to the documentation at
"""
import voluptuous as vol
from withings_api import WithingsAuth

from homeassistant.config_entries import ConfigEntry, SOURCE_IMPORT, SOURCE_USER
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, config_entry_oauth2_flow

from . import config_flow, const
from .common import _LOGGER, get_data_manager, NotAuthenticatedError
Expand All @@ -22,7 +23,6 @@
vol.Required(const.CLIENT_SECRET): vol.All(
cv.string, vol.Length(min=1)
),
vol.Optional(const.BASE_URL): cv.url,
vol.Required(const.PROFILES): vol.All(
cv.ensure_list,
vol.Unique(),
Expand All @@ -36,50 +36,65 @@
)


async def async_setup(hass: HomeAssistantType, config: ConfigType):
async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
"""Set up the Withings component."""
conf = config.get(DOMAIN)
conf = config.get(DOMAIN, {})
if not conf:
return True

hass.data[DOMAIN] = {const.CONFIG: conf}

base_url = conf.get(const.BASE_URL, hass.config.api.base_url).rstrip("/")

hass.http.register_view(config_flow.WithingsAuthCallbackView)

config_flow.register_flow_implementation(
config_flow.WithingsFlowHandler.async_register_implementation(
hass,
conf[const.CLIENT_ID],
conf[const.CLIENT_SECRET],
base_url,
conf[const.PROFILES],
)

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data={}
)
config_entry_oauth2_flow.LocalOAuth2Implementation(
hass,
const.DOMAIN,
conf[const.CLIENT_ID],
conf[const.CLIENT_SECRET],
f"{WithingsAuth.URL}/oauth2_user/authorize2",
f"{WithingsAuth.URL}/oauth2/token",
),
)

return True


async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
"""Set up Withings from a config entry."""
data_manager = get_data_manager(hass, entry)
# Upgrading existing token information to hass managed tokens.
if "auth_implementation" not in entry.data:
_LOGGER.debug("Upgrading existing config entry")
data = entry.data
creds = data.get(const.CREDENTIALS, {})
hass.config_entries.async_update_entry(
entry,
data={
"auth_implementation": const.DOMAIN,
"implementation": const.DOMAIN,
"profile": data.get("profile"),
"token": {
"access_token": creds.get("access_token"),
"refresh_token": creds.get("refresh_token"),
"expires_at": int(creds.get("token_expiry")),
"type": creds.get("token_type"),
"userid": creds.get("userid") or creds.get("user_id"),
},
},
)

implementation = await config_entry_oauth2_flow.async_get_config_entry_implementation(
hass, entry
)

data_manager = get_data_manager(hass, entry, implementation)

_LOGGER.debug("Confirming we're authenticated")
try:
await data_manager.check_authenticated()
except NotAuthenticatedError:
# Trigger new config flow.
hass.async_create_task(
hass.config_entries.flow.async_init(
const.DOMAIN,
context={"source": SOURCE_USER, const.PROFILE: data_manager.profile},
data={},
)
_LOGGER.error(
"Withings auth tokens exired for profile %s, remove and re-add the integration",
data_manager.profile,
)
return False

Expand All @@ -90,6 +105,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
return True


async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
"""Unload Withings config entry."""
return await hass.config_entries.async_forward_entry_unload(entry, "sensor")
Loading

0 comments on commit 15bedd8

Please sign in to comment.