redo filter switches

This commit is contained in:
Tomaae 2022-02-04 20:52:22 +01:00
parent 2b0e364f51
commit 9b2d5c9b68
2 changed files with 25 additions and 72 deletions

View file

@ -20,23 +20,6 @@ from .switch_types import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEVICE_ATTRIBUTES_FILTER = [
"chain",
"action",
"address-list",
"protocol",
"layer7-protocol",
"tcp-flags",
"connection-state",
"in-interface",
"src-address",
"src-port",
"out-interface",
"dst-address",
"dst-port",
"comment",
]
DEVICE_ATTRIBUTES_PPP_SECRET = [ DEVICE_ATTRIBUTES_PPP_SECRET = [
"connected", "connected",
"service", "service",
@ -111,12 +94,13 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches):
# Add switches # Add switches
for switch, sid_func in zip( for switch, sid_func in zip(
# Switch type name # Switch type name
["interface", "nat", "mangle"], ["interface", "nat", "mangle", "filter"],
# Entity function # Entity function
[ [
MikrotikControllerPortSwitch, MikrotikControllerPortSwitch,
MikrotikControllerNATSwitch, MikrotikControllerNATSwitch,
MikrotikControllerMangleSwitch, MikrotikControllerMangleSwitch,
MikrotikControllerFilterSwitch,
], ],
): ):
uid_switch = SWITCH_TYPES[switch] uid_switch = SWITCH_TYPES[switch]
@ -531,55 +515,9 @@ class MikrotikControllerMangleSwitch(MikrotikControllerSwitch):
class MikrotikControllerFilterSwitch(MikrotikControllerSwitch): class MikrotikControllerFilterSwitch(MikrotikControllerSwitch):
"""Representation of a Filter switch.""" """Representation of a Filter switch."""
def __init__(self, inst, uid, mikrotik_controller, sid_data):
"""Initialize."""
super().__init__(inst, uid, mikrotik_controller, sid_data)
@property
def name(self) -> str:
"""Return the name."""
if self._data["comment"]:
return f"{self._inst} Filter {self._data['comment']}"
return f"{self._inst} Filter {self._data['name']}"
@property
def unique_id(self) -> str:
"""Return a unique id for this entity."""
return f"{self._inst.lower()}-enable_filter-{self._data['uniq-id']}"
@property
def icon(self) -> str:
"""Return the icon."""
if not self._data["enabled"]:
icon = "mdi:filter-variant-remove"
else:
icon = "mdi:filter-variant"
return icon
@property
def device_info(self) -> Dict[str, Any]:
"""Return a description for device registry."""
info = {
"identifiers": {
(
DOMAIN,
"serial-number",
f"{self._ctrl.data['routerboard']['serial-number']}",
"switch",
"Filter",
)
},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": f"{self._inst} Filter",
}
return info
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn on the switch.""" """Turn on the switch."""
path = "/ip/firewall/filter" path = self.entity_description.data_switch_path
param = ".id" param = ".id"
value = None value = None
for uid in self._ctrl.data["filter"]: for uid in self._ctrl.data["filter"]:
@ -590,14 +528,13 @@ class MikrotikControllerFilterSwitch(MikrotikControllerSwitch):
): ):
value = self._ctrl.data["filter"][uid][".id"] value = self._ctrl.data["filter"][uid][".id"]
mod_param = "disabled" mod_param = self.entity_description.data_switch_parameter
mod_value = False self._ctrl.set_value(path, param, value, mod_param, False)
self._ctrl.set_value(path, param, value, mod_param, mod_value)
await self._ctrl.force_update() await self._ctrl.force_update()
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn off the switch.""" """Turn off the switch."""
path = "/ip/firewall/filter" path = self.entity_description.data_switch_path
param = ".id" param = ".id"
value = None value = None
for uid in self._ctrl.data["filter"]: for uid in self._ctrl.data["filter"]:
@ -608,9 +545,8 @@ class MikrotikControllerFilterSwitch(MikrotikControllerSwitch):
): ):
value = self._ctrl.data["filter"][uid][".id"] value = self._ctrl.data["filter"][uid][".id"]
mod_param = "disabled" mod_param = self.entity_description.data_switch_parameter
mod_value = True self._ctrl.set_value(path, param, value, mod_param, True)
self._ctrl.set_value(path, param, value, mod_param, mod_value)
await self._ctrl.async_update() await self._ctrl.async_update()

View file

@ -208,4 +208,21 @@ SWITCH_TYPES = {
data_reference="uniq-id", data_reference="uniq-id",
data_attributes_list=DEVICE_ATTRIBUTES_MANGLE, data_attributes_list=DEVICE_ATTRIBUTES_MANGLE,
), ),
"filter": MikrotikSwitchEntityDescription(
key="filter",
name="Filter",
icon_enabled="mdi:filter-variant",
icon_disabled="mdi:filter-variant-remove",
entity_category=None,
ha_group="Filter",
ha_connection=DOMAIN,
ha_connection_value="Filter",
data_path="filter",
data_switch_path="/ip/firewall/filter",
data_name="name",
data_name_comment=True,
data_uid="uniq-id",
data_reference="uniq-id",
data_attributes_list=DEVICE_ATTRIBUTES_FILTER,
),
} }