diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index a6ce7c0..0aaa8d3 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -134,11 +134,10 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches): # Switch type name [ "interface", + "nat", ], # Entity function - [ - MikrotikControllerPortSwitch, - ], + [MikrotikControllerPortSwitch, MikrotikControllerNATSwitch], ): uid_switch = SWITCH_TYPES[switch] for uid in mikrotik_controller.data[SWITCH_TYPES[switch].data_path]: @@ -460,10 +459,6 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch): class MikrotikControllerNATSwitch(MikrotikControllerSwitch): """Representation of a NAT 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.""" @@ -472,43 +467,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): return f"{self._inst} NAT {self._data['name']}" - @property - def unique_id(self) -> str: - """Return a unique id for this entity.""" - return f"{self._inst.lower()}-enable_nat-{self._data['uniq-id']}" - - @property - def icon(self) -> str: - """Return the icon.""" - if not self._data["enabled"]: - icon = "mdi:network-off-outline" - else: - icon = "mdi:network-outline" - - 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", - "NAT", - ) - }, - "manufacturer": self._ctrl.data["resource"]["platform"], - "model": self._ctrl.data["resource"]["board-name"], - "name": f"{self._inst} NAT", - } - return info - async def async_turn_on(self) -> None: """Turn on the switch.""" - path = "/ip/firewall/nat" + path = self.entity_description.data_switch_path param = ".id" value = None for uid in self._ctrl.data["nat"]: @@ -519,14 +480,13 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): ): value = self._ctrl.data["nat"][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/nat" + path = self.entity_description.data_switch_path param = ".id" value = None for uid in self._ctrl.data["nat"]: @@ -537,9 +497,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): ): value = self._ctrl.data["nat"][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 3e64deb..6cb3ba7 100644 --- a/custom_components/mikrotik_router/switch_types.py +++ b/custom_components/mikrotik_router/switch_types.py @@ -147,7 +147,6 @@ class MikrotikSwitchEntityDescription(SwitchEntityDescription): ha_connection: str = "" ha_connection_value: str = "" data_path: str = "" - data_attribute: str = "" data_is_on: str = "enabled" data_switch_path: str = "" data_switch_parameter: str = "disabled" @@ -169,11 +168,27 @@ SWITCH_TYPES = { ha_connection=CONNECTION_NETWORK_MAC, ha_connection_value="data__port-mac-address", data_path="interface", - data_attribute="temperature", data_switch_path="/interface", data_name="name", data_uid="name", data_reference="default-name", data_attributes_list=DEVICE_ATTRIBUTES_IFACE, ), + "nat": MikrotikSwitchEntityDescription( + key="nat", + name="NAT", + icon_enabled="network-outline", + icon_disabled="network-off-outline", + entity_category=None, + ha_group="NAT", + ha_connection=DOMAIN, + ha_connection_value="NAT", + data_path="nat", + data_switch_path="/ip/firewall/nat", + data_name="name", + data_name_comment=True, + data_uid="uniq-id", + data_reference="uniq-id", + data_attributes_list=DEVICE_ATTRIBUTES_NAT, + ), }