2019-12-03 01:49:25 +01:00
|
|
|
"""Support for the Mikrotik Router switches."""
|
2023-08-09 09:37:48 +02:00
|
|
|
from __future__ import annotations
|
2020-12-25 20:28:36 +01:00
|
|
|
|
2023-08-09 09:37:48 +02:00
|
|
|
from logging import getLogger
|
2022-02-03 10:28:22 +01:00
|
|
|
from collections.abc import Mapping
|
2023-08-09 09:37:48 +02:00
|
|
|
from typing import Any, Optional
|
|
|
|
|
2020-05-20 16:01:32 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2023-08-09 09:37:48 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-03 01:49:25 +01:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2023-08-09 09:37:48 +02:00
|
|
|
|
|
|
|
from .entity import MikrotikEntity, async_add_entities
|
2022-02-02 22:13:39 +01:00
|
|
|
from .helper import format_attribute
|
2022-02-03 10:28:22 +01:00
|
|
|
from .switch_types import (
|
2022-03-27 03:29:36 +02:00
|
|
|
SENSOR_TYPES,
|
|
|
|
SENSOR_SERVICES,
|
2022-02-03 10:28:22 +01:00
|
|
|
DEVICE_ATTRIBUTES_IFACE_ETHER,
|
|
|
|
DEVICE_ATTRIBUTES_IFACE_SFP,
|
2022-08-21 22:27:49 +02:00
|
|
|
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
|
2022-02-03 10:28:22 +01:00
|
|
|
)
|
2019-12-03 01:49:25 +01:00
|
|
|
|
2023-08-09 09:37:48 +02:00
|
|
|
_LOGGER = getLogger(__name__)
|
2019-12-03 01:49:25 +01:00
|
|
|
|
2020-12-25 11:48:24 +01:00
|
|
|
|
2019-12-03 01:49:25 +01:00
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
2023-08-09 09:37:48 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
_async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-03-27 03:29:36 +02:00
|
|
|
"""Set up entry for component"""
|
|
|
|
dispatcher = {
|
|
|
|
"MikrotikSwitch": MikrotikSwitch,
|
|
|
|
"MikrotikPortSwitch": MikrotikPortSwitch,
|
|
|
|
"MikrotikNATSwitch": MikrotikNATSwitch,
|
|
|
|
"MikrotikMangleSwitch": MikrotikMangleSwitch,
|
|
|
|
"MikrotikFilterSwitch": MikrotikFilterSwitch,
|
|
|
|
"MikrotikQueueSwitch": MikrotikQueueSwitch,
|
|
|
|
"MikrotikKidcontrolPauseSwitch": MikrotikKidcontrolPauseSwitch,
|
|
|
|
}
|
2023-08-09 09:37:48 +02:00
|
|
|
await async_add_entities(hass, config_entry, dispatcher)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 01:49:25 +01:00
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikSwitch
|
2019-12-03 01:49:25 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikSwitch(MikrotikEntity, SwitchEntity, RestoreEntity):
|
2019-12-06 01:22:34 +01:00
|
|
|
"""Representation of a switch."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2020-04-20 08:39:02 +02:00
|
|
|
@property
|
2020-12-25 20:28:36 +01:00
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if device is on."""
|
2022-03-27 03:29:36 +02:00
|
|
|
return self._data[self.entity_description.data_attribute]
|
2020-04-20 08:39:02 +02:00
|
|
|
|
|
|
|
@property
|
2022-02-03 10:28:22 +01:00
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon."""
|
2022-03-27 03:29:36 +02:00
|
|
|
if self._data[self.entity_description.data_attribute]:
|
2022-02-03 10:28:22 +01:00
|
|
|
return self.entity_description.icon_enabled
|
|
|
|
else:
|
|
|
|
return self.entity_description.icon_disabled
|
2020-04-20 08:39:02 +02:00
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2020-12-25 23:31:51 +01:00
|
|
|
"""Required abstract method."""
|
|
|
|
pass
|
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2020-12-25 23:31:51 +01:00
|
|
|
"""Required abstract method."""
|
|
|
|
pass
|
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
async def async_turn_on(self) -> None:
|
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
|
|
|
param = self.entity_description.data_reference
|
|
|
|
value = self._data[self.entity_description.data_reference]
|
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, False)
|
|
|
|
await self._ctrl.force_update()
|
|
|
|
|
|
|
|
async def async_turn_off(self) -> None:
|
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
|
|
|
param = self.entity_description.data_reference
|
|
|
|
value = self._data[self.entity_description.data_reference]
|
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, True)
|
|
|
|
await self._ctrl.async_update()
|
|
|
|
|
2019-12-03 18:30:45 +01:00
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikPortSwitch
|
2019-12-03 18:30:45 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikPortSwitch(MikrotikSwitch):
|
2019-12-03 18:30:45 +01:00
|
|
|
"""Representation of a network port switch."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 01:49:25 +01:00
|
|
|
@property
|
2022-02-03 10:28:22 +01:00
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
2021-04-12 14:28:39 +02:00
|
|
|
"""Return the state attributes."""
|
2022-02-03 10:28:22 +01:00
|
|
|
attributes = super().extra_state_attributes
|
2021-04-12 14:28:39 +02:00
|
|
|
|
2021-12-15 23:07:11 +01:00
|
|
|
if self._data["type"] == "ether":
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_ETHER:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
2021-04-12 14:28:39 +02:00
|
|
|
|
2021-12-15 23:07:11 +01:00
|
|
|
if "sfp-shutdown-temperature" in self._data:
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_SFP:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
2022-08-21 22:27:49 +02:00
|
|
|
elif self._data["type"] == "wlan":
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_WIRELESS:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
2021-04-12 14:28:39 +02:00
|
|
|
return attributes
|
|
|
|
|
2019-12-03 01:49:25 +01:00
|
|
|
@property
|
2020-12-25 20:28:36 +01:00
|
|
|
def icon(self) -> str:
|
2019-12-03 01:49:25 +01:00
|
|
|
"""Return the icon."""
|
2020-03-16 04:51:41 +01:00
|
|
|
if self._data["running"]:
|
2022-02-03 10:28:22 +01:00
|
|
|
icon = self.entity_description.icon_enabled
|
2019-12-03 01:49:25 +01:00
|
|
|
else:
|
2022-02-03 10:28:22 +01:00
|
|
|
icon = self.entity_description.icon_disabled
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2020-03-16 04:51:41 +01:00
|
|
|
if not self._data["enabled"]:
|
|
|
|
icon = "mdi:lan-disconnect"
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 01:49:25 +01:00
|
|
|
return icon
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2020-12-25 23:31:51 +01:00
|
|
|
async def async_turn_on(self) -> Optional[str]:
|
2019-12-03 01:49:25 +01:00
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
|
|
|
param = self.entity_description.data_reference
|
2020-12-14 15:54:32 +01:00
|
|
|
if self._data["about"] == "managed by CAPsMAN":
|
|
|
|
_LOGGER.error("Unable to enable %s, managed by CAPsMAN", self._data[param])
|
|
|
|
return "managed by CAPsMAN"
|
2020-04-13 09:16:33 +02:00
|
|
|
if "-" in self._data["port-mac-address"]:
|
|
|
|
param = "name"
|
2022-02-03 10:28:22 +01:00
|
|
|
value = self._data[self.entity_description.data_reference]
|
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, False)
|
2020-12-02 09:29:52 +01:00
|
|
|
|
2021-03-01 16:50:30 +01:00
|
|
|
if "poe-out" in self._data and self._data["poe-out"] == "off":
|
2020-12-02 09:29:52 +01:00
|
|
|
path = "/interface/ethernet"
|
|
|
|
self._ctrl.set_value(path, param, value, "poe-out", "auto-on")
|
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
await self._ctrl.force_update()
|
2019-12-03 01:49:25 +01:00
|
|
|
|
2020-12-25 23:31:51 +01:00
|
|
|
async def async_turn_off(self) -> Optional[str]:
|
2020-12-25 20:28:36 +01:00
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-03 10:28:22 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
|
|
|
param = self.entity_description.data_reference
|
2020-12-14 15:54:32 +01:00
|
|
|
if self._data["about"] == "managed by CAPsMAN":
|
|
|
|
_LOGGER.error("Unable to disable %s, managed by CAPsMAN", self._data[param])
|
|
|
|
return "managed by CAPsMAN"
|
2020-04-13 09:16:33 +02:00
|
|
|
if "-" in self._data["port-mac-address"]:
|
|
|
|
param = "name"
|
2022-02-03 10:28:22 +01:00
|
|
|
value = self._data[self.entity_description.data_reference]
|
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, True)
|
2020-12-02 09:29:52 +01:00
|
|
|
|
2021-03-01 20:56:48 +01:00
|
|
|
if "poe-out" in self._data and self._data["poe-out"] == "auto-on":
|
2020-12-02 09:29:52 +01:00
|
|
|
path = "/interface/ethernet"
|
|
|
|
self._ctrl.set_value(path, param, value, "poe-out", "off")
|
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
await self._ctrl.async_update()
|
2019-12-03 01:49:25 +01:00
|
|
|
|
2020-12-02 15:38:17 +01:00
|
|
|
|
2019-12-03 18:30:45 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikNATSwitch
|
2019-12-03 18:30:45 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikNATSwitch(MikrotikSwitch):
|
2019-12-03 18:30:45 +01:00
|
|
|
"""Representation of a NAT switch."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2020-12-25 20:28:36 +01:00
|
|
|
async def async_turn_on(self) -> None:
|
2019-12-03 18:30:45 +01:00
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 20:34:51 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2020-03-16 04:51:41 +01:00
|
|
|
param = ".id"
|
2019-12-07 21:18:27 +01:00
|
|
|
value = None
|
2020-03-16 04:51:41 +01:00
|
|
|
for uid in self._ctrl.data["nat"]:
|
2020-12-25 23:31:51 +01:00
|
|
|
if self._ctrl.data["nat"][uid]["uniq-id"] == (
|
|
|
|
f"{self._data['chain']},{self._data['action']},{self._data['protocol']},"
|
|
|
|
f"{self._data['in-interface']}:{self._data['dst-port']}-"
|
|
|
|
f"{self._data['out-interface']}:{self._data['to-addresses']}:{self._data['to-ports']}"
|
2020-03-16 04:51:41 +01:00
|
|
|
):
|
|
|
|
value = self._ctrl.data["nat"][uid][".id"]
|
2019-12-07 21:18:27 +01:00
|
|
|
|
2022-02-04 20:34:51 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, False)
|
2019-12-06 01:22:34 +01:00
|
|
|
await self._ctrl.force_update()
|
2019-12-03 18:30:45 +01:00
|
|
|
|
2020-12-25 20:28:36 +01:00
|
|
|
async def async_turn_off(self) -> None:
|
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 20:34:51 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2020-03-16 04:51:41 +01:00
|
|
|
param = ".id"
|
2019-12-07 21:18:27 +01:00
|
|
|
value = None
|
2020-03-16 04:51:41 +01:00
|
|
|
for uid in self._ctrl.data["nat"]:
|
2020-12-25 23:31:51 +01:00
|
|
|
if self._ctrl.data["nat"][uid]["uniq-id"] == (
|
|
|
|
f"{self._data['chain']},{self._data['action']},{self._data['protocol']},"
|
|
|
|
f"{self._data['in-interface']}:{self._data['dst-port']}-"
|
|
|
|
f"{self._data['out-interface']}:{self._data['to-addresses']}:{self._data['to-ports']}"
|
2020-03-16 04:51:41 +01:00
|
|
|
):
|
|
|
|
value = self._ctrl.data["nat"][uid][".id"]
|
2019-12-07 21:18:27 +01:00
|
|
|
|
2022-02-04 20:34:51 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, True)
|
2019-12-06 01:22:34 +01:00
|
|
|
await self._ctrl.async_update()
|
2019-12-03 18:30:45 +01:00
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
|
2020-12-18 19:58:54 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikMangleSwitch
|
2020-12-18 19:58:54 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikMangleSwitch(MikrotikSwitch):
|
2020-12-18 19:58:54 +01:00
|
|
|
"""Representation of a Mangle switch."""
|
|
|
|
|
2020-12-25 20:28:36 +01:00
|
|
|
async def async_turn_on(self) -> None:
|
2020-12-18 19:58:54 +01:00
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 20:44:59 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2020-12-18 19:58:54 +01:00
|
|
|
param = ".id"
|
|
|
|
value = None
|
|
|
|
for uid in self._ctrl.data["mangle"]:
|
2020-12-25 23:31:51 +01:00
|
|
|
if self._ctrl.data["mangle"][uid]["uniq-id"] == (
|
|
|
|
f"{self._data['chain']},{self._data['action']},{self._data['protocol']},"
|
|
|
|
f"{self._data['src-address']}:{self._data['src-port']}-"
|
2021-08-24 18:24:41 +07:00
|
|
|
f"{self._data['dst-address']}:{self._data['dst-port']},"
|
2021-08-24 19:24:50 +07:00
|
|
|
f"{self._data['src-address-list']}-{self._data['dst-address-list']}"
|
2020-12-18 19:58:54 +01:00
|
|
|
):
|
|
|
|
value = self._ctrl.data["mangle"][uid][".id"]
|
|
|
|
|
2022-02-04 20:44:59 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, False)
|
2020-12-18 19:58:54 +01:00
|
|
|
await self._ctrl.force_update()
|
|
|
|
|
2020-12-25 20:28:36 +01:00
|
|
|
async def async_turn_off(self) -> None:
|
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 20:44:59 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2020-12-18 19:58:54 +01:00
|
|
|
param = ".id"
|
|
|
|
value = None
|
|
|
|
for uid in self._ctrl.data["mangle"]:
|
2020-12-25 23:31:51 +01:00
|
|
|
if self._ctrl.data["mangle"][uid]["uniq-id"] == (
|
|
|
|
f"{self._data['chain']},{self._data['action']},{self._data['protocol']},"
|
|
|
|
f"{self._data['src-address']}:{self._data['src-port']}-"
|
2021-08-24 18:24:41 +07:00
|
|
|
f"{self._data['dst-address']}:{self._data['dst-port']},"
|
2021-08-24 19:24:50 +07:00
|
|
|
f"{self._data['src-address-list']}-{self._data['dst-address-list']}"
|
2020-12-18 19:58:54 +01:00
|
|
|
):
|
|
|
|
value = self._ctrl.data["mangle"][uid][".id"]
|
|
|
|
|
2022-02-04 20:44:59 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, True)
|
2020-12-18 19:58:54 +01:00
|
|
|
await self._ctrl.async_update()
|
|
|
|
|
|
|
|
|
2021-04-12 12:40:45 +02:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikFilterSwitch
|
2021-04-12 12:40:45 +02:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikFilterSwitch(MikrotikSwitch):
|
2021-04-12 12:40:45 +02:00
|
|
|
"""Representation of a Filter switch."""
|
|
|
|
|
|
|
|
async def async_turn_on(self) -> None:
|
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 20:52:22 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2021-04-12 12:40:45 +02:00
|
|
|
param = ".id"
|
|
|
|
value = None
|
|
|
|
for uid in self._ctrl.data["filter"]:
|
|
|
|
if self._ctrl.data["filter"][uid]["uniq-id"] == (
|
|
|
|
f"{self._data['chain']},{self._data['action']},{self._data['protocol']},{self._data['layer7-protocol']},"
|
2022-02-18 11:11:14 +01:00
|
|
|
f"{self._data['in-interface']},{self._data['in-interface-list']}:{self._data['src-address']},{self._data['src-address-list']}:{self._data['src-port']}-"
|
|
|
|
f"{self._data['out-interface']},{self._data['out-interface-list']}:{self._data['dst-address']},{self._data['dst-address-list']}:{self._data['dst-port']}"
|
2021-04-12 12:40:45 +02:00
|
|
|
):
|
|
|
|
value = self._ctrl.data["filter"][uid][".id"]
|
|
|
|
|
2022-02-04 20:52:22 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, False)
|
2021-04-12 12:40:45 +02:00
|
|
|
await self._ctrl.force_update()
|
|
|
|
|
|
|
|
async def async_turn_off(self) -> None:
|
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 20:52:22 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2021-04-12 12:40:45 +02:00
|
|
|
param = ".id"
|
|
|
|
value = None
|
|
|
|
for uid in self._ctrl.data["filter"]:
|
|
|
|
if self._ctrl.data["filter"][uid]["uniq-id"] == (
|
|
|
|
f"{self._data['chain']},{self._data['action']},{self._data['protocol']},{self._data['layer7-protocol']},"
|
2022-02-18 11:11:14 +01:00
|
|
|
f"{self._data['in-interface']},{self._data['in-interface-list']}:{self._data['src-address']},{self._data['src-address-list']}:{self._data['src-port']}-"
|
|
|
|
f"{self._data['out-interface']},{self._data['out-interface-list']}:{self._data['dst-address']},{self._data['dst-address-list']}:{self._data['dst-port']}"
|
2021-04-12 12:40:45 +02:00
|
|
|
):
|
|
|
|
value = self._ctrl.data["filter"][uid][".id"]
|
|
|
|
|
2022-02-04 20:52:22 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, True)
|
2021-04-12 12:40:45 +02:00
|
|
|
await self._ctrl.async_update()
|
|
|
|
|
|
|
|
|
2020-03-25 13:18:49 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikQueueSwitch
|
2020-03-25 13:18:49 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikQueueSwitch(MikrotikSwitch):
|
2020-03-25 13:18:49 +01:00
|
|
|
"""Representation of a queue switch."""
|
|
|
|
|
2020-12-25 20:28:36 +01:00
|
|
|
async def async_turn_on(self) -> None:
|
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 21:24:24 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2020-03-25 13:18:49 +01:00
|
|
|
param = ".id"
|
|
|
|
value = None
|
|
|
|
for uid in self._ctrl.data["queue"]:
|
2020-04-11 05:45:36 +02:00
|
|
|
if self._ctrl.data["queue"][uid]["name"] == f"{self._data['name']}":
|
2020-03-25 13:18:49 +01:00
|
|
|
value = self._ctrl.data["queue"][uid][".id"]
|
|
|
|
|
2022-02-04 21:24:24 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, False)
|
2020-03-25 13:18:49 +01:00
|
|
|
await self._ctrl.force_update()
|
|
|
|
|
2020-12-25 20:28:36 +01:00
|
|
|
async def async_turn_off(self) -> None:
|
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 21:24:24 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
2020-03-25 13:18:49 +01:00
|
|
|
param = ".id"
|
|
|
|
value = None
|
|
|
|
for uid in self._ctrl.data["queue"]:
|
2020-04-11 05:45:36 +02:00
|
|
|
if self._ctrl.data["queue"][uid]["name"] == f"{self._data['name']}":
|
2020-03-25 13:18:49 +01:00
|
|
|
value = self._ctrl.data["queue"][uid][".id"]
|
|
|
|
|
2022-02-04 21:24:24 +01:00
|
|
|
mod_param = self.entity_description.data_switch_parameter
|
|
|
|
self._ctrl.set_value(path, param, value, mod_param, True)
|
2020-12-25 18:42:57 +01:00
|
|
|
await self._ctrl.async_update()
|
2022-02-04 21:25:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikKidcontrolPauseSwitch
|
2022-02-04 21:25:01 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikKidcontrolPauseSwitch(MikrotikSwitch):
|
2022-02-04 21:25:01 +01:00
|
|
|
"""Representation of a queue switch."""
|
|
|
|
|
|
|
|
async def async_turn_on(self) -> None:
|
|
|
|
"""Turn on the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 21:25:01 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
|
|
|
param = self.entity_description.data_reference
|
|
|
|
value = self._data[self.entity_description.data_reference]
|
|
|
|
command = "resume"
|
|
|
|
self._ctrl.execute(path, command, param, value)
|
|
|
|
await self._ctrl.force_update()
|
|
|
|
|
|
|
|
async def async_turn_off(self) -> None:
|
|
|
|
"""Turn off the switch."""
|
2022-08-20 01:42:03 +02:00
|
|
|
if "write" not in self._ctrl.data["access"]:
|
|
|
|
return
|
|
|
|
|
2022-02-04 21:25:01 +01:00
|
|
|
path = self.entity_description.data_switch_path
|
|
|
|
param = self.entity_description.data_reference
|
|
|
|
value = self._data[self.entity_description.data_reference]
|
|
|
|
command = "pause"
|
|
|
|
self._ctrl.execute(path, command, param, value)
|
|
|
|
await self._ctrl.async_update()
|