diff --git a/custom_components/mikrotik_router/const.py b/custom_components/mikrotik_router/const.py index 8575162..6b02b5e 100644 --- a/custom_components/mikrotik_router/const.py +++ b/custom_components/mikrotik_router/const.py @@ -5,7 +5,7 @@ PLATFORMS = [ Platform.SENSOR, Platform.BINARY_SENSOR, # Platform.DEVICE_TRACKER, - # Platform.SWITCH, + Platform.SWITCH, # Platform.BUTTON, # Platform.UPDATE, ] diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index 0be9541..c16d626 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -1,12 +1,18 @@ """Support for the Mikrotik Router switches.""" +from __future__ import annotations -import logging -from typing import Any, Optional +from logging import getLogger from collections.abc import Mapping +from typing import Any, Optional + from homeassistant.components.switch import SwitchEntity +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 + +from .entity import MikrotikEntity, async_add_entities from .helper import format_attribute -from .entity import model_async_setup_entry, MikrotikEntity from .switch_types import ( SENSOR_TYPES, SENSOR_SERVICES, @@ -15,13 +21,17 @@ from .switch_types import ( DEVICE_ATTRIBUTES_IFACE_WIRELESS, ) -_LOGGER = logging.getLogger(__name__) +_LOGGER = getLogger(__name__) # --------------------------- # async_setup_entry # --------------------------- -async def async_setup_entry(hass, config_entry, async_add_entities): +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + _async_add_entities: AddEntitiesCallback, +) -> None: """Set up entry for component""" dispatcher = { "MikrotikSwitch": MikrotikSwitch, @@ -32,14 +42,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): "MikrotikQueueSwitch": MikrotikQueueSwitch, "MikrotikKidcontrolPauseSwitch": MikrotikKidcontrolPauseSwitch, } - await model_async_setup_entry( - hass, - config_entry, - async_add_entities, - SENSOR_SERVICES, - SENSOR_TYPES, - dispatcher, - ) + await async_add_entities(hass, config_entry, dispatcher) # --------------------------- diff --git a/custom_components/mikrotik_router/switch_types.py b/custom_components/mikrotik_router/switch_types.py index efccd05..fd59b9a 100644 --- a/custom_components/mikrotik_router/switch_types.py +++ b/custom_components/mikrotik_router/switch_types.py @@ -1,4 +1,6 @@ """Definitions for Mikrotik Router switch entities.""" +from __future__ import annotations + from dataclasses import dataclass, field from typing import List from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC @@ -164,25 +166,25 @@ class MikrotikSwitchEntityDescription(SwitchEntityDescription): device_class: str = SwitchDeviceClass.SWITCH - icon_enabled: str = "" - icon_disabled: str = "" - ha_group: str = "" - ha_connection: str = "" - ha_connection_value: str = "" - data_path: str = "" + icon_enabled: str | None = None + icon_disabled: str | None = None + ha_group: str | None = None + ha_connection: str | None = None + ha_connection_value: str | None = None + data_path: str | None = None data_attribute: str = "enabled" - data_switch_path: str = "" + data_switch_path: str | None = None data_switch_parameter: str = "disabled" - data_name: str = "" + data_name: str | None = None data_name_comment: bool = False - data_uid: str = "" - data_reference: str = "" + data_uid: str | None = None + data_reference: str | None = None data_attributes_list: List = field(default_factory=lambda: []) func: str = "MikrotikSwitch" -SENSOR_TYPES = { - "interface": MikrotikSwitchEntityDescription( +SENSOR_TYPES: tuple[MikrotikSensorEntityDescription, ...] = ( + MikrotikSwitchEntityDescription( key="interface", name="Port", icon_enabled="mdi:lan-connect", @@ -199,7 +201,7 @@ SENSOR_TYPES = { data_attributes_list=DEVICE_ATTRIBUTES_IFACE, func="MikrotikPortSwitch", ), - "nat": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="nat", name="", icon_enabled="mdi:network-outline", @@ -217,7 +219,7 @@ SENSOR_TYPES = { data_attributes_list=DEVICE_ATTRIBUTES_NAT, func="MikrotikNATSwitch", ), - "mangle": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="mangle", name="", icon_enabled="mdi:bookmark-outline", @@ -235,7 +237,7 @@ SENSOR_TYPES = { data_attributes_list=DEVICE_ATTRIBUTES_MANGLE, func="MikrotikMangleSwitch", ), - "filter": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="filter", name="", icon_enabled="mdi:filter-variant", @@ -253,7 +255,7 @@ SENSOR_TYPES = { data_attributes_list=DEVICE_ATTRIBUTES_FILTER, func="MikrotikFilterSwitch", ), - "ppp_secret": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="ppp_secret", name="PPP Secret", icon_enabled="mdi:account-outline", @@ -269,7 +271,7 @@ SENSOR_TYPES = { data_reference="name", data_attributes_list=DEVICE_ATTRIBUTES_PPP_SECRET, ), - "queue": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="queue", name="", icon_enabled="mdi:leaf", @@ -286,7 +288,7 @@ SENSOR_TYPES = { data_attributes_list=DEVICE_ATTRIBUTES_QUEUE, func="MikrotikQueueSwitch", ), - "kidcontrol_enable": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="kidcontrol_enable", name="", icon_enabled="mdi:account", @@ -302,7 +304,7 @@ SENSOR_TYPES = { data_reference="name", data_attributes_list=DEVICE_ATTRIBUTES_KIDCONTROL, ), - "kidcontrol_pause": MikrotikSwitchEntityDescription( + MikrotikSwitchEntityDescription( key="kidcontrol_paused", name="paused", icon_enabled="mdi:account-outline", @@ -320,6 +322,6 @@ SENSOR_TYPES = { data_attributes_list=DEVICE_ATTRIBUTES_KIDCONTROL, func="MikrotikKidcontrolPauseSwitch", ), -} +) SENSOR_SERVICES = {}