__init__.py 3.5 KB

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