From 9b2d5c9b68799603526bba7f8343603489e1136c Mon Sep 17 00:00:00 2001 From: Tomaae Date: Fri, 4 Feb 2022 20:52:22 +0100 Subject: [PATCH] redo filter switches --- custom_components/mikrotik_router/switch.py | 80 ++----------------- .../mikrotik_router/switch_types.py | 17 ++++ 2 files changed, 25 insertions(+), 72 deletions(-) diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index 36e7e52..b77efb4 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -20,23 +20,6 @@ from .switch_types import ( _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 = [ "connected", "service", @@ -111,12 +94,13 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches): # Add switches for switch, sid_func in zip( # Switch type name - ["interface", "nat", "mangle"], + ["interface", "nat", "mangle", "filter"], # Entity function [ MikrotikControllerPortSwitch, MikrotikControllerNATSwitch, MikrotikControllerMangleSwitch, + MikrotikControllerFilterSwitch, ], ): uid_switch = SWITCH_TYPES[switch] @@ -531,55 +515,9 @@ class MikrotikControllerMangleSwitch(MikrotikControllerSwitch): class MikrotikControllerFilterSwitch(MikrotikControllerSwitch): """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: """Turn on the switch.""" - path = "/ip/firewall/filter" + path = self.entity_description.data_switch_path param = ".id" value = None for uid in self._ctrl.data["filter"]: @@ -590,14 +528,13 @@ class MikrotikControllerFilterSwitch(MikrotikControllerSwitch): ): value = self._ctrl.data["filter"][uid][".id"] - mod_param = "disabled" - mod_value = False - self._ctrl.set_value(path, param, value, mod_param, mod_value) + 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.""" - path = "/ip/firewall/filter" + path = self.entity_description.data_switch_path param = ".id" value = None for uid in self._ctrl.data["filter"]: @@ -608,9 +545,8 @@ class MikrotikControllerFilterSwitch(MikrotikControllerSwitch): ): value = self._ctrl.data["filter"][uid][".id"] - mod_param = "disabled" - mod_value = True - self._ctrl.set_value(path, param, value, mod_param, mod_value) + mod_param = self.entity_description.data_switch_parameter + self._ctrl.set_value(path, param, value, mod_param, True) await self._ctrl.async_update() diff --git a/custom_components/mikrotik_router/switch_types.py b/custom_components/mikrotik_router/switch_types.py index c823a88..a750fee 100644 --- a/custom_components/mikrotik_router/switch_types.py +++ b/custom_components/mikrotik_router/switch_types.py @@ -208,4 +208,21 @@ SWITCH_TYPES = { data_reference="uniq-id", 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, + ), }