mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-10 17:34:29 +02:00
added run_script service #30
This commit is contained in:
parent
46f7c582ab
commit
4e66101038
4 changed files with 23 additions and 1 deletions
|
@ -1,17 +1,22 @@
|
||||||
"""Mikrotik Router integration."""
|
"""Mikrotik Router integration."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
from homeassistant.const import CONF_NAME
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
DATA_CLIENT,
|
DATA_CLIENT,
|
||||||
|
RUN_SCRIPT_COMMAND,
|
||||||
)
|
)
|
||||||
from .mikrotik_controller import MikrotikControllerData
|
from .mikrotik_controller import MikrotikControllerData
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SCRIPT_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# async_setup
|
# async_setup
|
||||||
|
@ -55,6 +60,10 @@ async def async_setup_entry(hass, config_entry):
|
||||||
hass.config_entries.async_forward_entry_setup(config_entry, "switch")
|
hass.config_entries.async_forward_entry_setup(config_entry, "switch")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, RUN_SCRIPT_COMMAND, controller.run_script, schema=SCRIPT_SCHEMA
|
||||||
|
)
|
||||||
|
|
||||||
device_registry = await hass.helpers.device_registry.async_get_registry()
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
||||||
device_registry.async_get_or_create(
|
device_registry.async_get_or_create(
|
||||||
config_entry_id=config_entry.entry_id,
|
config_entry_id=config_entry.entry_id,
|
||||||
|
@ -77,6 +86,7 @@ async def async_unload_entry(hass, config_entry):
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
||||||
|
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
|
||||||
await controller.async_reset()
|
await controller.async_reset()
|
||||||
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
|
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -5,6 +5,8 @@ DEFAULT_NAME = "Mikrotik Router"
|
||||||
DATA_CLIENT = "client"
|
DATA_CLIENT = "client"
|
||||||
ATTRIBUTION = "Data provided by Mikrotik"
|
ATTRIBUTION = "Data provided by Mikrotik"
|
||||||
|
|
||||||
|
RUN_SCRIPT_COMMAND = "run_script"
|
||||||
|
|
||||||
DEFAULT_ENCODING = "ISO-8859-1"
|
DEFAULT_ENCODING = "ISO-8859-1"
|
||||||
DEFAULT_LOGIN_METHOD = "plain"
|
DEFAULT_LOGIN_METHOD = "plain"
|
||||||
|
|
||||||
|
|
|
@ -193,6 +193,10 @@ class MikrotikControllerData:
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def run_script(self, name):
|
def run_script(self, name):
|
||||||
"""Run script using Mikrotik API"""
|
"""Run script using Mikrotik API"""
|
||||||
|
if type(name) != str:
|
||||||
|
if CONF_NAME in name.data:
|
||||||
|
name = name.data.get(CONF_NAME)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.api.run_script(name)
|
self.api.run_script(name)
|
||||||
except ApiEntryNotFound as error:
|
except ApiEntryNotFound as error:
|
||||||
|
|
6
custom_components/mikrotik_router/services.yaml
Normal file
6
custom_components/mikrotik_router/services.yaml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
run_script:
|
||||||
|
description: Run script on Mikrotik
|
||||||
|
fields:
|
||||||
|
name:
|
||||||
|
description: Name of the script
|
||||||
|
example: "MyScript"
|
Loading…
Add table
Add a link
Reference in a new issue