diff --git a/custom_components/mikrotik_router/__init__.py b/custom_components/mikrotik_router/__init__.py index 8016d73..625bf3a 100644 --- a/custom_components/mikrotik_router/__init__.py +++ b/custom_components/mikrotik_router/__init__.py @@ -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 @@ -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, @@ -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 diff --git a/custom_components/mikrotik_router/const.py b/custom_components/mikrotik_router/const.py index 9b62419..ef6e9eb 100644 --- a/custom_components/mikrotik_router/const.py +++ b/custom_components/mikrotik_router/const.py @@ -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" diff --git a/custom_components/mikrotik_router/mikrotik_controller.py b/custom_components/mikrotik_router/mikrotik_controller.py index d1c85d9..5450fa6 100644 --- a/custom_components/mikrotik_router/mikrotik_controller.py +++ b/custom_components/mikrotik_router/mikrotik_controller.py @@ -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: diff --git a/custom_components/mikrotik_router/services.yaml b/custom_components/mikrotik_router/services.yaml new file mode 100644 index 0000000..4d7cb38 --- /dev/null +++ b/custom_components/mikrotik_router/services.yaml @@ -0,0 +1,6 @@ +run_script: + description: Run script on Mikrotik + fields: + name: + description: Name of the script + example: "MyScript" \ No newline at end of file