Skip to content

Commit

Permalink
Use HassKey in device_tracker (home-assistant#126339)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored and JakeMartin-ICL committed Sep 21, 2024
1 parent f06c159 commit c979383
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions homeassistant/components/device_tracker/config_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity_platform import EntityPlatform
from homeassistant.helpers.typing import StateType
from homeassistant.util.hass_dict import HassKey

from .const import (
ATTR_HOST_NAME,
Expand All @@ -40,6 +41,9 @@
SourceType,
)

DOMAIN_DATA: HassKey[EntityComponent[BaseTrackerEntity]] = HassKey(DOMAIN)
DATA_KEY: HassKey[dict[str, tuple[str, str]]] = HassKey(f"{DOMAIN}_mac")

# mypy: disallow-any-generics


Expand All @@ -50,7 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if component is not None:
return await component.async_setup_entry(entry)

component = hass.data[DOMAIN] = EntityComponent[BaseTrackerEntity](
component = hass.data[DOMAIN_DATA] = EntityComponent[BaseTrackerEntity](
LOGGER, DOMAIN, hass
)
component.register_shutdown()
Expand All @@ -60,8 +64,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload an entry."""
component: EntityComponent[BaseTrackerEntity] = hass.data[DOMAIN]
return await component.async_unload_entry(entry)
return await hass.data[DOMAIN_DATA].async_unload_entry(entry)


@callback
Expand Down Expand Up @@ -93,16 +96,15 @@ def _async_register_mac(
unique_id: str,
) -> None:
"""Register a mac address with a unique ID."""
data_key = "device_tracker_mac"
mac = dr.format_mac(mac)
if data_key in hass.data:
hass.data[data_key][mac] = (domain, unique_id)
if DATA_KEY in hass.data:
hass.data[DATA_KEY][mac] = (domain, unique_id)
return

# Setup listening.

# dict mapping mac -> partial unique ID
data = hass.data[data_key] = {mac: (domain, unique_id)}
data = hass.data[DATA_KEY] = {mac: (domain, unique_id)}

@callback
def handle_device_event(ev: Event[EventDeviceRegistryUpdatedData]) -> None:
Expand Down

0 comments on commit c979383

Please sign in to comment.