Skip to content

Commit

Permalink
Load/unload gpslogger entities correctly between component and platfo…
Browse files Browse the repository at this point in the history
…rm (home-assistant#20448)

* Embed device_tracker in gpslogger

* Load/unload gpslogger entities correctly between component and platform

* Await the coroutine directly
  • Loading branch information
rohankapoorcom authored Jan 27, 2019
1 parent 0c87fb4 commit d179686
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 39 deletions.
32 changes: 0 additions & 32 deletions homeassistant/components/device_tracker/gpslogger.py

This file was deleted.

11 changes: 7 additions & 4 deletions homeassistant/components/gpslogger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
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__)

Expand Down Expand Up @@ -57,9 +57,6 @@ def _id(value: str) -> str:

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


Expand Down Expand Up @@ -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(
Expand Down
44 changes: 44 additions & 0 deletions homeassistant/components/gpslogger/device_tracker.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 23 additions & 3 deletions tests/components/gpslogger/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])

0 comments on commit d179686

Please sign in to comment.