Reworked scripts to buttons only, fixes #414

This commit is contained in:
Tomaae 2025-05-01 11:21:10 +02:00
parent ee886e0365
commit 6487398741
No known key found for this signature in database
GPG key ID: 60C51A657EEF2D87
5 changed files with 8 additions and 42 deletions

View file

@ -12,7 +12,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_VERIFY_SSL
from .const import PLATFORMS, DOMAIN, RUN_SCRIPT_COMMAND, DEFAULT_VERIFY_SSL
from .const import PLATFORMS, DOMAIN, DEFAULT_VERIFY_SSL
from .coordinator import MikrotikData, MikrotikCoordinator, MikrotikTrackerCoordinator
SCRIPT_SCHEMA = vol.Schema(
@ -40,10 +40,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
config_entry.async_on_unload(config_entry.add_update_listener(async_reload_entry))
hass.services.async_register(
DOMAIN, RUN_SCRIPT_COMMAND, coordinator.run_script, schema=SCRIPT_SCHEMA
)
return True
@ -64,7 +60,6 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
if unload_ok := await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
):
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok

View file

@ -14,6 +14,7 @@ from .button_types import (
SENSOR_TYPES,
SENSOR_SERVICES,
)
from .exceptions import ApiEntryNotFound
_LOGGER = getLogger(__name__)
@ -54,5 +55,8 @@ class MikrotikScriptButton(MikrotikButton):
"""Representation of a script button."""
async def async_press(self) -> None:
"""Process the button press."""
self.coordinator.run_script(self._data["name"])
"""Run script using Mikrotik API"""
try:
self.coordinator.api.run_script(self._data["name"])
except ApiEntryNotFound as error:
_LOGGER.error("Failed to run script: %s", error)

View file

@ -15,8 +15,6 @@ DOMAIN = "mikrotik_router"
DEFAULT_NAME = "Mikrotik Router"
ATTRIBUTION = "Data provided by Mikrotik"
RUN_SCRIPT_COMMAND = "run_script"
DEFAULT_ENCODING = "ISO-8859-1"
DEFAULT_LOGIN_METHOD = "plain"

View file

@ -64,7 +64,6 @@ from .const import (
CONF_SENSOR_NETWATCH_TRACKER,
DEFAULT_SENSOR_NETWATCH_TRACKER,
)
from .exceptions import ApiEntryNotFound
from .apiparser import parse_api
from .mikrotikapi import MikrotikAPI
@ -463,28 +462,6 @@ class MikrotikCoordinator(DataUpdateCoordinator[None]):
"""Change value using Mikrotik API"""
return self.api.execute(path, command, param, value, attributes)
# ---------------------------
# run_script
# ---------------------------
def run_script(self, name):
"""Run script using Mikrotik API"""
if type(name) != str:
if "router" not in name.data:
return
if self.config_entry.data["name"] != name.data.get("router"):
return
if "script" in name.data:
name = name.data.get("script")
else:
return
try:
self.api.run_script(name)
except ApiEntryNotFound as error:
_LOGGER.error("Failed to run script: %s", error)
# ---------------------------
# get_capabilities
# ---------------------------

View file

@ -1,9 +1 @@
run_script:
description: Run script on Mikrotik
fields:
router:
description: Name of the router
example: "Mikrotik"
script:
description: Name of the script
example: "MyScript"
---