mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-31 23:10:05 +02:00
added support for per entity services
refactored HA entity implementation
This commit is contained in:
parent
40d98f4778
commit
d863afd7ca
11 changed files with 525 additions and 1025 deletions
|
@ -4,16 +4,12 @@ import logging
|
|||
from typing import Any, Optional
|
||||
from collections.abc import Mapping
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.const import CONF_NAME, CONF_HOST, ATTR_ATTRIBUTION
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from .helper import format_attribute
|
||||
from .const import DOMAIN, ATTRIBUTION
|
||||
from .model import model_async_setup_entry, MikrotikEntity
|
||||
from .switch_types import (
|
||||
MikrotikSwitchEntityDescription,
|
||||
SWITCH_TYPES,
|
||||
SENSOR_TYPES,
|
||||
SENSOR_SERVICES,
|
||||
DEVICE_ATTRIBUTES_IFACE_ETHER,
|
||||
DEVICE_ATTRIBUTES_IFACE_SFP,
|
||||
)
|
||||
|
@ -25,142 +21,45 @@ _LOGGER = logging.getLogger(__name__)
|
|||
# async_setup_entry
|
||||
# ---------------------------
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up switches for Mikrotik Router component."""
|
||||
inst = config_entry.data[CONF_NAME]
|
||||
mikrotik_controller = hass.data[DOMAIN][config_entry.entry_id]
|
||||
switches = {}
|
||||
|
||||
@callback
|
||||
def update_controller():
|
||||
"""Update the values of the controller."""
|
||||
update_items(inst, mikrotik_controller, async_add_entities, switches)
|
||||
|
||||
mikrotik_controller.listeners.append(
|
||||
async_dispatcher_connect(
|
||||
hass, mikrotik_controller.signal_update, update_controller
|
||||
)
|
||||
"""Set up entry for component"""
|
||||
dispatcher = {
|
||||
"MikrotikSwitch": MikrotikSwitch,
|
||||
"MikrotikPortSwitch": MikrotikPortSwitch,
|
||||
"MikrotikNATSwitch": MikrotikNATSwitch,
|
||||
"MikrotikMangleSwitch": MikrotikMangleSwitch,
|
||||
"MikrotikFilterSwitch": MikrotikFilterSwitch,
|
||||
"MikrotikQueueSwitch": MikrotikQueueSwitch,
|
||||
"MikrotikKidcontrolPauseSwitch": MikrotikKidcontrolPauseSwitch,
|
||||
}
|
||||
await model_async_setup_entry(
|
||||
hass,
|
||||
config_entry,
|
||||
async_add_entities,
|
||||
SENSOR_SERVICES,
|
||||
SENSOR_TYPES,
|
||||
dispatcher,
|
||||
)
|
||||
|
||||
update_controller()
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# update_items
|
||||
# MikrotikSwitch
|
||||
# ---------------------------
|
||||
@callback
|
||||
def update_items(inst, mikrotik_controller, async_add_entities, switches):
|
||||
"""Update device switch state from the controller."""
|
||||
new_switches = []
|
||||
|
||||
# Add switches
|
||||
for switch, sid_func in zip(
|
||||
# Switch type name
|
||||
[
|
||||
"interface",
|
||||
"nat",
|
||||
"mangle",
|
||||
"filter",
|
||||
"ppp_secret",
|
||||
"queue",
|
||||
"kidcontrol_enable",
|
||||
"kidcontrol_pause",
|
||||
],
|
||||
# Entity function
|
||||
[
|
||||
MikrotikControllerPortSwitch,
|
||||
MikrotikControllerNATSwitch,
|
||||
MikrotikControllerMangleSwitch,
|
||||
MikrotikControllerFilterSwitch,
|
||||
MikrotikControllerSwitch,
|
||||
MikrotikControllerQueueSwitch,
|
||||
MikrotikControllerSwitch,
|
||||
MikrotikControllerKidcontrolPauseSwitch,
|
||||
],
|
||||
):
|
||||
uid_switch = SWITCH_TYPES[switch]
|
||||
for uid in mikrotik_controller.data[SWITCH_TYPES[switch].data_path]:
|
||||
uid_data = mikrotik_controller.data[SWITCH_TYPES[switch].data_path]
|
||||
item_id = f"{inst}-{switch}-{uid_data[uid][uid_switch.data_reference]}"
|
||||
_LOGGER.debug("Updating sensor %s", item_id)
|
||||
if item_id in switches:
|
||||
if switches[item_id].enabled:
|
||||
switches[item_id].async_schedule_update_ha_state()
|
||||
continue
|
||||
|
||||
switches[item_id] = sid_func(
|
||||
inst=inst,
|
||||
uid=uid,
|
||||
mikrotik_controller=mikrotik_controller,
|
||||
entity_description=uid_switch,
|
||||
)
|
||||
new_switches.append(switches[item_id])
|
||||
|
||||
if new_switches:
|
||||
async_add_entities(new_switches)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerSwitch(SwitchEntity, RestoreEntity):
|
||||
class MikrotikSwitch(MikrotikEntity, SwitchEntity, RestoreEntity):
|
||||
"""Representation of a switch."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
inst,
|
||||
uid,
|
||||
mikrotik_controller,
|
||||
entity_description: MikrotikSwitchEntityDescription,
|
||||
):
|
||||
self.entity_description = entity_description
|
||||
self._inst = inst
|
||||
self._ctrl = mikrotik_controller
|
||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||
self._data = mikrotik_controller.data[self.entity_description.data_path][uid]
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if controller is available."""
|
||||
return self._ctrl.connected()
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name."""
|
||||
if self.entity_description.data_name_comment and self._data["comment"]:
|
||||
return (
|
||||
f"{self._inst} {self.entity_description.name} {self._data['comment']}"
|
||||
)
|
||||
|
||||
return f"{self._inst} {self.entity_description.name} {self._data[self.entity_description.data_name]}"
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id for this entity."""
|
||||
return f"{self._inst.lower()}-{self.entity_description.key}-{self._data[self.entity_description.data_reference].lower()}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if device is on."""
|
||||
return self._data[self.entity_description.data_is_on]
|
||||
return self._data[self.entity_description.data_attribute]
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon."""
|
||||
if self._data[self.entity_description.data_is_on]:
|
||||
if self._data[self.entity_description.data_attribute]:
|
||||
return self.entity_description.icon_enabled
|
||||
else:
|
||||
return self.entity_description.icon_disabled
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Mapping[str, Any]:
|
||||
"""Return the state attributes."""
|
||||
attributes = super().extra_state_attributes
|
||||
for variable in self.entity_description.data_attributes_list:
|
||||
if variable in self._data:
|
||||
attributes[format_attribute(variable)] = self._data[variable]
|
||||
|
||||
return attributes
|
||||
|
||||
def turn_on(self, **kwargs: Any) -> None:
|
||||
"""Required abstract method."""
|
||||
pass
|
||||
|
@ -187,49 +86,11 @@ class MikrotikControllerSwitch(SwitchEntity, RestoreEntity):
|
|||
self._ctrl.set_value(path, param, value, mod_param, True)
|
||||
await self._ctrl.async_update()
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return a description for device registry."""
|
||||
dev_connection = DOMAIN
|
||||
dev_connection_value = self.entity_description.data_reference
|
||||
dev_group = self.entity_description.ha_group
|
||||
if self.entity_description.ha_group.startswith("data__"):
|
||||
dev_group = self.entity_description.ha_group[6:]
|
||||
if dev_group in self._data:
|
||||
dev_group = self._data[dev_group]
|
||||
dev_connection_value = dev_group
|
||||
|
||||
if self.entity_description.ha_connection:
|
||||
dev_connection = self.entity_description.ha_connection
|
||||
|
||||
if self.entity_description.ha_connection_value:
|
||||
dev_connection_value = self.entity_description.ha_connection_value
|
||||
if dev_connection_value.startswith("data__"):
|
||||
dev_connection_value = dev_connection_value[6:]
|
||||
dev_connection_value = self._data[dev_connection_value]
|
||||
|
||||
info = DeviceInfo(
|
||||
connections={(dev_connection, f"{dev_connection_value}")},
|
||||
identifiers={(dev_connection, f"{dev_connection_value}")},
|
||||
default_name=f"{self._inst} {dev_group}",
|
||||
default_model=f"{self._ctrl.data['resource']['board-name']}",
|
||||
default_manufacturer=f"{self._ctrl.data['resource']['platform']}",
|
||||
sw_version=f"{self._ctrl.data['resource']['version']}",
|
||||
configuration_url=f"http://{self._ctrl.config_entry.data[CONF_HOST]}",
|
||||
via_device=(DOMAIN, f"{self._ctrl.data['routerboard']['serial-number']}"),
|
||||
)
|
||||
|
||||
return info
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added to hass."""
|
||||
_LOGGER.debug("New switch %s (%s)", self._inst, self.unique_id)
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerPortSwitch
|
||||
# MikrotikPortSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||
class MikrotikPortSwitch(MikrotikSwitch):
|
||||
"""Representation of a network port switch."""
|
||||
|
||||
@property
|
||||
|
@ -302,9 +163,9 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
|||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerNATSwitch
|
||||
# MikrotikNATSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||
class MikrotikNATSwitch(MikrotikSwitch):
|
||||
"""Representation of a NAT switch."""
|
||||
|
||||
@property
|
||||
|
@ -351,9 +212,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
|||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerMangleSwitch
|
||||
# MikrotikMangleSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerMangleSwitch(MikrotikControllerSwitch):
|
||||
class MikrotikMangleSwitch(MikrotikSwitch):
|
||||
"""Representation of a Mangle switch."""
|
||||
|
||||
async def async_turn_on(self) -> None:
|
||||
|
@ -394,9 +255,9 @@ class MikrotikControllerMangleSwitch(MikrotikControllerSwitch):
|
|||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerFilterSwitch
|
||||
# MikrotikFilterSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerFilterSwitch(MikrotikControllerSwitch):
|
||||
class MikrotikFilterSwitch(MikrotikSwitch):
|
||||
"""Representation of a Filter switch."""
|
||||
|
||||
async def async_turn_on(self) -> None:
|
||||
|
@ -435,9 +296,9 @@ class MikrotikControllerFilterSwitch(MikrotikControllerSwitch):
|
|||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerQueueSwitch
|
||||
# MikrotikQueueSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerQueueSwitch(MikrotikControllerSwitch):
|
||||
class MikrotikQueueSwitch(MikrotikSwitch):
|
||||
"""Representation of a queue switch."""
|
||||
|
||||
async def async_turn_on(self) -> None:
|
||||
|
@ -468,9 +329,9 @@ class MikrotikControllerQueueSwitch(MikrotikControllerSwitch):
|
|||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerKidcontrolPauseSwitch
|
||||
# MikrotikKidcontrolPauseSwitch
|
||||
# ---------------------------
|
||||
class MikrotikControllerKidcontrolPauseSwitch(MikrotikControllerSwitch):
|
||||
class MikrotikKidcontrolPauseSwitch(MikrotikSwitch):
|
||||
"""Representation of a queue switch."""
|
||||
|
||||
async def async_turn_on(self) -> None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue