Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions homeassistant/components/locative/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

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__)
Expand Down Expand Up @@ -57,9 +56,6 @@ def _validate_test_mode(obj: Dict) -> Dict:

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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
"""
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

_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(
Expand All @@ -24,5 +29,13 @@ async def _set_location(device, gps_location, location_name):
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
26 changes: 24 additions & 2 deletions tests/components/locative/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
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)
Expand Down Expand Up @@ -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])