Skip to content

Commit

Permalink
added run_script service #30
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaae committed Apr 20, 2020
1 parent 46f7c58 commit 4e66101
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion custom_components/mikrotik_router/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
"""Mikrotik Router integration."""

import logging
import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.exceptions import ConfigEntryNotReady

from homeassistant.const import CONF_NAME
from .const import (
DOMAIN,
DATA_CLIENT,
RUN_SCRIPT_COMMAND,
)
from .mikrotik_controller import MikrotikControllerData

_LOGGER = logging.getLogger(__name__)

SCRIPT_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})


# ---------------------------
# async_setup
Expand Down Expand Up @@ -55,6 +60,10 @@ async def async_setup_entry(hass, config_entry):
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.async_get_or_create(
config_entry_id=config_entry.entry_id,
Expand All @@ -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, "device_tracker")
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
await controller.async_reset()
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
return True
2 changes: 2 additions & 0 deletions custom_components/mikrotik_router/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
DATA_CLIENT = "client"
ATTRIBUTION = "Data provided by Mikrotik"

RUN_SCRIPT_COMMAND = "run_script"

DEFAULT_ENCODING = "ISO-8859-1"
DEFAULT_LOGIN_METHOD = "plain"

Expand Down
4 changes: 4 additions & 0 deletions custom_components/mikrotik_router/mikrotik_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def set_value(self, path, param, value, mod_param, mod_value):
# ---------------------------
def run_script(self, name):
"""Run script using Mikrotik API"""
if type(name) != str:
if CONF_NAME in name.data:
name = name.data.get(CONF_NAME)

try:
self.api.run_script(name)
except ApiEntryNotFound as error:
Expand Down
6 changes: 6 additions & 0 deletions custom_components/mikrotik_router/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
run_script:
description: Run script on Mikrotik
fields:
name:
description: Name of the script
example: "MyScript"

0 comments on commit 4e66101

Please sign in to comment.