mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-30 22:49:30 +02:00
added firmware update binary sensor
This commit is contained in:
parent
ecee4043e3
commit
fe74959b7e
3 changed files with 183 additions and 0 deletions
|
@ -56,6 +56,10 @@ async def async_setup_entry(hass, config_entry):
|
||||||
hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
|
hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
hass.async_create_task(
|
||||||
|
hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor")
|
||||||
|
)
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.async_forward_entry_setup(config_entry, "device_tracker")
|
hass.config_entries.async_forward_entry_setup(config_entry, "device_tracker")
|
||||||
)
|
)
|
||||||
|
@ -83,6 +87,7 @@ async def async_unload_entry(hass, config_entry):
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
||||||
|
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
||||||
await mikrotik_controller.async_reset()
|
await mikrotik_controller.async_reset()
|
||||||
|
|
149
custom_components/mikrotik_router/binary_sensor.py
Normal file
149
custom_components/mikrotik_router/binary_sensor.py
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
"""Support for the Mikrotik Router binary sensor service."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_NAME,
|
||||||
|
ATTR_ATTRIBUTION,
|
||||||
|
)
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
DOMAIN,
|
||||||
|
DATA_CLIENT,
|
||||||
|
ATTRIBUTION,
|
||||||
|
)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ATTR_LABEL = "label"
|
||||||
|
ATTR_GROUP = "group"
|
||||||
|
ATTR_PATH = "data_path"
|
||||||
|
ATTR_ATTR = "data_attr"
|
||||||
|
|
||||||
|
SENSOR_TYPES = {
|
||||||
|
'system_fwupdate': {
|
||||||
|
ATTR_LABEL: 'Firmware update',
|
||||||
|
ATTR_GROUP: "System",
|
||||||
|
ATTR_PATH: "fw-update",
|
||||||
|
ATTR_ATTR: "available",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# async_setup_entry
|
||||||
|
# ---------------------------
|
||||||
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
|
"""Set up device tracker for Mikrotik Router component."""
|
||||||
|
name = config_entry.data[CONF_NAME]
|
||||||
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
|
sensors = {}
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def update_controller():
|
||||||
|
"""Update the values of the controller."""
|
||||||
|
update_items(name, mikrotik_controller, async_add_entities, sensors)
|
||||||
|
|
||||||
|
mikrotik_controller.listeners.append(
|
||||||
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
||||||
|
)
|
||||||
|
|
||||||
|
update_controller()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# update_items
|
||||||
|
# ---------------------------
|
||||||
|
@callback
|
||||||
|
def update_items(name, mikrotik_controller, async_add_entities, sensors):
|
||||||
|
"""Update sensor state from the controller."""
|
||||||
|
new_sensors = []
|
||||||
|
|
||||||
|
for sensor in SENSOR_TYPES:
|
||||||
|
item_id = name + "-" + sensor
|
||||||
|
if item_id in sensors:
|
||||||
|
if sensors[item_id].enabled:
|
||||||
|
sensors[item_id].async_schedule_update_ha_state()
|
||||||
|
continue
|
||||||
|
|
||||||
|
sensors[item_id] = MikrotikControllerBinarySensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor)
|
||||||
|
new_sensors.append(sensors[item_id])
|
||||||
|
|
||||||
|
if new_sensors:
|
||||||
|
async_add_entities(new_sensors, True)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class MikrotikControllerBinarySensor(BinarySensorDevice):
|
||||||
|
"""Define an Mikrotik Controller Binary Sensor."""
|
||||||
|
|
||||||
|
def __init__(self, mikrotik_controller, name, kind, uid=''):
|
||||||
|
"""Initialize."""
|
||||||
|
self.mikrotik_controller = mikrotik_controller
|
||||||
|
self._name = name
|
||||||
|
self.kind = kind
|
||||||
|
self.uid = uid
|
||||||
|
|
||||||
|
self._device_class = None
|
||||||
|
self._state = None
|
||||||
|
self._icon = None
|
||||||
|
self._unit_of_measurement = None
|
||||||
|
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return the name."""
|
||||||
|
if self.uid:
|
||||||
|
return f"{self._name} {self.uid} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
|
||||||
|
return f"{self._name} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
"""Return the state attributes."""
|
||||||
|
return self._attrs
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return a unique_id for this entity."""
|
||||||
|
if self.uid:
|
||||||
|
return f"{self._name.lower()}-{self.kind.lower()}-{self.uid.lower()}"
|
||||||
|
return f"{self._name.lower()}-{self.kind.lower()}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return True if entity is available."""
|
||||||
|
return bool(self.mikrotik_controller.data)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self):
|
||||||
|
"""Return a port description for device registry."""
|
||||||
|
info = {
|
||||||
|
"identifiers": {(DOMAIN, "serial-number", self.mikrotik_controller.data['routerboard']['serial-number'], "switch", "PORT")},
|
||||||
|
"manufacturer": self.mikrotik_controller.data['resource']['platform'],
|
||||||
|
"model": self.mikrotik_controller.data['resource']['board-name'],
|
||||||
|
"name": SENSOR_TYPES[self.kind][ATTR_GROUP],
|
||||||
|
}
|
||||||
|
return info
|
||||||
|
|
||||||
|
async def async_update(self):
|
||||||
|
"""Synchronize state with controller."""
|
||||||
|
# await self.mikrotik_controller.async_update()
|
||||||
|
return
|
||||||
|
|
||||||
|
async def async_added_to_hass(self):
|
||||||
|
"""Port entity created."""
|
||||||
|
_LOGGER.debug("New sensor %s (%s)", self._name, self.kind)
|
||||||
|
return
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
"""Return true if sensor is on."""
|
||||||
|
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]]:
|
||||||
|
val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]]
|
||||||
|
|
||||||
|
return val
|
|
@ -36,6 +36,7 @@ class MikrotikControllerData():
|
||||||
self.data['interface'] = {}
|
self.data['interface'] = {}
|
||||||
self.data['arp'] = {}
|
self.data['arp'] = {}
|
||||||
self.data['nat'] = {}
|
self.data['nat'] = {}
|
||||||
|
self.data['fw-update'] = {}
|
||||||
|
|
||||||
self.listeners = []
|
self.listeners = []
|
||||||
|
|
||||||
|
@ -44,6 +45,7 @@ class MikrotikControllerData():
|
||||||
self.api = None
|
self.api = None
|
||||||
|
|
||||||
async_track_time_interval(self.hass, self.force_update, self.option_scan_interval)
|
async_track_time_interval(self.hass, self.force_update, self.option_scan_interval)
|
||||||
|
async_track_time_interval(self.hass, self.async_fwupdate_check, timedelta(hours=1))
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -96,6 +98,17 @@ class MikrotikControllerData():
|
||||||
self.get_system_resource()
|
self.get_system_resource()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# async_fwupdate_check
|
||||||
|
# ---------------------------
|
||||||
|
async def async_fwupdate_check(self):
|
||||||
|
"""Update Mikrotik Controller data."""
|
||||||
|
|
||||||
|
self.get_firmare_update()
|
||||||
|
|
||||||
|
async_dispatcher_send(self.hass, self.signal_update)
|
||||||
|
return
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# async_update
|
# async_update
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -103,6 +116,9 @@ class MikrotikControllerData():
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update Mikrotik Controller data."""
|
"""Update Mikrotik Controller data."""
|
||||||
|
|
||||||
|
if 'available' not in self.data['fw-update']:
|
||||||
|
await self.async_fwupdate_check()
|
||||||
|
|
||||||
self.get_interfaces()
|
self.get_interfaces()
|
||||||
self.get_arp()
|
self.get_arp()
|
||||||
self.get_nat()
|
self.get_nat()
|
||||||
|
@ -322,3 +338,16 @@ class MikrotikControllerData():
|
||||||
self.data['resource']['hdd-usage'] = "unknown"
|
self.data['resource']['hdd-usage'] = "unknown"
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# get_system_routerboard
|
||||||
|
# ---------------------------
|
||||||
|
def get_firmare_update(self):
|
||||||
|
data = self.api.path("/system/package/update")
|
||||||
|
for entry in data:
|
||||||
|
self.data['fw-update']['available'] = True if entry['status'] == "New version is available" else False
|
||||||
|
self.data['fw-update']['channel'] = entry['channel'] if 'channel' in entry else "unknown"
|
||||||
|
self.data['fw-update']['installed-version'] = entry['installed-version'] if 'installed-version' in entry else "unknown"
|
||||||
|
self.data['fw-update']['latest-version'] = entry['latest-version'] if 'latest-version' in entry else "unknown"
|
||||||
|
|
||||||
|
return
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue