Fix Fritz device tracker multiple routers (#49808)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
f5e4b13814
commit
0421c55bf1
4 changed files with 36 additions and 8 deletions
|
@ -15,8 +15,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .common import FritzBoxTools
|
||||
from .const import DOMAIN, PLATFORMS
|
||||
from .common import FritzBoxTools, FritzData
|
||||
from .const import DATA_FRITZ, DOMAIN, PLATFORMS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -43,6 +43,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = fritz_tools
|
||||
|
||||
if DATA_FRITZ not in hass.data:
|
||||
hass.data[DATA_FRITZ] = FritzData()
|
||||
|
||||
@callback
|
||||
def _async_unload(event):
|
||||
fritz_tools.async_unload()
|
||||
|
@ -61,6 +64,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigType) -> bool:
|
|||
fritzbox: FritzBoxTools = hass.data[DOMAIN][entry.entry_id]
|
||||
fritzbox.async_unload()
|
||||
|
||||
fritz_data = hass.data[DATA_FRITZ]
|
||||
fritz_data.tracked.pop(fritzbox.unique_id)
|
||||
|
||||
if not bool(fritz_data.tracked):
|
||||
hass.data.pop(DATA_FRITZ)
|
||||
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
|
|
@ -184,6 +184,14 @@ class FritzBoxTools:
|
|||
return dev_info
|
||||
|
||||
|
||||
class FritzData:
|
||||
"""Storage class for platform global data."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the data."""
|
||||
self.tracked = {}
|
||||
|
||||
|
||||
class FritzDevice:
|
||||
"""FritzScanner device."""
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ DOMAIN = "fritz"
|
|||
|
||||
PLATFORMS = ["device_tracker"]
|
||||
|
||||
DATA_FRITZ = "fritz_data"
|
||||
|
||||
DEFAULT_DEVICE_NAME = "Unknown device"
|
||||
DEFAULT_HOST = "192.168.178.1"
|
||||
|
|
|
@ -21,7 +21,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .common import FritzBoxTools
|
||||
from .const import DEFAULT_DEVICE_NAME, DOMAIN
|
||||
from .const import DATA_FRITZ, DEFAULT_DEVICE_NAME, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -69,12 +69,12 @@ async def async_setup_entry(
|
|||
"""Set up device tracker for FRITZ!Box component."""
|
||||
_LOGGER.debug("Starting FRITZ!Box device tracker")
|
||||
router = hass.data[DOMAIN][entry.entry_id]
|
||||
tracked = set()
|
||||
data_fritz = hass.data[DATA_FRITZ]
|
||||
|
||||
@callback
|
||||
def update_router():
|
||||
"""Update the values of the router."""
|
||||
_async_add_entities(router, async_add_entities, tracked)
|
||||
_async_add_entities(router, async_add_entities, data_fritz)
|
||||
|
||||
async_dispatcher_connect(hass, router.signal_device_new, update_router)
|
||||
|
||||
|
@ -82,16 +82,26 @@ async def async_setup_entry(
|
|||
|
||||
|
||||
@callback
|
||||
def _async_add_entities(router, async_add_entities, tracked):
|
||||
def _async_add_entities(router, async_add_entities, data_fritz):
|
||||
"""Add new tracker entities from the router."""
|
||||
|
||||
def _is_tracked(mac, device):
|
||||
for tracked in data_fritz.tracked.values():
|
||||
if mac in tracked:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
new_tracked = []
|
||||
if router.unique_id not in data_fritz.tracked:
|
||||
data_fritz.tracked[router.unique_id] = set()
|
||||
|
||||
for mac, device in router.devices.items():
|
||||
if mac in tracked:
|
||||
if device.ip_address == "" or _is_tracked(mac, device):
|
||||
continue
|
||||
|
||||
new_tracked.append(FritzBoxTracker(router, device))
|
||||
tracked.add(mac)
|
||||
data_fritz.tracked[router.unique_id].add(mac)
|
||||
|
||||
if new_tracked:
|
||||
async_add_entities(new_tracked)
|
||||
|
|
Loading…
Add table
Reference in a new issue