__init__.py 3.6 KB

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