Skip to content
Snippets Groups Projects
Commit 0c87fb42 authored by Rohan Kapoor's avatar Rohan Kapoor Committed by Fabian Affolter
Browse files

Load/unload locative entities correctly between component and platform (#20498)

* Embed device_tracker in locative

* Load/unload locative entities correctly between component and platform

* Await the coroutine directly

* Await the correct coroutine
parent f575d1d3
No related branches found
No related tags found
No related merge requests found
......@@ -12,11 +12,10 @@ from aiohttp import web
import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import \
DOMAIN as DEVICE_TRACKER_DOMAIN
DOMAIN as DEVICE_TRACKER
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, ATTR_LATITUDE, \
ATTR_LONGITUDE, STATE_NOT_HOME, CONF_WEBHOOK_ID, ATTR_ID, HTTP_OK
from homeassistant.helpers import config_entry_flow
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send
_LOGGER = logging.getLogger(__name__)
......@@ -57,9 +56,6 @@ WEBHOOK_SCHEMA = vol.All(
async def async_setup(hass, hass_config):
"""Set up the Locative component."""
hass.async_create_task(
async_load_platform(hass, 'device_tracker', DOMAIN, {}, hass_config)
)
return True
......@@ -93,7 +89,7 @@ async def handle_webhook(hass, webhook_id, request):
if direction == 'exit':
current_state = hass.states.get(
'{}.{}'.format(DEVICE_TRACKER_DOMAIN, device))
'{}.{}'.format(DEVICE_TRACKER, device))
if current_state is None or current_state.state == location_name:
location_name = STATE_NOT_HOME
......@@ -140,12 +136,18 @@ async def async_setup_entry(hass, entry):
"""Configure based on config entry."""
hass.components.webhook.async_register(
DOMAIN, 'Locative', entry.data[CONF_WEBHOOK_ID], handle_webhook)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, DEVICE_TRACKER)
)
return True
async def async_unload_entry(hass, entry):
"""Unload a 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
config_entry_flow.register_webhook_flow(
......
......@@ -6,6 +6,9 @@ https://home-assistant.io/components/device_tracker.locative/
"""
import logging
from homeassistant.components.device_tracker import \
DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.components.locative import DOMAIN as LOCATIVE_DOMAIN
from homeassistant.components.locative import TRACKER_UPDATE
from homeassistant.helpers.dispatcher import async_dispatcher_connect
......@@ -13,9 +16,11 @@ _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['locative']
DATA_KEY = '{}.{}'.format(LOCATIVE_DOMAIN, DEVICE_TRACKER_DOMAIN)
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Set up an endpoint for the Locative device tracker."""
async def async_setup_entry(hass, entry, async_see):
"""Configure a dispatcher connection based on a config entry."""
async def _set_location(device, gps_location, location_name):
"""Fire HA event to set location."""
await async_see(
......@@ -24,5 +29,13 @@ async def async_setup_scanner(hass, config, async_see, discovery_info=None):
location_name=location_name
)
async_dispatcher_connect(hass, TRACKER_UPDATE, _set_location)
hass.data[DATA_KEY] = async_dispatcher_connect(
hass, TRACKER_UPDATE, _set_location
)
return True
async def async_unload_entry(hass, entry):
"""Unload the config entry and remove the dispatcher connection."""
hass.data[DATA_KEY]()
return True
......@@ -4,11 +4,15 @@ from unittest.mock import patch, Mock
import pytest
from homeassistant import data_entry_flow
from homeassistant.components import locative
from homeassistant.components.device_tracker import \
DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.components.locative import DOMAIN
from homeassistant.const import HTTP_OK, HTTP_UNPROCESSABLE_ENTITY
from homeassistant.components.locative import DOMAIN, TRACKER_UPDATE
from homeassistant.const import HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, \
CONF_WEBHOOK_ID
from homeassistant.helpers.dispatcher import DATA_DISPATCHER
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
......@@ -234,3 +238,21 @@ async def test_exit_first(hass, locative_client, webhook_id):
state = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
data['device']))
assert state.state == 'not_home'
@pytest.mark.xfail(
reason='The device_tracker component does not support unloading yet.'
)
async def test_load_unload_entry(hass):
"""Test that the appropriate dispatch signals are added and removed."""
entry = MockConfigEntry(domain=DOMAIN, data={
CONF_WEBHOOK_ID: 'locative_test'
})
await locative.async_setup_entry(hass, entry)
await hass.async_block_till_done()
assert 1 == len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE])
await locative.async_unload_entry(hass, entry)
await hass.async_block_till_done()
assert 0 == len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment