mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Load/unload geofency entities correctly between component and platform (#20497)
* Load/unload geofency entities correctly between component and platform * Lint * Await the coroutine directly
This commit is contained in:
parent
3d4f2926e9
commit
f575d1d3a6
@ -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(
|
||||
|
@ -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
|
||||
|
@ -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])
|
||||
|
Loading…
x
Reference in New Issue
Block a user