__init__.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """The Integration 101 Template integration."""
  2. from __future__ import annotations
  3. from collections.abc import Callable
  4. from dataclasses import dataclass
  5. import logging
  6. from homeassistant.config_entries import ConfigEntry
  7. from homeassistant.const import Platform
  8. from homeassistant.core import HomeAssistant
  9. from homeassistant.exceptions import ConfigEntryNotReady
  10. from homeassistant.helpers.device_registry import DeviceEntry
  11. from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
  12. from .coordinator import RouterCoordinator
  13. _LOGGER = logging.getLogger(__name__)
  14. PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR,
  15. Platform.SENSOR,
  16. Platform.BUTTON,
  17. Platform.SWITCH,
  18. Platform.TEXT]
  19. type RouterConfigEntry = ConfigEntry[RuntimeData]
  20. @dataclass
  21. class RuntimeData:
  22. """Class to hold your data."""
  23. coordinator: DataUpdateCoordinator
  24. async def async_setup_entry(hass: HomeAssistant, config_entry: RouterConfigEntry) -> bool:
  25. """Set up Example Integration from a config entry."""
  26. # Initialise the coordinator that manages data updates from your api.
  27. # This is defined in coordinator.py
  28. coordinator = RouterCoordinator(hass, config_entry)
  29. # Perform an initial data load from api.
  30. # async_config_entry_first_refresh() is special in that it does not log errors if it fails
  31. if not await coordinator.api.async_login():
  32. raise ConfigEntryNotReady
  33. # Initialise a listener for config flow options changes.
  34. # This will be removed automatically if the integration is unloaded.
  35. # See config_flow for defining an options setting that shows up as configure
  36. # on the integration.
  37. # If you do not want any config flow options, no need to have listener.
  38. config_entry.async_on_unload(
  39. config_entry.add_update_listener(_async_update_listener)
  40. )
  41. # Add the coordinator and update listener to config runtime data to make
  42. # accessible throughout your integration
  43. config_entry.runtime_data = RuntimeData(coordinator)
  44. # Setup platforms (based on the list of entity types in PLATFORMS defined above)
  45. # This calls the async_setup method in each of your entity type files.
  46. await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
  47. # Return true to denote a successful setup.
  48. return True
  49. async def _async_update_listener(hass: HomeAssistant, config_entry: RouterConfigEntry):
  50. """Handle config options update."""
  51. # Reload the integration when the options change.
  52. await hass.config_entries.async_reload(config_entry.entry_id)
  53. async def async_remove_config_entry_device(
  54. hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
  55. ) -> bool:
  56. """Delete device if selected from UI."""
  57. # Adding this function shows the delete device option in the UI.
  58. # Remove this function if you do not want that option.
  59. # You may need to do some checks here before allowing devices to be removed.
  60. return True
  61. async def async_unload_entry(hass: HomeAssistant, config_entry: RouterConfigEntry) -> bool:
  62. """Unload a config entry."""
  63. # This is called when you remove your integration or shutdown HA.
  64. # If you have created any custom services, they need to be removed here too.
  65. # Unload platforms and return result
  66. return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)