mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 12:21:04 -04:00
Migrate OwnTracks to entry runtime data (#185608)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Support for OwnTracks."""
|
||||
# pylint: disable=home-assistant-use-runtime-data # Uses legacy hass.data[DOMAIN] pattern
|
||||
|
||||
from collections import defaultdict
|
||||
from functools import partial
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
@@ -26,7 +26,7 @@ from homeassistant.util import slugify
|
||||
from homeassistant.util.json import json_loads
|
||||
|
||||
from .config_flow import CONF_SECRET
|
||||
from .const import DOMAIN
|
||||
from .const import DATA_OWNTRACKS_CONFIG, DOMAIN
|
||||
from .messages import async_handle_message, encrypt_message
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -42,6 +42,8 @@ PLATFORMS = [Platform.DEVICE_TRACKER]
|
||||
|
||||
DEFAULT_OWNTRACKS_TOPIC = "owntracks/#"
|
||||
|
||||
type OwnTracksConfigEntry = ConfigEntry[OwnTracksContext]
|
||||
|
||||
CONFIG_SCHEMA = probatio.All(
|
||||
cv.removed(CONF_WEBHOOK_ID),
|
||||
probatio.Schema(
|
||||
@@ -70,13 +72,13 @@ CONFIG_SCHEMA = probatio.All(
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Initialize OwnTracks component."""
|
||||
hass.data[DOMAIN] = {"config": config[DOMAIN], "devices": {}, "unsub": None}
|
||||
hass.data[DATA_OWNTRACKS_CONFIG] = config[DOMAIN]
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: OwnTracksConfigEntry) -> bool:
|
||||
"""Set up OwnTracks entry."""
|
||||
config = hass.data[DOMAIN]["config"]
|
||||
config = hass.data[DATA_OWNTRACKS_CONFIG]
|
||||
max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
|
||||
waypoint_import = config.get(CONF_WAYPOINT_IMPORT)
|
||||
waypoint_whitelist = config.get(CONF_WAYPOINT_WHITELIST)
|
||||
@@ -98,31 +100,32 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
webhook_id = config.get(CONF_WEBHOOK_ID) or entry.data[CONF_WEBHOOK_ID]
|
||||
|
||||
hass.data[DOMAIN]["context"] = context
|
||||
entry.runtime_data = context
|
||||
|
||||
async_when_setup(hass, "mqtt", async_connect_mqtt)
|
||||
async_when_setup(hass, "mqtt", partial(async_connect_mqtt, context=context))
|
||||
|
||||
webhook.async_register(hass, DOMAIN, "OwnTracks", webhook_id, handle_webhook)
|
||||
webhook.async_register(
|
||||
hass,
|
||||
DOMAIN,
|
||||
"OwnTracks",
|
||||
webhook_id,
|
||||
partial(handle_webhook, context=context),
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
hass.data[DOMAIN]["unsub"] = async_dispatcher_connect(
|
||||
hass, DOMAIN, async_handle_message
|
||||
)
|
||||
entry.async_on_unload(async_dispatcher_connect(hass, DOMAIN, async_handle_message))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: OwnTracksConfigEntry) -> bool:
|
||||
"""Unload an OwnTracks config entry."""
|
||||
webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
hass.data[DOMAIN]["unsub"]()
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
async def async_remove_entry(hass: HomeAssistant, entry: OwnTracksConfigEntry) -> None:
|
||||
"""Remove an OwnTracks config entry."""
|
||||
if not entry.data.get("cloudhook"):
|
||||
return
|
||||
@@ -130,9 +133,8 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
await cloud.async_delete_cloudhook(hass, entry.data[CONF_WEBHOOK_ID])
|
||||
|
||||
|
||||
async def async_connect_mqtt(hass, component):
|
||||
async def async_connect_mqtt(hass, component, context):
|
||||
"""Subscribe to MQTT topic."""
|
||||
context = hass.data[DOMAIN]["context"]
|
||||
|
||||
@callback
|
||||
def async_handle_mqtt_message(msg):
|
||||
@@ -153,14 +155,16 @@ async def async_connect_mqtt(hass, component):
|
||||
|
||||
|
||||
async def handle_webhook(
|
||||
hass: HomeAssistant, webhook_id: str, request: web.Request
|
||||
hass: HomeAssistant,
|
||||
webhook_id: str,
|
||||
request: web.Request,
|
||||
context: OwnTracksContext,
|
||||
) -> web.Response:
|
||||
"""Handle webhook callback.
|
||||
|
||||
iOS sets the "topic" as part of the payload.
|
||||
Android does not set a topic but adds headers to the request.
|
||||
"""
|
||||
context = hass.data[DOMAIN]["context"]
|
||||
topic_base = re.sub("/#$", "", context.mqtt_topic)
|
||||
|
||||
try:
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
"""Constants for OwnTracks."""
|
||||
|
||||
from typing import Final
|
||||
from typing import Any, Final
|
||||
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
DOMAIN: Final = "owntracks"
|
||||
|
||||
DATA_OWNTRACKS_CONFIG: HassKey[dict[str, Any]] = HassKey(DOMAIN)
|
||||
|
||||
ATTR_ADDRESS: Final = "address"
|
||||
ATTR_BATTERY_STATUS: Final = "battery_status"
|
||||
ATTR_COURSE: Final = "course"
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
"""Device tracker platform that adds support for OwnTracks over MQTT."""
|
||||
# pylint: disable=home-assistant-use-runtime-data # Uses legacy hass.data[DOMAIN] pattern
|
||||
|
||||
from typing import Any, override
|
||||
|
||||
@@ -10,7 +9,6 @@ from homeassistant.components.device_tracker import (
|
||||
TrackerEntity,
|
||||
TrackerEntityStateAttribute,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_BATTERY_LEVEL, EntityStateAttribute
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
@@ -19,6 +17,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import OwnTracksConfigEntry
|
||||
from .const import (
|
||||
ATTR_ADDRESS,
|
||||
ATTR_BATTERY_STATUS,
|
||||
@@ -41,7 +40,7 @@ _RESTORED_OWNTRACKS_ATTRIBUTES: tuple[str, ...] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: OwnTracksConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up OwnTracks based off an entry."""
|
||||
@@ -53,24 +52,25 @@ async def async_setup_entry(
|
||||
for identifier in device.identifiers
|
||||
}
|
||||
|
||||
devices: dict[str, OwnTracksEntity] = {}
|
||||
entities = []
|
||||
for dev_id in dev_ids:
|
||||
entity = hass.data[DOMAIN]["devices"][dev_id] = OwnTracksEntity(dev_id)
|
||||
entity = devices[dev_id] = OwnTracksEntity(dev_id)
|
||||
entities.append(entity)
|
||||
|
||||
@callback
|
||||
def _receive_data(dev_id, **data):
|
||||
"""Receive set location."""
|
||||
entity = hass.data[DOMAIN]["devices"].get(dev_id)
|
||||
entity = devices.get(dev_id)
|
||||
|
||||
if entity is not None:
|
||||
entity.update_data(data)
|
||||
return
|
||||
|
||||
entity = hass.data[DOMAIN]["devices"][dev_id] = OwnTracksEntity(dev_id, data)
|
||||
entity = devices[dev_id] = OwnTracksEntity(dev_id, data)
|
||||
async_add_entities([entity])
|
||||
|
||||
hass.data[DOMAIN]["context"].set_async_see(_receive_data)
|
||||
entry.runtime_data.set_async_see(_receive_data)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user