mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-02 00:44:31 +02:00
entity refactoring
This commit is contained in:
parent
05d21d1f42
commit
d991d14a35
5 changed files with 145 additions and 153 deletions
|
@ -37,14 +37,14 @@ SENSOR_TYPES = {
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up device tracker for Mikrotik Router component."""
|
"""Set up device tracker for Mikrotik Router component."""
|
||||||
name = config_entry.data[CONF_NAME]
|
inst = config_entry.data[CONF_NAME]
|
||||||
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
sensors = {}
|
sensors = {}
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_controller():
|
def update_controller():
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
update_items(name, mikrotik_controller, async_add_entities, sensors)
|
update_items(inst, mikrotik_controller, async_add_entities, sensors)
|
||||||
|
|
||||||
mikrotik_controller.listeners.append(
|
mikrotik_controller.listeners.append(
|
||||||
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
||||||
|
@ -58,18 +58,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
# update_items
|
# update_items
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
@callback
|
@callback
|
||||||
def update_items(name, mikrotik_controller, async_add_entities, sensors):
|
def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
||||||
"""Update sensor state from the controller."""
|
"""Update sensor state from the controller."""
|
||||||
new_sensors = []
|
new_sensors = []
|
||||||
|
|
||||||
for sensor in SENSOR_TYPES:
|
for sensor in SENSOR_TYPES:
|
||||||
item_id = name + "-" + sensor
|
item_id = "{}-{}".format(inst, sensor)
|
||||||
if item_id in sensors:
|
if item_id in sensors:
|
||||||
if sensors[item_id].enabled:
|
if sensors[item_id].enabled:
|
||||||
sensors[item_id].async_schedule_update_ha_state()
|
sensors[item_id].async_schedule_update_ha_state()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sensors[item_id] = MikrotikControllerBinarySensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor)
|
sensors[item_id] = MikrotikControllerBinarySensor(mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor)
|
||||||
new_sensors.append(sensors[item_id])
|
new_sensors.append(sensors[item_id])
|
||||||
|
|
||||||
if new_sensors:
|
if new_sensors:
|
||||||
|
@ -81,12 +81,14 @@ def update_items(name, mikrotik_controller, async_add_entities, sensors):
|
||||||
class MikrotikControllerBinarySensor(BinarySensorDevice):
|
class MikrotikControllerBinarySensor(BinarySensorDevice):
|
||||||
"""Define an Mikrotik Controller Binary Sensor."""
|
"""Define an Mikrotik Controller Binary Sensor."""
|
||||||
|
|
||||||
def __init__(self, mikrotik_controller, name, kind, uid=''):
|
def __init__(self, mikrotik_controller, inst, sensor):
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self.mikrotik_controller = mikrotik_controller
|
self._inst = inst
|
||||||
self._name = name
|
self._sensor = sensor
|
||||||
self.kind = kind
|
self._ctrl = mikrotik_controller
|
||||||
self.uid = uid
|
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]]
|
||||||
|
self._type = SENSOR_TYPES[sensor]
|
||||||
|
self._attr = SENSOR_TYPES[sensor][ATTR_ATTR]
|
||||||
|
|
||||||
self._device_class = None
|
self._device_class = None
|
||||||
self._state = None
|
self._state = None
|
||||||
|
@ -97,9 +99,7 @@ class MikrotikControllerBinarySensor(BinarySensorDevice):
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name."""
|
"""Return the name."""
|
||||||
if self.uid:
|
return "{} {}".format(self._inst, self._type[ATTR_LABEL])
|
||||||
return f"{self._name} {self.uid} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
|
|
||||||
return f"{self._name} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
|
@ -109,41 +109,38 @@ class MikrotikControllerBinarySensor(BinarySensorDevice):
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
if self.uid:
|
return "{}-{}".format(self._inst.lower(), self._sensor.lower())
|
||||||
return f"{self._name.lower()}-{self.kind.lower()}-{self.uid.lower()}"
|
|
||||||
return f"{self._name.lower()}-{self.kind.lower()}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return bool(self.mikrotik_controller.data)
|
return bool(self._ctrl.data)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a port description for device registry."""
|
"""Return a port description for device registry."""
|
||||||
info = {
|
info = {
|
||||||
"identifiers": {(DOMAIN, "serial-number", self.mikrotik_controller.data['routerboard']['serial-number'], "switch", "PORT")},
|
"identifiers": {(DOMAIN, "serial-number", self._ctrl.data['routerboard']['serial-number'], "switch", "PORT")},
|
||||||
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
||||||
"model": self.mikrotik_controller.data['resource']['board-name'],
|
"model": self._ctrl.data['resource']['board-name'],
|
||||||
"name": SENSOR_TYPES[self.kind][ATTR_GROUP],
|
"name": self._type[ATTR_GROUP],
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Synchronize state with controller."""
|
"""Synchronize state with controller."""
|
||||||
# await self.mikrotik_controller.async_update()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Port entity created."""
|
"""Port entity created."""
|
||||||
_LOGGER.debug("New sensor %s (%s)", self._name, self.kind)
|
_LOGGER.debug("New sensor %s (%s)", self._inst, self._sensor)
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if sensor is on."""
|
"""Return true if sensor is on."""
|
||||||
val = False
|
val = False
|
||||||
if SENSOR_TYPES[self.kind][ATTR_PATH] in self.mikrotik_controller.data and SENSOR_TYPES[self.kind][ATTR_ATTR] in self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]]:
|
if self._attr in self._data:
|
||||||
val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]]
|
val = self._data[self._attr]
|
||||||
|
|
||||||
return val
|
return val
|
||||||
|
|
|
@ -40,14 +40,14 @@ DEVICE_ATTRIBUTES = [
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up device tracker for Mikrotik Router component."""
|
"""Set up device tracker for Mikrotik Router component."""
|
||||||
name = config_entry.data[CONF_NAME]
|
inst = config_entry.data[CONF_NAME]
|
||||||
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
tracked = {}
|
tracked = {}
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_controller():
|
def update_controller():
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
update_items(name, mikrotik_controller, async_add_entities, tracked)
|
update_items(inst, mikrotik_controller, async_add_entities, tracked)
|
||||||
|
|
||||||
mikrotik_controller.listeners.append(
|
mikrotik_controller.listeners.append(
|
||||||
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
||||||
|
@ -61,19 +61,19 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
# update_items
|
# update_items
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
@callback
|
@callback
|
||||||
def update_items(name, mikrotik_controller, async_add_entities, tracked):
|
def update_items(inst, mikrotik_controller, async_add_entities, tracked):
|
||||||
"""Update tracked device state from the controller."""
|
"""Update tracked device state from the controller."""
|
||||||
new_tracked = []
|
new_tracked = []
|
||||||
|
|
||||||
for uid in mikrotik_controller.data['interface']:
|
for uid in mikrotik_controller.data['interface']:
|
||||||
if mikrotik_controller.data['interface'][uid]['type'] == "ether":
|
if mikrotik_controller.data['interface'][uid]['type'] == "ether":
|
||||||
item_id = name + "-" + mikrotik_controller.data['interface'][uid]['default-name']
|
item_id = "{}-{}".format(inst, mikrotik_controller.data['interface'][uid]['default-name'])
|
||||||
if item_id in tracked:
|
if item_id in tracked:
|
||||||
if tracked[item_id].enabled:
|
if tracked[item_id].enabled:
|
||||||
tracked[item_id].async_schedule_update_ha_state()
|
tracked[item_id].async_schedule_update_ha_state()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
tracked[item_id] = MikrotikControllerPortDeviceTracker(name, uid, mikrotik_controller)
|
tracked[item_id] = MikrotikControllerPortDeviceTracker(inst, uid, mikrotik_controller)
|
||||||
new_tracked.append(tracked[item_id])
|
new_tracked.append(tracked[item_id])
|
||||||
|
|
||||||
if new_tracked:
|
if new_tracked:
|
||||||
|
@ -88,11 +88,11 @@ def update_items(name, mikrotik_controller, async_add_entities, tracked):
|
||||||
class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
||||||
"""Representation of a network port."""
|
"""Representation of a network port."""
|
||||||
|
|
||||||
def __init__(self, name, uid, mikrotik_controller):
|
def __init__(self, inst, uid, mikrotik_controller):
|
||||||
"""Set up tracked port."""
|
"""Set up tracked port."""
|
||||||
self._name = name
|
self._inst = inst
|
||||||
self._uid = uid
|
self._ctrl = mikrotik_controller
|
||||||
self.mikrotik_controller = mikrotik_controller
|
self._data = mikrotik_controller.data['interface'][uid]
|
||||||
|
|
||||||
self._attrs = {
|
self._attrs = {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
|
@ -105,18 +105,17 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Port entity created."""
|
"""Port entity created."""
|
||||||
_LOGGER.debug("New port tracker %s (%s)", self._name, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address'])
|
_LOGGER.debug("New port tracker %s (%s)", self._inst, self._data['port-mac-address'])
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Synchronize state with controller."""
|
"""Synchronize state with controller."""
|
||||||
# await self.mikrotik_controller.async_update()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_connected(self):
|
def is_connected(self):
|
||||||
"""Return true if the port is connected to the network."""
|
"""Return true if the port is connected to the network."""
|
||||||
return self.mikrotik_controller.data['interface'][self._uid]['running']
|
return self._data['running']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def source_type(self):
|
def source_type(self):
|
||||||
|
@ -124,29 +123,29 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
||||||
return SOURCE_TYPE_ROUTER
|
return SOURCE_TYPE_ROUTER
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self):
|
||||||
"""Return the name of the port."""
|
"""Return the name of the port."""
|
||||||
return f"{self._name} {self.mikrotik_controller.data['interface'][self._uid]['default-name']}"
|
return "{} {}".format(self._inst, self._data['default-name'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self):
|
||||||
"""Return a unique identifier for this port."""
|
"""Return a unique identifier for this port."""
|
||||||
return f"{self._name.lower()}-{self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']}"
|
return "{}-{}".format(self._inst.lower(), self._data['port-mac-address'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self):
|
||||||
"""Return if controller is available."""
|
"""Return if controller is available."""
|
||||||
return self.mikrotik_controller.connected()
|
return self._ctrl.connected()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon."""
|
"""Return the icon."""
|
||||||
if self.mikrotik_controller.data['interface'][self._uid]['running']:
|
if self._data['running']:
|
||||||
icon = 'mdi:lan-connect'
|
icon = 'mdi:lan-connect'
|
||||||
else:
|
else:
|
||||||
icon = 'mdi:lan-pending'
|
icon = 'mdi:lan-pending'
|
||||||
|
|
||||||
if not self.mikrotik_controller.data['interface'][self._uid]['enabled']:
|
if not self._data['enabled']:
|
||||||
icon = 'mdi:lan-disconnect'
|
icon = 'mdi:lan-disconnect'
|
||||||
|
|
||||||
return icon
|
return icon
|
||||||
|
@ -155,10 +154,10 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a port description for device registry."""
|
"""Return a port description for device registry."""
|
||||||
info = {
|
info = {
|
||||||
"connections": {(CONNECTION_NETWORK_MAC, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address'])},
|
"connections": {(CONNECTION_NETWORK_MAC, self._data['port-mac-address'])},
|
||||||
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
||||||
"model": self.mikrotik_controller.data['resource']['board-name'],
|
"model": self._ctrl.data['resource']['board-name'],
|
||||||
"name": self.mikrotik_controller.data['interface'][self._uid]['default-name'],
|
"name": self._data['default-name'],
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
@ -168,7 +167,7 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
||||||
attributes = self._attrs
|
attributes = self._attrs
|
||||||
|
|
||||||
for variable in DEVICE_ATTRIBUTES:
|
for variable in DEVICE_ATTRIBUTES:
|
||||||
if variable in self.mikrotik_controller.data['interface'][self._uid]:
|
if variable in self._data:
|
||||||
attributes[variable] = self.mikrotik_controller.data['interface'][self._uid][variable]
|
attributes[variable] = self._data[variable]
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
|
@ -4,7 +4,6 @@ from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
# from homeassistant.util import Throttle
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -17,7 +16,6 @@ from .const import (
|
||||||
from .mikrotikapi import MikrotikAPI
|
from .mikrotikapi import MikrotikAPI
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
# DEFAULT_SCAN_INTERVAL = timedelta(seconds=DEFAULT_SCAN_INTERVAL)
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -61,14 +59,14 @@ class MikrotikControllerData():
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.config_entry = config_entry
|
self.config_entry = config_entry
|
||||||
|
|
||||||
self.data = {}
|
self.data = {'routerboard': {},
|
||||||
self.data['routerboard'] = {}
|
'resource': {},
|
||||||
self.data['resource'] = {}
|
'interface': {},
|
||||||
self.data['interface'] = {}
|
'arp': {},
|
||||||
self.data['arp'] = {}
|
'nat': {},
|
||||||
self.data['nat'] = {}
|
'fw-update': {},
|
||||||
self.data['fw-update'] = {}
|
'script': {}
|
||||||
self.data['script'] = {}
|
}
|
||||||
|
|
||||||
self.listeners = []
|
self.listeners = []
|
||||||
|
|
||||||
|
@ -118,7 +116,7 @@ class MikrotikControllerData():
|
||||||
@property
|
@property
|
||||||
def signal_update(self):
|
def signal_update(self):
|
||||||
"""Event to signal new data."""
|
"""Event to signal new data."""
|
||||||
return f"{DOMAIN}-update-{self.name}"
|
return "{}-update-{}".format(DOMAIN, self.name)
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# connected
|
# connected
|
||||||
|
@ -150,7 +148,6 @@ class MikrotikControllerData():
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# async_update
|
# async_update
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# @Throttle(DEFAULT_SCAN_INTERVAL)
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update Mikrotik data"""
|
"""Update Mikrotik data"""
|
||||||
|
|
||||||
|
|
|
@ -61,14 +61,14 @@ SENSOR_TYPES = {
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up device tracker for Mikrotik Router component."""
|
"""Set up device tracker for Mikrotik Router component."""
|
||||||
name = config_entry.data[CONF_NAME]
|
inst = config_entry.data[CONF_NAME]
|
||||||
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
sensors = {}
|
sensors = {}
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_controller():
|
def update_controller():
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
update_items(name, mikrotik_controller, async_add_entities, sensors)
|
update_items(inst, mikrotik_controller, async_add_entities, sensors)
|
||||||
|
|
||||||
mikrotik_controller.listeners.append(
|
mikrotik_controller.listeners.append(
|
||||||
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
||||||
|
@ -82,18 +82,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
# update_items
|
# update_items
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
@callback
|
@callback
|
||||||
def update_items(name, mikrotik_controller, async_add_entities, sensors):
|
def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
||||||
"""Update sensor state from the controller."""
|
"""Update sensor state from the controller."""
|
||||||
new_sensors = []
|
new_sensors = []
|
||||||
|
|
||||||
for sensor in SENSOR_TYPES:
|
for sensor in SENSOR_TYPES:
|
||||||
item_id = name + "-" + sensor
|
item_id = "{}-{}".format(inst, sensor)
|
||||||
if item_id in sensors:
|
if item_id in sensors:
|
||||||
if sensors[item_id].enabled:
|
if sensors[item_id].enabled:
|
||||||
sensors[item_id].async_schedule_update_ha_state()
|
sensors[item_id].async_schedule_update_ha_state()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sensors[item_id] = MikrotikControllerSensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor)
|
sensors[item_id] = MikrotikControllerSensor(mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor)
|
||||||
new_sensors.append(sensors[item_id])
|
new_sensors.append(sensors[item_id])
|
||||||
|
|
||||||
if new_sensors:
|
if new_sensors:
|
||||||
|
@ -108,12 +108,14 @@ def update_items(name, mikrotik_controller, async_add_entities, sensors):
|
||||||
class MikrotikControllerSensor(Entity):
|
class MikrotikControllerSensor(Entity):
|
||||||
"""Define an Mikrotik Controller sensor."""
|
"""Define an Mikrotik Controller sensor."""
|
||||||
|
|
||||||
def __init__(self, mikrotik_controller, name, kind, uid=''):
|
def __init__(self, mikrotik_controller, inst, sensor):
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self.mikrotik_controller = mikrotik_controller
|
self._inst = inst
|
||||||
self._name = name
|
self._sensor = sensor
|
||||||
self.kind = kind
|
self._ctrl = mikrotik_controller
|
||||||
self.uid = uid
|
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]]
|
||||||
|
self._type = SENSOR_TYPES[sensor]
|
||||||
|
self._attr = SENSOR_TYPES[sensor][ATTR_ATTR]
|
||||||
|
|
||||||
self._device_class = None
|
self._device_class = None
|
||||||
self._state = None
|
self._state = None
|
||||||
|
@ -124,16 +126,14 @@ class MikrotikControllerSensor(Entity):
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name."""
|
"""Return the name."""
|
||||||
if self.uid:
|
return "{} {}".format(self._inst, self._type[ATTR_LABEL])
|
||||||
return f"{self._name} {self.uid} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
|
|
||||||
return f"{self._name} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state."""
|
"""Return the state."""
|
||||||
val = "unknown"
|
val = "unknown"
|
||||||
if SENSOR_TYPES[self.kind][ATTR_PATH] in self.mikrotik_controller.data and SENSOR_TYPES[self.kind][ATTR_ATTR] in self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]]:
|
if self._attr in self._data:
|
||||||
val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]]
|
val = self._data[self._attr]
|
||||||
|
|
||||||
return val
|
return val
|
||||||
|
|
||||||
|
@ -145,48 +145,45 @@ class MikrotikControllerSensor(Entity):
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon."""
|
"""Return the icon."""
|
||||||
self._icon = SENSOR_TYPES[self.kind][ATTR_ICON]
|
self._icon = self._type[ATTR_ICON]
|
||||||
return self._icon
|
return self._icon
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
"""Return the device_class."""
|
"""Return the device_class."""
|
||||||
return SENSOR_TYPES[self.kind][ATTR_DEVICE_CLASS]
|
return self._type[ATTR_DEVICE_CLASS]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
if self.uid:
|
return "{}-{}".format(self._inst.lower(), self._sensor.lower())
|
||||||
return f"{self._name.lower()}-{self.kind.lower()}-{self.uid.lower()}"
|
|
||||||
return f"{self._name.lower()}-{self.kind.lower()}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit the value is expressed in."""
|
"""Return the unit the value is expressed in."""
|
||||||
return SENSOR_TYPES[self.kind][ATTR_UNIT]
|
return self._type[ATTR_UNIT]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return bool(self.mikrotik_controller.data)
|
return bool(self._ctrl.data)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a port description for device registry."""
|
"""Return a port description for device registry."""
|
||||||
info = {
|
info = {
|
||||||
"identifiers": {(DOMAIN, "serial-number", self.mikrotik_controller.data['routerboard']['serial-number'], "switch", "PORT")},
|
"identifiers": {(DOMAIN, "serial-number", self._ctrl.data['routerboard']['serial-number'], "switch", "PORT")},
|
||||||
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
||||||
"model": self.mikrotik_controller.data['resource']['board-name'],
|
"model": self._ctrl.data['resource']['board-name'],
|
||||||
"name": SENSOR_TYPES[self.kind][ATTR_GROUP],
|
"name": self._type[ATTR_GROUP],
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Synchronize state with controller."""
|
"""Synchronize state with controller."""
|
||||||
# await self.mikrotik_controller.async_update()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Port entity created."""
|
"""Port entity created."""
|
||||||
_LOGGER.debug("New sensor %s (%s)", self._name, self.kind)
|
_LOGGER.debug("New sensor %s (%s)", self._inst, self._sensor)
|
||||||
return
|
return
|
||||||
|
|
|
@ -61,14 +61,14 @@ DEVICE_ATTRIBUTES_SCRIPT = [
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up switches for Mikrotik Router component."""
|
"""Set up switches for Mikrotik Router component."""
|
||||||
name = config_entry.data[CONF_NAME]
|
inst = config_entry.data[CONF_NAME]
|
||||||
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
switches = {}
|
switches = {}
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_controller():
|
def update_controller():
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
update_items(name, mikrotik_controller, async_add_entities, switches)
|
update_items(inst, mikrotik_controller, async_add_entities, switches)
|
||||||
|
|
||||||
mikrotik_controller.listeners.append(
|
mikrotik_controller.listeners.append(
|
||||||
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
||||||
|
@ -82,27 +82,27 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
# update_items
|
# update_items
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
@callback
|
@callback
|
||||||
def update_items(name, mikrotik_controller, async_add_entities, switches):
|
def update_items(inst, mikrotik_controller, async_add_entities, switches):
|
||||||
"""Update device switch state from the controller."""
|
"""Update device switch state from the controller."""
|
||||||
new_switches = []
|
new_switches = []
|
||||||
|
|
||||||
# Add switches
|
# Add switches
|
||||||
for sid in SWITCH_LIST:
|
for sid in SWITCH_LIST:
|
||||||
for uid in mikrotik_controller.data[sid]:
|
for uid in mikrotik_controller.data[sid]:
|
||||||
item_id = name + "-" + sid + "-" + mikrotik_controller.data[sid][uid]['name']
|
item_id = "{}-{}-{}".format(inst, sid, mikrotik_controller.data[sid][uid]['name'])
|
||||||
if item_id in switches:
|
if item_id in switches:
|
||||||
if switches[item_id].enabled:
|
if switches[item_id].enabled:
|
||||||
switches[item_id].async_schedule_update_ha_state()
|
switches[item_id].async_schedule_update_ha_state()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if sid == 'interface':
|
if sid == 'interface':
|
||||||
switches[item_id] = MikrotikControllerPortSwitch(name, uid, mikrotik_controller)
|
switches[item_id] = MikrotikControllerPortSwitch(inst, uid, mikrotik_controller)
|
||||||
|
|
||||||
if sid == 'nat':
|
if sid == 'nat':
|
||||||
switches[item_id] = MikrotikControllerNATSwitch(name, uid, mikrotik_controller)
|
switches[item_id] = MikrotikControllerNATSwitch(inst, uid, mikrotik_controller)
|
||||||
|
|
||||||
if sid == 'script':
|
if sid == 'script':
|
||||||
switches[item_id] = MikrotikControllerScriptSwitch(name, uid, mikrotik_controller)
|
switches[item_id] = MikrotikControllerScriptSwitch(inst, uid, mikrotik_controller)
|
||||||
|
|
||||||
new_switches.append(switches[item_id])
|
new_switches.append(switches[item_id])
|
||||||
|
|
||||||
|
@ -116,28 +116,27 @@ def update_items(name, mikrotik_controller, async_add_entities, switches):
|
||||||
# MikrotikControllerSwitch
|
# MikrotikControllerSwitch
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
class MikrotikControllerSwitch(SwitchDevice, RestoreEntity):
|
class MikrotikControllerSwitch(SwitchDevice, RestoreEntity):
|
||||||
"""Representation of a network port switch."""
|
"""Representation of a switch."""
|
||||||
|
|
||||||
def __init__(self, name, uid, mikrotik_controller):
|
def __init__(self, inst, uid, mikrotik_controller):
|
||||||
"""Set up switch."""
|
"""Set up switch."""
|
||||||
self._name = name
|
self._inst = inst
|
||||||
self._uid = uid
|
self._uid = uid
|
||||||
self.mikrotik_controller = mikrotik_controller
|
self._ctrl = mikrotik_controller
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Switch entity created."""
|
"""Switch entity created."""
|
||||||
_LOGGER.debug("New switch %s (%s)", self._name, self._uid)
|
_LOGGER.debug("New switch %s (%s)", self._inst, self._uid)
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Synchronize state with controller."""
|
"""Synchronize state with controller."""
|
||||||
# await self.mikrotik_controller.async_update()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return if controller is available."""
|
"""Return if controller is available."""
|
||||||
return self.mikrotik_controller.connected()
|
return self._ctrl.connected()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -146,38 +145,39 @@ class MikrotikControllerSwitch(SwitchDevice, RestoreEntity):
|
||||||
class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||||
"""Representation of a network port switch."""
|
"""Representation of a network port switch."""
|
||||||
|
|
||||||
def __init__(self, name, uid, mikrotik_controller):
|
def __init__(self, inst, uid, mikrotik_controller):
|
||||||
"""Set up tracked port."""
|
"""Set up tracked port."""
|
||||||
super().__init__(name, uid, mikrotik_controller)
|
super().__init__(inst, uid, mikrotik_controller)
|
||||||
|
|
||||||
|
self._data = mikrotik_controller.data['interface'][self._uid]
|
||||||
self._attrs = {
|
self._attrs = {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Port entity created."""
|
"""Port entity created."""
|
||||||
_LOGGER.debug("New port switch %s (%s)", self._name, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address'])
|
_LOGGER.debug("New port switch %s (%s)", self._inst, self._data['port-mac-address'])
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the port."""
|
"""Return the name of the port."""
|
||||||
return f"{self._name} port {self.mikrotik_controller.data['interface'][self._uid]['default-name']}"
|
return "{} port {}".format(self._inst, self._data['default-name'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return a unique identifier for this port."""
|
"""Return a unique identifier for this port."""
|
||||||
return f"{self._name.lower()}-enable_switch-{self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']}"
|
return "{}-enable_switch-{}".format(self._inst.lower(), self._data['port-mac-address'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon."""
|
"""Return the icon."""
|
||||||
if self.mikrotik_controller.data['interface'][self._uid]['running']:
|
if self._data['running']:
|
||||||
icon = 'mdi:lan-connect'
|
icon = 'mdi:lan-connect'
|
||||||
else:
|
else:
|
||||||
icon = 'mdi:lan-pending'
|
icon = 'mdi:lan-pending'
|
||||||
|
|
||||||
if not self.mikrotik_controller.data['interface'][self._uid]['enabled']:
|
if not self._data['enabled']:
|
||||||
icon = 'mdi:lan-disconnect'
|
icon = 'mdi:lan-disconnect'
|
||||||
|
|
||||||
return icon
|
return icon
|
||||||
|
@ -186,10 +186,10 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a port description for device registry."""
|
"""Return a port description for device registry."""
|
||||||
info = {
|
info = {
|
||||||
"connections": {(CONNECTION_NETWORK_MAC, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address'])},
|
"connections": {(CONNECTION_NETWORK_MAC, self._data['port-mac-address'])},
|
||||||
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
||||||
"model": self.mikrotik_controller.data['resource']['board-name'],
|
"model": self._ctrl.data['resource']['board-name'],
|
||||||
"name": self.mikrotik_controller.data['interface'][self._uid]['default-name'],
|
"name": self._data['default-name'],
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
@ -199,8 +199,8 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||||
attributes = self._attrs
|
attributes = self._attrs
|
||||||
|
|
||||||
for variable in DEVICE_ATTRIBUTES_IFACE:
|
for variable in DEVICE_ATTRIBUTES_IFACE:
|
||||||
if variable in self.mikrotik_controller.data['interface'][self._uid]:
|
if variable in self._data:
|
||||||
attributes[variable] = self.mikrotik_controller.data['interface'][self._uid][variable]
|
attributes[variable] = self._data[variable]
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
|
@ -208,28 +208,28 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
path = '/interface'
|
path = '/interface'
|
||||||
param = 'default-name'
|
param = 'default-name'
|
||||||
value = self.mikrotik_controller.data['interface'][self._uid][param]
|
value = self._data[param]
|
||||||
mod_param = 'disabled'
|
mod_param = 'disabled'
|
||||||
mod_value = False
|
mod_value = False
|
||||||
self.mikrotik_controller.set_value(path, param, value, mod_param, mod_value)
|
self._ctrl.set_value(path, param, value, mod_param, mod_value)
|
||||||
await self.mikrotik_controller.force_update()
|
await self._ctrl.force_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_turn_off(self):
|
async def async_turn_off(self):
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
path = '/interface'
|
path = '/interface'
|
||||||
param = 'default-name'
|
param = 'default-name'
|
||||||
value = self.mikrotik_controller.data['interface'][self._uid][param]
|
value = self._data[param]
|
||||||
mod_param = 'disabled'
|
mod_param = 'disabled'
|
||||||
mod_value = True
|
mod_value = True
|
||||||
self.mikrotik_controller.set_value(path, param, value, mod_param, mod_value)
|
self._ctrl.set_value(path, param, value, mod_param, mod_value)
|
||||||
await self.mikrotik_controller.async_update()
|
await self._ctrl.async_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return self.mikrotik_controller.data['interface'][self._uid]['enabled']
|
return self._data['enabled']
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -238,33 +238,34 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||||
class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
"""Representation of a NAT switch."""
|
"""Representation of a NAT switch."""
|
||||||
|
|
||||||
def __init__(self, name, uid, mikrotik_controller):
|
def __init__(self, inst, uid, mikrotik_controller):
|
||||||
"""Set up NAT switch."""
|
"""Set up NAT switch."""
|
||||||
super().__init__(name, uid, mikrotik_controller)
|
super().__init__(inst, uid, mikrotik_controller)
|
||||||
|
|
||||||
|
self._data = mikrotik_controller.data['nat'][self._uid]
|
||||||
self._attrs = {
|
self._attrs = {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""NAT switch entity created."""
|
"""NAT switch entity created."""
|
||||||
_LOGGER.debug("New port switch %s (%s)", self._name, self.mikrotik_controller.data['nat'][self._uid]['name'])
|
_LOGGER.debug("New port switch %s (%s)", self._inst, self._data['name'])
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the NAT switch."""
|
"""Return the name of the NAT switch."""
|
||||||
return f"{self._name} NAT {self.mikrotik_controller.data['nat'][self._uid]['name']}"
|
return "{} NAT {}".format(self._inst, self._data['name'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return a unique identifier for this NAT switch."""
|
"""Return a unique identifier for this NAT switch."""
|
||||||
return f"{self._name.lower()}-nat_switch-{self.mikrotik_controller.data['nat'][self._uid]['name']}"
|
return "{}-nat_switch-{}".format(self._inst.lower(), self._data['name'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon."""
|
"""Return the icon."""
|
||||||
if not self.mikrotik_controller.data['nat'][self._uid]['enabled']:
|
if not self._data['enabled']:
|
||||||
icon = 'mdi:network-off-outline'
|
icon = 'mdi:network-off-outline'
|
||||||
else:
|
else:
|
||||||
icon = 'mdi:network-outline'
|
icon = 'mdi:network-outline'
|
||||||
|
@ -275,9 +276,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a NAT switch description for device registry."""
|
"""Return a NAT switch description for device registry."""
|
||||||
info = {
|
info = {
|
||||||
"identifiers": {(DOMAIN, "serial-number", self.mikrotik_controller.data['routerboard']['serial-number'], "switch", "NAT")},
|
"identifiers": {(DOMAIN, "serial-number", self._ctrl.data['routerboard']['serial-number'], "switch", "NAT")},
|
||||||
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
||||||
"model": self.mikrotik_controller.data['resource']['board-name'],
|
"model": self._ctrl.data['resource']['board-name'],
|
||||||
"name": "NAT",
|
"name": "NAT",
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
@ -288,8 +289,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
attributes = self._attrs
|
attributes = self._attrs
|
||||||
|
|
||||||
for variable in DEVICE_ATTRIBUTES_NAT:
|
for variable in DEVICE_ATTRIBUTES_NAT:
|
||||||
if variable in self.mikrotik_controller.data['nat'][self._uid]:
|
if variable in self._data:
|
||||||
attributes[variable] = self.mikrotik_controller.data['nat'][self._uid][variable]
|
attributes[variable] = self._data[variable]
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
|
@ -300,8 +301,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
value = self._uid
|
value = self._uid
|
||||||
mod_param = 'disabled'
|
mod_param = 'disabled'
|
||||||
mod_value = False
|
mod_value = False
|
||||||
self.mikrotik_controller.set_value(path, param, value, mod_param, mod_value)
|
self._ctrl.set_value(path, param, value, mod_param, mod_value)
|
||||||
await self.mikrotik_controller.force_update()
|
await self._ctrl.force_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_turn_off(self):
|
async def async_turn_off(self):
|
||||||
|
@ -311,14 +312,14 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
value = self._uid
|
value = self._uid
|
||||||
mod_param = 'disabled'
|
mod_param = 'disabled'
|
||||||
mod_value = True
|
mod_value = True
|
||||||
self.mikrotik_controller.set_value(path, param, value, mod_param, mod_value)
|
self._ctrl.set_value(path, param, value, mod_param, mod_value)
|
||||||
await self.mikrotik_controller.async_update()
|
await self._ctrl.async_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return self.mikrotik_controller.data['nat'][self._uid]['enabled']
|
return self._data['enabled']
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -327,28 +328,29 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
|
class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
|
||||||
"""Representation of a script switch."""
|
"""Representation of a script switch."""
|
||||||
|
|
||||||
def __init__(self, name, uid, mikrotik_controller):
|
def __init__(self, inst, uid, mikrotik_controller):
|
||||||
"""Set up script switch."""
|
"""Set up script switch."""
|
||||||
super().__init__(name, uid, mikrotik_controller)
|
super().__init__(inst, uid, mikrotik_controller)
|
||||||
|
|
||||||
|
self._data = mikrotik_controller.data['script'][self._uid]
|
||||||
self._attrs = {
|
self._attrs = {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Script switch entity created."""
|
"""Script switch entity created."""
|
||||||
_LOGGER.debug("New script switch %s (%s)", self._name, self.mikrotik_controller.data['script'][self._uid]['name'])
|
_LOGGER.debug("New script switch %s (%s)", self._inst, self._data['name'])
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the script switch."""
|
"""Return the name of the script switch."""
|
||||||
return f"{self._name} script {self.mikrotik_controller.data['script'][self._uid]['name']}"
|
return "{} script {}".format(self._inst, self._data['name'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return a unique identifier for this script switch."""
|
"""Return a unique identifier for this script switch."""
|
||||||
return f"{self._name.lower()}-script_switch-{self.mikrotik_controller.data['script'][self._uid]['name']}"
|
return "{}-script_switch-{}".format(self._inst.lower(), self._data['name'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
|
@ -359,9 +361,9 @@ class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a script switch description for device registry."""
|
"""Return a script switch description for device registry."""
|
||||||
info = {
|
info = {
|
||||||
"identifiers": {(DOMAIN, "serial-number", self.mikrotik_controller.data['routerboard']['serial-number'], "switch", "Scripts")},
|
"identifiers": {(DOMAIN, "serial-number", self._ctrl.data['routerboard']['serial-number'], "switch", "Scripts")},
|
||||||
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
||||||
"model": self.mikrotik_controller.data['resource']['board-name'],
|
"model": self._ctrl.data['resource']['board-name'],
|
||||||
"name": "Scripts",
|
"name": "Scripts",
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
@ -372,15 +374,15 @@ class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
|
||||||
attributes = self._attrs
|
attributes = self._attrs
|
||||||
|
|
||||||
for variable in DEVICE_ATTRIBUTES_SCRIPT:
|
for variable in DEVICE_ATTRIBUTES_SCRIPT:
|
||||||
if variable in self.mikrotik_controller.data['script'][self._uid]:
|
if variable in self._data:
|
||||||
attributes[variable] = self.mikrotik_controller.data['script'][self._uid][variable]
|
attributes[variable] = self._data[variable]
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
async def async_turn_on(self):
|
async def async_turn_on(self):
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
self.mikrotik_controller.run_script(self.mikrotik_controller.data['script'][self._uid]['name'])
|
self._ctrl.run_script(self._data['name'])
|
||||||
await self.mikrotik_controller.force_update()
|
await self._ctrl.force_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
async def async_turn_off(self):
|
async def async_turn_off(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue