__init__.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if not await coordinator.api.async_login():
  34. raise ConfigEntryNotReady
  35. # Create the device
  36. di = await coordinator.api.async_query_api(oid=EP_DEVICESTATUS)
  37. coordinator.device_info = DeviceInfo()
  38. # Initialise a listener for config flow options changes.
  39. # This will be removed automatically if the integration is unloaded.
  40. # See config_flow for defining an options setting that shows up as configure
  41. # on the integration.
  42. # If you do not want any config flow options, no need to have listener.
  43. config_entry.async_on_unload(
  44. config_entry.add_update_listener(_async_update_listener)
  45. )
  46. # Add the coordinator and update listener to config runtime data to make
  47. # accessible throughout your integration
  48. config_entry.runtime_data = RuntimeData(coordinator)
  49. # Setup platforms (based on the list of entity types in PLATFORMS defined above)
  50. # This calls the async_setup method in each of your entity type files.
  51. await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
  52. # Return true to denote a successful setup.
  53. return True
  54. async def _async_update_listener(hass: HomeAssistant, config_entry: RouterConfigEntry):
  55. """Handle config options update."""
  56. # Reload the integration when the options change.
  57. await hass.config_entries.async_reload(config_entry.entry_id)
  58. async def async_remove_config_entry_device(
  59. hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
  60. ) -> bool:
  61. """Delete device if selected from UI."""
  62. # Adding this function shows the delete device option in the UI.
  63. # Remove this function if you do not want that option.
  64. # You may need to do some checks here before allowing devices to be removed.
  65. return True
  66. async def async_unload_entry(hass: HomeAssistant, config_entry: RouterConfigEntry) -> bool:
  67. """Unload a config entry."""
  68. # This is called when you remove your integration or shutdown HA.
  69. # If you have created any custom services, they need to be removed here too.
  70. # Unload platforms and return result
  71. return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)