Refactored model_async_setup_entry

This commit is contained in:
Tomaae 2022-08-19 14:04:13 +02:00
parent dc2e804f0e
commit f487441038
No known key found for this signature in database
GPG key ID: 8360BBD8A381D1C0

View file

@ -22,6 +22,51 @@ from .const import (
_LOGGER = getLogger(__name__) _LOGGER = getLogger(__name__)
def _skip_sensor(config_entry, uid_sensor, uid_data, uid) -> bool:
# Sensors
if (
uid_sensor.func == "MikrotikInterfaceTrafficSensor"
and not config_entry.options.get(
CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC
)
):
return True
if (
uid_sensor.func == "MikrotikInterfaceTrafficSensor"
and uid_data[uid]["type"] == "bridge"
):
return True
if (
uid_sensor.func == "MikrotikClientTrafficSensor"
and uid_sensor.data_attribute not in uid_data[uid].keys()
):
return True
# Binary sensors
if (
uid_sensor.func == "MikrotikPortBinarySensor"
and uid_data[uid]["type"] == "wlan"
):
return True
if uid_sensor.func == "MikrotikPortBinarySensor" and not config_entry.options.get(
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
):
return True
# Device Tracker
if (
# Skip if host tracking is disabled
uid_sensor.func == "MikrotikHostDeviceTracker"
and not config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS)
):
return True
return False
# --------------------------- # ---------------------------
# model_async_setup_entry # model_async_setup_entry
# --------------------------- # ---------------------------
@ -70,7 +115,7 @@ def model_update_items(
sensor_types, sensor_types,
): ):
def _register_entity(_sensors, _item_id, _uid, _uid_sensor): def _register_entity(_sensors, _item_id, _uid, _uid_sensor):
_LOGGER.debug("Updating entity %s", _item_id) _LOGGER.debug("Updating entity %s (%s)", inst, _item_id)
if _item_id in _sensors: if _item_id in _sensors:
return None return None
@ -85,7 +130,6 @@ def model_update_items(
for sensor in sensor_types: for sensor in sensor_types:
uid_sensor = sensor_types[sensor] uid_sensor = sensor_types[sensor]
if not uid_sensor.data_reference: if not uid_sensor.data_reference:
uid_sensor = sensor_types[sensor]
if ( if (
uid_sensor.data_attribute uid_sensor.data_attribute
not in mikrotik_controller.data[uid_sensor.data_path] not in mikrotik_controller.data[uid_sensor.data_path]
@ -101,54 +145,9 @@ def model_update_items(
sensors[item_id] = tmp sensors[item_id] = tmp
new_sensors.append(sensors[item_id]) new_sensors.append(sensors[item_id])
else: else:
# Sensors
if (
uid_sensor.func == "MikrotikInterfaceTrafficSensor"
and not config_entry.options.get(
CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC
)
):
continue
for uid in mikrotik_controller.data[uid_sensor.data_path]: for uid in mikrotik_controller.data[uid_sensor.data_path]:
uid_data = mikrotik_controller.data[uid_sensor.data_path] uid_data = mikrotik_controller.data[uid_sensor.data_path]
if _skip_sensor(config_entry, uid_sensor, uid_data, uid):
# Sensors
if (
uid_sensor.func == "MikrotikInterfaceTrafficSensor"
and uid_data[uid]["type"] == "bridge"
):
continue
if (
uid_sensor.func == "MikrotikClientTrafficSensor"
and uid_sensor.data_attribute not in uid_data[uid].keys()
):
continue
# Binary sensors
if (
uid_sensor.func == "MikrotikPortBinarySensor"
and uid_data[uid]["type"] == "wlan"
):
continue
if (
uid_sensor.func == "MikrotikPortBinarySensor"
and not config_entry.options.get(
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
)
):
continue
# Device Tracker
if (
# Skip if host tracking is disabled
uid_sensor.func == "MikrotikHostDeviceTracker"
and not config_entry.options.get(
CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS
)
):
continue continue
item_id = f"{inst}-{sensor}-{str(uid_data[uid][uid_sensor.data_reference]).lower()}" item_id = f"{inst}-{sensor}-{str(uid_data[uid][uid_sensor.data_reference]).lower()}"