mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-24 04:24:29 +02:00
Reworked scripts to buttons only, fixes #414
This commit is contained in:
parent
ee886e0365
commit
6487398741
5 changed files with 8 additions and 42 deletions
|
@ -12,7 +12,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||||
|
|
||||||
from homeassistant.const import CONF_VERIFY_SSL
|
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
|
from .coordinator import MikrotikData, MikrotikCoordinator, MikrotikTrackerCoordinator
|
||||||
|
|
||||||
SCRIPT_SCHEMA = vol.Schema(
|
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))
|
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
|
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(
|
if unload_ok := await hass.config_entries.async_unload_platforms(
|
||||||
config_entry, PLATFORMS
|
config_entry, PLATFORMS
|
||||||
):
|
):
|
||||||
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
|
|
||||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
|
@ -14,6 +14,7 @@ from .button_types import (
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
SENSOR_SERVICES,
|
SENSOR_SERVICES,
|
||||||
)
|
)
|
||||||
|
from .exceptions import ApiEntryNotFound
|
||||||
|
|
||||||
_LOGGER = getLogger(__name__)
|
_LOGGER = getLogger(__name__)
|
||||||
|
|
||||||
|
@ -54,5 +55,8 @@ class MikrotikScriptButton(MikrotikButton):
|
||||||
"""Representation of a script button."""
|
"""Representation of a script button."""
|
||||||
|
|
||||||
async def async_press(self) -> None:
|
async def async_press(self) -> None:
|
||||||
"""Process the button press."""
|
"""Run script using Mikrotik API"""
|
||||||
self.coordinator.run_script(self._data["name"])
|
try:
|
||||||
|
self.coordinator.api.run_script(self._data["name"])
|
||||||
|
except ApiEntryNotFound as error:
|
||||||
|
_LOGGER.error("Failed to run script: %s", error)
|
||||||
|
|
|
@ -15,8 +15,6 @@ DOMAIN = "mikrotik_router"
|
||||||
DEFAULT_NAME = "Mikrotik Router"
|
DEFAULT_NAME = "Mikrotik Router"
|
||||||
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"
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,6 @@ from .const import (
|
||||||
CONF_SENSOR_NETWATCH_TRACKER,
|
CONF_SENSOR_NETWATCH_TRACKER,
|
||||||
DEFAULT_SENSOR_NETWATCH_TRACKER,
|
DEFAULT_SENSOR_NETWATCH_TRACKER,
|
||||||
)
|
)
|
||||||
from .exceptions import ApiEntryNotFound
|
|
||||||
from .apiparser import parse_api
|
from .apiparser import parse_api
|
||||||
from .mikrotikapi import MikrotikAPI
|
from .mikrotikapi import MikrotikAPI
|
||||||
|
|
||||||
|
@ -463,28 +462,6 @@ class MikrotikCoordinator(DataUpdateCoordinator[None]):
|
||||||
"""Change value using Mikrotik API"""
|
"""Change value using Mikrotik API"""
|
||||||
return self.api.execute(path, command, param, value, attributes)
|
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
|
# get_capabilities
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
|
|
@ -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"
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue