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

92 lines
2.6 KiB
Python
Raw Normal View History

2020-03-21 19:02:28 +03:00
"""Mikrotik Router integration."""
2020-04-20 11:05:49 +02:00
import voluptuous as vol
2020-03-21 19:02:28 +03:00
2020-04-20 11:05:49 +02:00
import homeassistant.helpers.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 homeassistant.exceptions import ConfigEntryNotReady
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
)
from .mikrotik_controller import MikrotikControllerData
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
# ---------------------------
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)
2020-03-21 19:02:28 +03:00
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry) -> bool:
2020-03-21 19:02:28 +03:00
"""Set up Mikrotik Router as config entry."""
2020-04-11 05:35:40 +02:00
controller = MikrotikControllerData(hass, config_entry)
await controller.async_hwinfo_update()
if not controller.connected():
2022-03-26 01:29:03 +01:00
raise ConfigEntryNotReady("Cannot connect to host")
2020-04-11 05:35:40 +02:00
await controller.async_update()
2020-03-21 19:02:28 +03:00
2020-04-11 05:35:40 +02:00
if not controller.data:
2020-03-21 19:02:28 +03:00
raise ConfigEntryNotReady()
2020-04-11 05:35:40 +02:00
await controller.async_init()
2022-02-18 08:06:10 +01:00
hass.data[DOMAIN][config_entry.entry_id] = controller
2020-03-21 19:02:28 +03:00
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
2022-01-08 21:05:50 +01:00
2020-04-20 11:05:49 +02:00
hass.services.async_register(
DOMAIN, RUN_SCRIPT_COMMAND, controller.run_script, schema=SCRIPT_SCHEMA
)
2020-03-21 19:02:28 +03:00
return True
# ---------------------------
# async_unload_entry
# ---------------------------
async def async_unload_entry(hass, config_entry) -> bool:
2020-03-21 19:02:28 +03:00
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
)
if unload_ok:
2022-02-18 08:06:10 +01:00
controller = hass.data[DOMAIN][config_entry.entry_id]
await controller.async_reset()
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