1 """The keenetic_ndms2 component."""
3 from __future__
import annotations
15 CONF_INCLUDE_ASSOCIATED,
18 DEFAULT_CONSIDER_HOME,
20 DEFAULT_SCAN_INTERVAL,
25 from .router
import KeeneticRouter
27 PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER]
28 _LOGGER = logging.getLogger(__name__)
32 """Set up the component."""
33 hass.data.setdefault(DOMAIN, {})
37 await router.async_setup()
39 undo_listener = entry.add_update_listener(update_listener)
41 hass.data[DOMAIN][entry.entry_id] = {
43 UNDO_UPDATE_LISTENER: undo_listener,
46 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
52 """Unload a config entry."""
53 hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()
55 unload_ok = await hass.config_entries.async_unload_platforms(
56 config_entry, PLATFORMS
59 router: KeeneticRouter = hass.data[DOMAIN][config_entry.entry_id][ROUTER]
61 await router.async_teardown()
63 hass.data[DOMAIN].pop(config_entry.entry_id)
65 new_tracked_interfaces: set[str] = set(config_entry.options[CONF_INTERFACES])
67 if router.tracked_interfaces - new_tracked_interfaces:
69 "Cleaning device_tracker entities since some interfaces are now untracked:"
71 ent_reg = er.async_get(hass)
72 dev_reg = dr.async_get(hass)
74 keep_devices: set[str] = {
76 for mac, device
in router.last_devices.items()
77 if device.interface
in new_tracked_interfaces
79 for entity_entry
in ent_reg.entities.get_entries_for_config_entry_id(
82 if entity_entry.domain == Platform.DEVICE_TRACKER:
83 mac = entity_entry.unique_id.partition(
"_")[0]
84 if mac
not in keep_devices:
85 _LOGGER.debug(
"Removing entity %s", entity_entry.entity_id)
87 ent_reg.async_remove(entity_entry.entity_id)
88 if entity_entry.device_id:
89 dev_reg.async_update_device(
90 entity_entry.device_id,
91 remove_config_entry_id=config_entry.entry_id,
94 _LOGGER.debug(
"Finished cleaning device_tracker entities")
100 """Handle options update."""
101 await hass.config_entries.async_reload(entry.entry_id)
105 """Populate default options."""
106 host: str = entry.data[CONF_HOST]
107 imported_options: dict = hass.data[DOMAIN].
get(f
"imported_options_{host}", {})
109 CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
110 CONF_CONSIDER_HOME: DEFAULT_CONSIDER_HOME,
111 CONF_INTERFACES: [DEFAULT_INTERFACE],
112 CONF_TRY_HOTSPOT:
True,
113 CONF_INCLUDE_ARP:
True,
114 CONF_INCLUDE_ASSOCIATED:
True,
119 if options.keys() - entry.options.keys():
120 hass.config_entries.async_update_entry(entry, options=options)
web.Response get(self, web.Request request, str config_key)
None update_listener(HomeAssistant hass, ConfigEntry entry)
def async_add_defaults(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)