diff --git a/homeassistant/components/geofency/__init__.py b/homeassistant/components/geofency/__init__.py index 093ebaa2fd3..239af14add8 100644 --- a/homeassistant/components/geofency/__init__.py +++ b/homeassistant/components/geofency/__init__.py @@ -10,10 +10,10 @@ import voluptuous as vol from aiohttp import web import homeassistant.helpers.config_validation as cv +from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, STATE_NOT_HOME, \ ATTR_LATITUDE, ATTR_LONGITUDE, CONF_WEBHOOK_ID, HTTP_OK, ATTR_NAME from homeassistant.helpers import config_entry_flow -from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.util import slugify @@ -71,10 +71,6 @@ async def async_setup(hass, hass_config): config = hass_config[DOMAIN] mobile_beacons = config[CONF_MOBILE_BEACONS] hass.data[DOMAIN] = [slugify(beacon) for beacon in mobile_beacons] - - hass.async_create_task( - async_load_platform(hass, 'device_tracker', DOMAIN, {}, hass_config) - ) return True @@ -136,12 +132,18 @@ async def async_setup_entry(hass, entry): """Configure based on config entry.""" hass.components.webhook.async_register( DOMAIN, 'Geofency', 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( diff --git a/homeassistant/components/geofency/device_tracker.py b/homeassistant/components/geofency/device_tracker.py index af11194c1d6..eea0960ec11 100644 --- a/homeassistant/components/geofency/device_tracker.py +++ b/homeassistant/components/geofency/device_tracker.py @@ -6,16 +6,21 @@ https://home-assistant.io/components/device_tracker.geofency/ """ import logging -from homeassistant.components.geofency import TRACKER_UPDATE +from homeassistant.components.device_tracker import DOMAIN as \ + DEVICE_TRACKER_DOMAIN +from homeassistant.components.geofency import TRACKER_UPDATE, \ + DOMAIN as GEOFENCY_DOMAIN from homeassistant.helpers.dispatcher import async_dispatcher_connect _LOGGER = logging.getLogger(__name__) DEPENDENCIES = ['geofency'] +DATA_KEY = '{}.{}'.format(GEOFENCY_DOMAIN, DEVICE_TRACKER_DOMAIN) -async def async_setup_scanner(hass, config, async_see, discovery_info=None): - """Set up the Geofency 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_name, attributes): """Fire HA event to set location.""" await async_see( @@ -25,5 +30,13 @@ async def async_setup_scanner(hass, config, async_see, discovery_info=None): attributes=attributes ) - 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 diff --git a/tests/components/geofency/test_init.py b/tests/components/geofency/test_init.py index dbad7ba668b..41c232a51c3 100644 --- a/tests/components/geofency/test_init.py +++ b/tests/components/geofency/test_init.py @@ -5,14 +5,16 @@ from unittest.mock import patch, Mock import pytest from homeassistant import data_entry_flow -from homeassistant.components import zone +from homeassistant.components import zone, geofency from homeassistant.components.geofency import ( - CONF_MOBILE_BEACONS, DOMAIN) + CONF_MOBILE_BEACONS, DOMAIN, TRACKER_UPDATE) from homeassistant.const import ( HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, STATE_HOME, - STATE_NOT_HOME) + STATE_NOT_HOME, CONF_WEBHOOK_ID) +from homeassistant.helpers.dispatcher import DATA_DISPATCHER from homeassistant.setup import async_setup_component from homeassistant.util import slugify +from tests.common import MockConfigEntry HOME_LATITUDE = 37.239622 HOME_LONGITUDE = -115.815811 @@ -281,3 +283,21 @@ async def test_beacon_enter_and_exit_car(hass, geofency_client, webhook_id): state_name = hass.states.get('{}.{}'.format( 'device_tracker', device_name)).state assert STATE_HOME == state_name + + +@pytest.mark.xfail( + reason='The device_tracker component does not support unloading yet.' +) +async def test_load_unload_entry(hass, geofency_client): + """Test that the appropriate dispatch signals are added and removed.""" + entry = MockConfigEntry(domain=DOMAIN, data={ + CONF_WEBHOOK_ID: 'geofency_test' + }) + + await geofency.async_setup_entry(hass, entry) + await hass.async_block_till_done() + assert 1 == len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE]) + + await geofency.async_unload_entry(hass, entry) + await hass.async_block_till_done() + assert 0 == len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE])