Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 5 deletions homeassistant/components/geofency/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
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

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -136,12 +132,20 @@ 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])

hass.async_create_task(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can await the coroutine directly. We don't need to create a task.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't better practice to create a task so that we don't block this thread? Or does it not matter since all we're doing is registering with the dispatcher?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no risk of deadlock when using this method.

hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
)
return True

config_entry_flow.register_webhook_flow(
Expand Down
21 changes: 17 additions & 4 deletions homeassistant/components/geofency/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
"""
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(
Expand All @@ -25,5 +30,13 @@ async def _set_location(device, gps, location_name, attributes):
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
26 changes: 23 additions & 3 deletions tests/components/geofency/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
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
Expand Down Expand Up @@ -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])
Comment thread
rohankapoorcom marked this conversation as resolved.
Outdated