tomaae.homeassistant-mikrot.../custom_components/mikrotik_router/__init__.py

74 lines
2.2 KiB
Python
Raw Normal View History

2020-03-21 19:02:28 +03:00
"""Mikrotik Router integration."""
2023-08-08 00:50:09 +02:00
from __future__ import annotations
2020-03-21 19:02:28 +03:00
2020-04-20 11:05:49 +02:00
import voluptuous as vol
2020-03-21 19:02:28 +03:00
2023-08-08 00:50:09 +02:00
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
2020-03-21 19:02:28 +03:00
from .const import (
PLATFORMS,
2020-03-21 19:02:28 +03:00
DOMAIN,
2020-04-20 11:05:49 +02:00
RUN_SCRIPT_COMMAND,
2020-03-21 19:02:28 +03:00
)
2023-08-08 00:50:09 +02:00
from .coordinator import MikrotikCoordinator
2020-03-21 19:02:28 +03:00
SCRIPT_SCHEMA = vol.Schema(
{vol.Required("router"): cv.string, vol.Required("script"): cv.string}
)
2020-04-20 11:05:49 +02:00
2020-03-21 19:02:28 +03:00
# ---------------------------
# async_setup_entry
# ---------------------------
2023-08-08 00:50:09 +02:00
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
2020-03-21 19:02:28 +03:00
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
2023-08-08 00:50:09 +02:00
config_entry.async_on_unload(config_entry.add_update_listener(async_reload_entry))
2022-01-08 21:05:50 +01:00
2020-04-20 11:05:49 +02:00
hass.services.async_register(
2023-08-08 00:50:09 +02:00
DOMAIN, RUN_SCRIPT_COMMAND, coordinator.run_script, schema=SCRIPT_SCHEMA
2020-04-20 11:05:49 +02:00
)
2020-03-21 19:02:28 +03:00
return True
2023-08-08 00:50:09 +02:00
# ---------------------------
# 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)
2020-03-21 19:02:28 +03:00
# ---------------------------
# async_unload_entry
# ---------------------------
2023-08-08 00:50:09 +02:00
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
2020-03-21 19:02:28 +03:00
"""Unload a config entry."""
2023-08-08 00:50:09 +02:00
if unload_ok := await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
2023-08-08 00:50:09 +02:00
):
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
2022-02-18 08:06:10 +01:00
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
# ---------------------------
# async_remove_config_entry_device
# ---------------------------
async def async_remove_config_entry_device(
hass, config_entry: ConfigEntry, device_entry: device_registry.DeviceEntry
) -> bool:
"""Remove a config entry from a device."""
return True