mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-03 09:24:31 +02:00
coordinator update
This commit is contained in:
parent
ce9ce906f5
commit
a21e95c36a
14 changed files with 445 additions and 496 deletions
|
@ -1,80 +1,62 @@
|
|||
"""Mikrotik Router integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers import device_registry
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import (
|
||||
PLATFORMS,
|
||||
DOMAIN,
|
||||
RUN_SCRIPT_COMMAND,
|
||||
)
|
||||
from .mikrotik_controller import MikrotikControllerData
|
||||
from .coordinator import MikrotikCoordinator
|
||||
|
||||
SCRIPT_SCHEMA = vol.Schema(
|
||||
{vol.Required("router"): cv.string, vol.Required("script"): cv.string}
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# async_setup
|
||||
# ---------------------------
|
||||
async def async_setup(hass, _config):
|
||||
"""Set up configured Mikrotik Controller."""
|
||||
hass.data[DOMAIN] = {}
|
||||
return True
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# update_listener
|
||||
# ---------------------------
|
||||
async def update_listener(hass, config_entry) -> None:
|
||||
"""Handle options update."""
|
||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# async_setup_entry
|
||||
# ---------------------------
|
||||
async def async_setup_entry(hass, config_entry) -> bool:
|
||||
"""Set up Mikrotik Router as config entry."""
|
||||
controller = MikrotikControllerData(hass, config_entry)
|
||||
await controller.async_hwinfo_update()
|
||||
if not controller.connected():
|
||||
raise ConfigEntryNotReady("Cannot connect to host")
|
||||
|
||||
await controller.async_update()
|
||||
|
||||
if not controller.data:
|
||||
raise ConfigEntryNotReady()
|
||||
|
||||
await controller.async_init()
|
||||
hass.data[DOMAIN][config_entry.entry_id] = controller
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
"""Set up a config entry."""
|
||||
coordinator = MikrotikCoordinator(hass, config_entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
|
||||
|
||||
config_entry.async_on_unload(config_entry.add_update_listener(async_reload_entry))
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, RUN_SCRIPT_COMMAND, controller.run_script, schema=SCRIPT_SCHEMA
|
||||
DOMAIN, RUN_SCRIPT_COMMAND, coordinator.run_script, schema=SCRIPT_SCHEMA
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# async_reload_entry
|
||||
# ---------------------------
|
||||
async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||
"""Reload the config entry when it changed."""
|
||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# async_unload_entry
|
||||
# ---------------------------
|
||||
async def async_unload_entry(hass, config_entry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
||||
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(
|
||||
config_entry, PLATFORMS
|
||||
)
|
||||
if unload_ok:
|
||||
controller = hass.data[DOMAIN][config_entry.entry_id]
|
||||
await controller.async_reset()
|
||||
):
|
||||
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
|
||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue