Fix netatmo webhook registration issue (#32994)

* Wait for cloud connection

* Just wait

* Remove redundant entry

* Drop webhook before unloading other platforms

* Add missing scope

* Update homeassistant/components/netatmo/__init__.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Fix test

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
cgtobi 2020-03-20 15:22:27 +01:00 committed by GitHub
parent 6ab14a3729
commit eb77b94315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 19 deletions

View File

@ -28,6 +28,7 @@ from . import api, config_flow
from .const import ( from .const import (
AUTH, AUTH,
CONF_CLOUDHOOK_URL, CONF_CLOUDHOOK_URL,
DATA_DEVICE_IDS,
DATA_PERSONS, DATA_PERSONS,
DOMAIN, DOMAIN,
OAUTH2_AUTHORIZE, OAUTH2_AUTHORIZE,
@ -65,6 +66,7 @@ async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Netatmo component.""" """Set up the Netatmo component."""
hass.data[DOMAIN] = {} hass.data[DOMAIN] = {}
hass.data[DOMAIN][DATA_PERSONS] = {} hass.data[DOMAIN][DATA_PERSONS] = {}
hass.data[DOMAIN][DATA_DEVICE_IDS] = {}
if DOMAIN not in config: if DOMAIN not in config:
return True return True
@ -104,7 +106,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID]) webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
async def register_webhook(event): async def register_webhook(event):
# Wait for the could integration to be ready # Wait for the cloud integration to be ready
await asyncio.sleep(WAIT_FOR_CLOUD) await asyncio.sleep(WAIT_FOR_CLOUD)
if CONF_WEBHOOK_ID not in entry.data: if CONF_WEBHOOK_ID not in entry.data:
@ -112,6 +114,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.config_entries.async_update_entry(entry, data=data) hass.config_entries.async_update_entry(entry, data=data)
if hass.components.cloud.async_active_subscription(): if hass.components.cloud.async_active_subscription():
# Wait for cloud connection to be established
await asyncio.sleep(WAIT_FOR_CLOUD)
if CONF_CLOUDHOOK_URL not in entry.data: if CONF_CLOUDHOOK_URL not in entry.data:
webhook_url = await hass.components.cloud.async_create_cloudhook( webhook_url = await hass.components.cloud.async_create_cloudhook(
entry.data[CONF_WEBHOOK_ID] entry.data[CONF_WEBHOOK_ID]
@ -144,6 +149,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
if CONF_WEBHOOK_ID in entry.data:
await hass.async_add_executor_job(
hass.data[DOMAIN][entry.entry_id][AUTH].dropwebhook
)
unload_ok = all( unload_ok = all(
await asyncio.gather( await asyncio.gather(
*[ *[
@ -152,14 +162,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
] ]
) )
) )
if unload_ok: if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)
if CONF_WEBHOOK_ID in entry.data:
await hass.async_add_executor_job(
hass.data[DOMAIN][entry.entry_id][AUTH].dropwebhook()
)
return unload_ok return unload_ok

View File

@ -84,21 +84,11 @@ class NetatmoCamera(Camera):
self._unique_id = f"{self._camera_id}-{self._camera_type}" self._unique_id = f"{self._camera_id}-{self._camera_type}"
self._verify_ssl = verify_ssl self._verify_ssl = verify_ssl
self._quality = quality self._quality = quality
# URLs
self._vpnurl = None self._vpnurl = None
self._localurl = None self._localurl = None
# Monitoring status
self._status = None self._status = None
# SD Card status
self._sd_status = None self._sd_status = None
# Power status
self._alim_status = None self._alim_status = None
# Is local
self._is_local = None self._is_local = None
def camera_image(self): def camera_image(self):
@ -219,8 +209,6 @@ class NetatmoCamera(Camera):
def update(self): def update(self):
"""Update entity status.""" """Update entity status."""
# Refresh camera data
self._data.update() self._data.update()
camera = self._data.camera_data.get_camera(cid=self._camera_id) camera = self._data.camera_data.get_camera(cid=self._camera_id)

View File

@ -33,6 +33,7 @@ class NetatmoFlowHandler(
"read_station", "read_station",
"read_thermostat", "read_thermostat",
"write_camera", "write_camera",
"write_presence",
"write_thermostat", "write_thermostat",
] ]

View File

@ -14,12 +14,12 @@ MODELS = {
"NOC": "Smart Outdoor Camera", "NOC": "Smart Outdoor Camera",
"NSD": "Smart Smoke Alarm", "NSD": "Smart Smoke Alarm",
"NACamDoorTag": "Smart Door and Window Sensors", "NACamDoorTag": "Smart Door and Window Sensors",
"NHC": "Smart Indoor Air Quality Monitor",
"NAMain": "Smart Home Weather station indoor module", "NAMain": "Smart Home Weather station indoor module",
"NAModule1": "Smart Home Weather station outdoor module", "NAModule1": "Smart Home Weather station outdoor module",
"NAModule4": "Smart Additional Indoor module", "NAModule4": "Smart Additional Indoor module",
"NAModule3": "Smart Rain Gauge", "NAModule3": "Smart Rain Gauge",
"NAModule2": "Smart Anemometer", "NAModule2": "Smart Anemometer",
"NHC": "Home Coach",
} }
AUTH = "netatmo_auth" AUTH = "netatmo_auth"
@ -32,6 +32,7 @@ CONF_CLOUDHOOK_URL = "cloudhook_url"
OAUTH2_AUTHORIZE = "https://api.netatmo.com/oauth2/authorize" OAUTH2_AUTHORIZE = "https://api.netatmo.com/oauth2/authorize"
OAUTH2_TOKEN = "https://api.netatmo.com/oauth2/token" OAUTH2_TOKEN = "https://api.netatmo.com/oauth2/token"
DATA_DEVICE_IDS = "netatmo_device_ids"
DATA_PERSONS = "netatmo_persons" DATA_PERSONS = "netatmo_persons"
NETATMO_WEBHOOK_URL = None NETATMO_WEBHOOK_URL = None

View File

@ -65,6 +65,7 @@ async def test_full_flow(hass, aiohttp_client, aioclient_mock):
"read_station", "read_station",
"read_thermostat", "read_thermostat",
"write_camera", "write_camera",
"write_presence",
"write_thermostat", "write_thermostat",
] ]
) )