Use Cloudhooks for OwnTracks (#23847)

* Use Cloudhooks for OwnTracks

* Update config_flow.py

* Update config_flow.py
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-05-14 11:59:11 +02:00
1 parent 19cfa8cf22
commit 6fa8556033
4 files changed
+101 -18

No files matched your search

@@ -91,6 +91,24 @@ async def async_setup_entry(hass, entry):
return True
async def async_unload_entry(hass, entry):
"""Unload an OwnTracks config entry."""
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
await hass.config_entries.async_forward_entry_unload(
entry, 'device_tracker')
return True
async def async_remove_entry(hass, entry):
"""Remove an OwnTracks config entry."""
if (not entry.data.get('cloudhook') or
'cloud' not in hass.config.components):
return
await hass.components.cloud.async_delete_cloudhook(
entry.data[CONF_WEBHOOK_ID])
async def async_connect_mqtt(hass, component):
"""Subscribe to MQTT topic."""
context = hass.data[DOMAIN]['context']
@@ -4,6 +4,7 @@ from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.auth.util import generate_secret
CONF_SECRET = 'secret'
CONF_CLOUDHOOK = 'cloudhook'
def supports_encryption():
@@ -31,9 +32,7 @@ class OwnTracksFlow(config_entries.ConfigFlow):
step_id='user',
)
webhook_id = self.hass.components.webhook.async_generate_id()
webhook_url = \
self.hass.components.webhook.async_generate_url(webhook_id)
webhook_id, webhook_url, cloudhook = await self._get_webhook_id()
secret = generate_secret(16)
@@ -50,7 +49,8 @@ class OwnTracksFlow(config_entries.ConfigFlow):
title="OwnTracks",
data={
CONF_WEBHOOK_ID: webhook_id,
CONF_SECRET: secret
CONF_SECRET: secret,
CONF_CLOUDHOOK: cloudhook,
},
description_placeholders={
'secret': secret_desc,
@@ -67,12 +67,29 @@ class OwnTracksFlow(config_entries.ConfigFlow):
async def async_step_import(self, user_input):
"""Import a config flow from configuration."""
webhook_id = self.hass.components.webhook.async_generate_id()
webhook_id, _webhook_url, cloudhook = await self._get_webhook_id()
secret = generate_secret(16)
return self.async_create_entry(
title="OwnTracks",
data={
CONF_WEBHOOK_ID: webhook_id,
CONF_SECRET: secret
CONF_SECRET: secret,
CONF_CLOUDHOOK: cloudhook,
}
)
async def _get_webhook_id(self):
"""Generate webhook ID."""
webhook_id = self.hass.components.webhook.async_generate_id()
if self.hass.components.cloud.async_active_subscription():
webhook_url = \
await self.hass.components.cloud.async_create_cloudhook(
webhook_id
)
cloudhook = True
else:
webhook_url = \
self.hass.components.webhook.async_generate_url(webhook_id)
cloudhook = False
return webhook_id, webhook_url, cloudhook