tomaae.homeassistant-mikrot.../custom_components/mikrotik_router/switch.py

397 lines
15 KiB
Python
Raw Normal View History

"""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
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
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 (
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
)
2023-08-09 09:37:48 +02:00
_LOGGER = getLogger(__name__)
2020-12-25 11:48:24 +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:
"""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)
# ---------------------------
# MikrotikSwitch
# ---------------------------
class MikrotikSwitch(MikrotikEntity, SwitchEntity, RestoreEntity):
2019-12-06 01:22:34 +01:00
"""Representation of a switch."""
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."""
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."""
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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, False)
await self.coordinator.async_refresh()
2022-02-03 10:28:22 +01:00
async def async_turn_off(self) -> None:
"""Turn off the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, True)
await self.coordinator.async_refresh()
2022-02-03 10:28:22 +01:00
2019-12-03 18:30:45 +01:00
# ---------------------------
# MikrotikPortSwitch
2019-12-03 18:30:45 +01:00
# ---------------------------
class MikrotikPortSwitch(MikrotikSwitch):
2019-12-03 18:30:45 +01:00
"""Representation of a network port switch."""
@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
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
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
@property
2020-12-25 20:28:36 +01:00
def icon(self) -> str:
"""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
else:
2022-02-03 10:28:22 +01:00
icon = self.entity_description.icon_disabled
2020-03-16 04:51:41 +01:00
if not self._data["enabled"]:
icon = "mdi:lan-disconnect"
return icon
2020-12-25 23:31:51 +01:00
async def async_turn_on(self) -> Optional[str]:
"""Turn on the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.data["access"]:
return
2022-02-03 10:28:22 +01:00
path = self.entity_description.data_switch_path
param = self.entity_description.data_reference
if self._data["about"] == "managed by CAPsMAN":
_LOGGER.error("Unable to enable %s, managed by CAPsMAN", self._data[param])
return "managed by CAPsMAN"
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
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, False)
if "poe-out" in self._data and self._data["poe-out"] == "off":
path = "/interface/ethernet"
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, "poe-out", "auto-on")
2023-08-09 09:48:08 +02:00
await self.coordinator.async_refresh()
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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.data["access"]:
return
2022-02-03 10:28:22 +01:00
path = self.entity_description.data_switch_path
param = self.entity_description.data_reference
if self._data["about"] == "managed by CAPsMAN":
_LOGGER.error("Unable to disable %s, managed by CAPsMAN", self._data[param])
return "managed by CAPsMAN"
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
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, True)
if "poe-out" in self._data and self._data["poe-out"] == "auto-on":
path = "/interface/ethernet"
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, "poe-out", "off")
2023-08-09 09:48:08 +02:00
await self.coordinator.async_refresh()
2020-12-02 15:38:17 +01:00
2019-12-03 18:30:45 +01:00
# ---------------------------
# MikrotikNATSwitch
2019-12-03 18:30:45 +01:00
# ---------------------------
class MikrotikNATSwitch(MikrotikSwitch):
2019-12-03 18:30:45 +01:00
"""Representation of a NAT switch."""
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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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"
value = None
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["nat"]:
if self.coordinator.data["nat"][uid]["uniq-id"] == (
2020-12-25 23:31:51 +01:00
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
):
2023-08-09 09:48:08 +02:00
value = self.coordinator.data["nat"][uid][".id"]
2022-02-04 20:34:51 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, False)
await self.coordinator.async_refresh()
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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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"
value = None
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["nat"]:
if self.coordinator.data["nat"][uid]["uniq-id"] == (
2020-12-25 23:31:51 +01:00
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
):
2023-08-09 09:48:08 +02:00
value = self.coordinator.data["nat"][uid][".id"]
2022-02-04 20:34:51 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, True)
await self.coordinator.async_refresh()
2019-12-03 18:30:45 +01:00
2019-12-04 20:13:11 +01:00
2020-12-18 19:58:54 +01:00
# ---------------------------
# MikrotikMangleSwitch
2020-12-18 19:58:54 +01: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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["mangle"]:
if self.coordinator.data["mangle"][uid]["uniq-id"] == (
2020-12-25 23:31:51 +01:00
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
):
2023-08-09 09:48:08 +02:00
value = self.coordinator.data["mangle"][uid][".id"]
2020-12-18 19:58:54 +01:00
2022-02-04 20:44:59 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, False)
await self.coordinator.async_refresh()
2020-12-18 19:58:54 +01:00
2020-12-25 20:28:36 +01:00
async def async_turn_off(self) -> None:
"""Turn off the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["mangle"]:
if self.coordinator.data["mangle"][uid]["uniq-id"] == (
2020-12-25 23:31:51 +01:00
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
):
2023-08-09 09:48:08 +02:00
value = self.coordinator.data["mangle"][uid][".id"]
2020-12-18 19:58:54 +01:00
2022-02-04 20:44:59 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, True)
await self.coordinator.async_refresh()
2020-12-18 19:58:54 +01:00
2021-04-12 12:40:45 +02:00
# ---------------------------
# MikrotikFilterSwitch
2021-04-12 12:40:45 +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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["filter"]:
if self.coordinator.data["filter"][uid]["uniq-id"] == (
2021-04-12 12:40:45 +02:00
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
):
2023-08-09 09:48:08 +02:00
value = self.coordinator.data["filter"][uid][".id"]
2021-04-12 12:40:45 +02:00
2022-02-04 20:52:22 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, False)
await self.coordinator.async_refresh()
2021-04-12 12:40:45 +02:00
async def async_turn_off(self) -> None:
"""Turn off the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["filter"]:
if self.coordinator.data["filter"][uid]["uniq-id"] == (
2021-04-12 12:40:45 +02:00
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
):
2023-08-09 09:48:08 +02:00
value = self.coordinator.data["filter"][uid][".id"]
2021-04-12 12:40:45 +02:00
2022-02-04 20:52:22 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, True)
await self.coordinator.async_refresh()
2021-04-12 12:40:45 +02:00
# ---------------------------
# MikrotikQueueSwitch
# ---------------------------
class MikrotikQueueSwitch(MikrotikSwitch):
"""Representation of a queue switch."""
2020-12-25 20:28:36 +01:00
async def async_turn_on(self) -> None:
"""Turn on the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.data["access"]:
return
2022-02-04 21:24:24 +01:00
path = self.entity_description.data_switch_path
param = ".id"
value = None
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["queue"]:
if self.coordinator.data["queue"][uid]["name"] == f"{self._data['name']}":
value = self.coordinator.data["queue"][uid][".id"]
2022-02-04 21:24:24 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, False)
await self.coordinator.async_refresh()
2020-12-25 20:28:36 +01:00
async def async_turn_off(self) -> None:
"""Turn off the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.data["access"]:
return
2022-02-04 21:24:24 +01:00
path = self.entity_description.data_switch_path
param = ".id"
value = None
2023-08-09 09:48:08 +02:00
for uid in self.coordinator.data["queue"]:
if self.coordinator.data["queue"][uid]["name"] == f"{self._data['name']}":
value = self.coordinator.data["queue"][uid][".id"]
2022-02-04 21:24:24 +01:00
mod_param = self.entity_description.data_switch_parameter
2023-08-09 09:48:08 +02:00
self.coordinator.set_value(path, param, value, mod_param, True)
await self.coordinator.async_refresh()
2022-02-04 21:25:01 +01:00
# ---------------------------
# MikrotikKidcontrolPauseSwitch
2022-02-04 21:25:01 +01: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."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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"
2023-08-09 09:48:08 +02:00
self.coordinator.execute(path, command, param, value)
await self.coordinator.async_refresh()
2022-02-04 21:25:01 +01:00
async def async_turn_off(self) -> None:
"""Turn off the switch."""
2023-08-09 09:48:08 +02:00
if "write" not in self.coordinator.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"
2023-08-09 09:48:08 +02:00
self.coordinator.execute(path, command, param, value)
await self.coordinator.async_refresh()