Load/unload gpslogger entities correctly between component and platform (#20448)

* Embed device_tracker in gpslogger

* Load/unload gpslogger entities correctly between component and platform

* Await the coroutine directly
pull/20526/head
Rohan Kapoor 2019-01-27 15:37:19 -08:00 committed by GitHub
parent 0c87fb421e
commit d179686edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 39 deletions

View File

@ -1,32 +0,0 @@
"""
Support for the GPSLogger platform.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.gpslogger/
"""
import logging
from homeassistant.components.gpslogger import TRACKER_UPDATE
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType, ConfigType
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['gpslogger']
async def async_setup_scanner(hass: HomeAssistantType, config: ConfigType,
async_see, discovery_info=None):
"""Set up an endpoint for the GPSLogger device tracker."""
async def _set_location(device, gps_location, battery, accuracy, attrs):
"""Fire HA event to set location."""
await async_see(
dev_id=device,
gps=gps_location,
battery=battery,
gps_accuracy=accuracy,
attributes=attrs
)
async_dispatcher_connect(hass, TRACKER_UPDATE, _set_location)
return True

View File

@ -15,8 +15,8 @@ from homeassistant.components.device_tracker.tile import ATTR_ALTITUDE
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, \
HTTP_OK, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_WEBHOOK_ID
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.components.device_tracker import DOMAIN as DEVICE_TRACKER
_LOGGER = logging.getLogger(__name__)
@ -57,9 +57,6 @@ WEBHOOK_SCHEMA = vol.Schema({
async def async_setup(hass, hass_config):
"""Set up the GPSLogger component."""
hass.async_create_task(
async_load_platform(hass, 'device_tracker', DOMAIN, {}, hass_config)
)
return True
@ -103,12 +100,18 @@ async def async_setup_entry(hass, entry):
"""Configure based on config entry."""
hass.components.webhook.async_register(
DOMAIN, 'GPSLogger', 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(

View File

@ -0,0 +1,44 @@
"""
Support for the GPSLogger platform.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.gpslogger/
"""
import logging
from homeassistant.components.device_tracker import DOMAIN as \
DEVICE_TRACKER_DOMAIN
from homeassistant.components.gpslogger import DOMAIN as GPSLOGGER_DOMAIN, \
TRACKER_UPDATE
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['gpslogger']
DATA_KEY = '{}.{}'.format(GPSLOGGER_DOMAIN, DEVICE_TRACKER_DOMAIN)
async def async_setup_entry(hass: HomeAssistantType, entry, async_see):
"""Configure a dispatcher connection based on a config entry."""
async def _set_location(device, gps_location, battery, accuracy, attrs):
"""Fire HA event to set location."""
await async_see(
dev_id=device,
gps=gps_location,
battery=battery,
gps_accuracy=accuracy,
attributes=attrs
)
hass.data[DATA_KEY] = async_dispatcher_connect(
hass, TRACKER_UPDATE, _set_location
)
return True
async def async_unload_entry(hass: HomeAssistantType, entry):
"""Unload the config entry and remove the dispatcher connection."""
hass.data[DATA_KEY]()
return True

View File

@ -2,15 +2,17 @@
from unittest.mock import patch, Mock
import pytest
from homeassistant.helpers.dispatcher import DATA_DISPATCHER
from homeassistant import data_entry_flow
from homeassistant.components import zone
from homeassistant.components import zone, gpslogger
from homeassistant.components.device_tracker import \
DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.components.gpslogger import DOMAIN
from homeassistant.components.gpslogger import DOMAIN, TRACKER_UPDATE
from homeassistant.const import HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, \
STATE_HOME, STATE_NOT_HOME
STATE_HOME, STATE_NOT_HOME, CONF_WEBHOOK_ID
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
HOME_LATITUDE = 37.239622
HOME_LONGITUDE = -115.815811
@ -164,3 +166,21 @@ async def test_enter_with_attrs(hass, gpslogger_client, webhook_id):
assert 102.0 == state.attributes['altitude']
assert 'gps' == state.attributes['provider']
assert 'running' == state.attributes['activity']
@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: 'gpslogger_test'
})
await gpslogger.async_setup_entry(hass, entry)
await hass.async_block_till_done()
assert 1 == len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE])
await gpslogger.async_unload_entry(hass, entry)
await hass.async_block_till_done()
assert 0 == len(hass.data[DATA_DISPATCHER][TRACKER_UPDATE])