mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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
This commit is contained in:
parent
0c87fb421e
commit
d179686edf
@ -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
|
|
@ -15,8 +15,8 @@ from homeassistant.components.device_tracker.tile import ATTR_ALTITUDE
|
|||||||
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, \
|
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, \
|
||||||
HTTP_OK, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_WEBHOOK_ID
|
HTTP_OK, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_WEBHOOK_ID
|
||||||
from homeassistant.helpers import config_entry_flow
|
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.helpers.dispatcher import async_dispatcher_send
|
||||||
|
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -57,9 +57,6 @@ WEBHOOK_SCHEMA = vol.Schema({
|
|||||||
|
|
||||||
async def async_setup(hass, hass_config):
|
async def async_setup(hass, hass_config):
|
||||||
"""Set up the GPSLogger component."""
|
"""Set up the GPSLogger component."""
|
||||||
hass.async_create_task(
|
|
||||||
async_load_platform(hass, 'device_tracker', DOMAIN, {}, hass_config)
|
|
||||||
)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -103,12 +100,18 @@ async def async_setup_entry(hass, entry):
|
|||||||
"""Configure based on config entry."""
|
"""Configure based on config entry."""
|
||||||
hass.components.webhook.async_register(
|
hass.components.webhook.async_register(
|
||||||
DOMAIN, 'GPSLogger', entry.data[CONF_WEBHOOK_ID], handle_webhook)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass, entry):
|
async def async_unload_entry(hass, entry):
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
||||||
|
|
||||||
|
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
config_entry_flow.register_webhook_flow(
|
config_entry_flow.register_webhook_flow(
|
||||||
|
44
homeassistant/components/gpslogger/device_tracker.py
Normal file
44
homeassistant/components/gpslogger/device_tracker.py
Normal 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
|
@ -2,15 +2,17 @@
|
|||||||
from unittest.mock import patch, Mock
|
from unittest.mock import patch, Mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from homeassistant.helpers.dispatcher import DATA_DISPATCHER
|
||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components import zone
|
from homeassistant.components import zone, gpslogger
|
||||||
from homeassistant.components.device_tracker import \
|
from homeassistant.components.device_tracker import \
|
||||||
DOMAIN as DEVICE_TRACKER_DOMAIN
|
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, \
|
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 homeassistant.setup import async_setup_component
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
HOME_LATITUDE = 37.239622
|
HOME_LATITUDE = 37.239622
|
||||||
HOME_LONGITUDE = -115.815811
|
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 102.0 == state.attributes['altitude']
|
||||||
assert 'gps' == state.attributes['provider']
|
assert 'gps' == state.attributes['provider']
|
||||||
assert 'running' == state.attributes['activity']
|
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])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user