| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- """The Integration 101 Template integration."""
- from __future__ import annotations
- from collections.abc import Callable
- from dataclasses import dataclass
- import logging
- from homeassistant.config_entries import ConfigEntry
- from homeassistant.const import Platform
- from homeassistant.core import HomeAssistant
- from homeassistant.exceptions import ConfigEntryNotReady
- from homeassistant.helpers.device_registry import DeviceEntry
- from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
- from homeassistant.helpers.entity import DeviceInfo
- from .coordinator import RouterCoordinator
- from .const import EP_DEVICESTATUS
- _LOGGER = logging.getLogger(__name__)
- PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR,
- Platform.SENSOR,
- Platform.BUTTON,
- Platform.SWITCH,
- Platform.TEXT]
- type RouterConfigEntry = ConfigEntry[RuntimeData]
- @dataclass
- class RuntimeData:
- """Class to hold your data."""
- coordinator: DataUpdateCoordinator
- async def async_setup_entry(hass: HomeAssistant, config_entry: RouterConfigEntry) -> bool:
- """Set up Example Integration from a config entry."""
- # Initialise the coordinator that manages data updates from your api.
- # This is defined in coordinator.py
- coordinator = RouterCoordinator(hass, config_entry)
- # Perform an initial data load from api.
- # async_config_entry_first_refresh() is special in that it does not log errors if it fails
- await coordinator.async_config_entry_first_refresh()
-
- # Create the device
- # di = await coordinator.api.async_query_api(oid=EP_DEVICESTATUS)
- # coordinator.device_info = DeviceInfo()
- # Initialise a listener for config flow options changes.
- # This will be removed automatically if the integration is unloaded.
- # See config_flow for defining an options setting that shows up as configure
- # on the integration.
- # If you do not want any config flow options, no need to have listener.
- config_entry.async_on_unload(
- config_entry.add_update_listener(_async_update_listener)
- )
- # Add the coordinator and update listener to config runtime data to make
- # accessible throughout your integration
- config_entry.runtime_data = RuntimeData(coordinator)
- # Setup platforms (based on the list of entity types in PLATFORMS defined above)
- # This calls the async_setup method in each of your entity type files.
- await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
- # Return true to denote a successful setup.
- return True
- async def _async_update_listener(hass: HomeAssistant, config_entry: RouterConfigEntry):
- """Handle config options update."""
- # Reload the integration when the options change.
- await hass.config_entries.async_reload(config_entry.entry_id)
- async def async_remove_config_entry_device(
- hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
- ) -> bool:
- """Delete device if selected from UI."""
- # Adding this function shows the delete device option in the UI.
- # Remove this function if you do not want that option.
- # You may need to do some checks here before allowing devices to be removed.
- return True
- async def async_unload_entry(hass: HomeAssistant, config_entry: RouterConfigEntry) -> bool:
- """Unload a config entry."""
- # This is called when you remove your integration or shutdown HA.
- # If you have created any custom services, they need to be removed here too.
- # Unload platforms and return result
- return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|