Skip to content

Commit

Permalink
в настройки добавлена выборка по каким устройствам создавать device_t…
Browse files Browse the repository at this point in the history
…racker
  • Loading branch information
malinovsku committed Aug 11, 2024
1 parent 8092841 commit 2e9e60e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
12 changes: 7 additions & 5 deletions custom_components/keenetic_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
CONF_CLIENTS_SELECT_POLICY,
CONF_CREATE_PORT_FRW,
CONF_CREATE_IMAGE_QR,
CONF_SELECT_CREATE_DT,
)

PLATFORMS: list[Platform] = [
Expand Down Expand Up @@ -162,26 +163,27 @@ def remove_entities_or_devices(hass, entry) -> None:
for entity in entity_conf:
delete_ent = False
if (
entity.domain == "device_tracker"
and not entry.options.get(CONF_CREATE_DT, True)
entity.domain == "device_tracker"
and not entry.options.get(CONF_CREATE_DT, False)
and hass.states.get(entity.entity_id).attributes.get("mac") not in entry.options.get(CONF_SELECT_CREATE_DT, [])
):
delete_ent = True
elif (
entity.domain == "switch"
and entity.translation_key == "port_forwarding"
and not entry.options.get(CONF_CREATE_PORT_FRW, True)
and not entry.options.get(CONF_CREATE_PORT_FRW, False)
):
delete_ent = True
elif (
entity.domain == "image"
and entity.translation_key == "qrwifi"
and not entry.options.get(CONF_CREATE_IMAGE_QR, True)
and not entry.options.get(CONF_CREATE_IMAGE_QR, False)
):
delete_ent = True
elif (
entity.domain == "select"
and entity.translation_key == "client_policy"
and not entry.options.get(CONF_CREATE_ALL_CLIENTS_POLICY, True)
and not entry.options.get(CONF_CREATE_ALL_CLIENTS_POLICY, False)
and hass.states.get(entity.entity_id).attributes.get("mac") not in entry.options.get(CONF_CLIENTS_SELECT_POLICY, [])
):
delete_ent = True
Expand Down
18 changes: 16 additions & 2 deletions custom_components/keenetic_api/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CONF_CREATE_PORT_FRW,
DEFAULT_BACKUP_TYPE_FILE,
CONF_BACKUP_TYPE_FILE,
CONF_SELECT_CREATE_DT,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -118,11 +119,18 @@ async def async_step_configure_router(
client['mac']: f"{client['name'] or client['hostname']} ({client['mac']})"
for client in data_clients
}
clients |= {
clients_policy = clients
clients_policy |= {
mac: f"Unknown ({mac})"
for mac in self._options.get(CONF_CLIENTS_SELECT_POLICY, [])
if mac not in clients
}
clients_dt = clients
clients_dt |= {
mac: f"Unknown ({mac})"
for mac in self._options.get(CONF_SELECT_CREATE_DT, [])
if mac not in clients
}

return self.async_show_form(
step_id="configure_router",
Expand Down Expand Up @@ -150,14 +158,20 @@ async def async_step_configure_router(
CONF_CLIENTS_SELECT_POLICY,
default=self._options.get(CONF_CLIENTS_SELECT_POLICY, []),
): cv.multi_select(
dict(sorted(clients.items(), key=operator.itemgetter(1)))
dict(sorted(clients_policy.items(), key=operator.itemgetter(1)))
),
vol.Optional(
CONF_CREATE_DT,
default=self._options.get(
CONF_CREATE_DT, False
),
): bool,
vol.Optional(
CONF_SELECT_CREATE_DT,
default=self._options.get(CONF_SELECT_CREATE_DT, []),
): cv.multi_select(
dict(sorted(clients_dt.items(), key=operator.itemgetter(1)))
),
vol.Optional(
CONF_CREATE_PORT_FRW,
default=self._options.get(
Expand Down
1 change: 1 addition & 0 deletions custom_components/keenetic_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CONF_BACKUP_TYPE_FILE: Final = "backup_type_file"

CONF_CREATE_DT: Final = "create_device_tracker"
CONF_SELECT_CREATE_DT: Final = "create_select_device_tracker"

FW_SANDBOX: Final = {
"stable": "main",
Expand Down
11 changes: 6 additions & 5 deletions custom_components/keenetic_api/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DOMAIN,
COORD_FULL,
CONF_CREATE_DT,
CONF_SELECT_CREATE_DT,
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -33,17 +34,17 @@ async def async_setup_entry(

@callback
def async_update_router() -> None:
new_device: list[KeeneticScannerEntity] = []
if entry.options.get(CONF_CREATE_DT):
for mac, device in coordinator.data.show_ip_hotspot.items():
device_trackers: list[KeeneticScannerEntity] = []
for mac, device in coordinator.data.show_ip_hotspot.items():
if mac in entry.options.get(CONF_SELECT_CREATE_DT, []) or entry.options.get(CONF_CREATE_DT, False):
if mac not in tracked:
tracked[mac] = KeeneticScannerEntity(
coordinator,
mac,
device.name or device.hostname or device.mac,
)
new_device.append(tracked[mac])
async_add_entities(new_device)
device_trackers.append(tracked[mac])
async_add_entities(device_trackers)

entry.async_on_unload(coordinator.async_add_listener(async_update_router))
async_update_router()
Expand Down
11 changes: 6 additions & 5 deletions custom_components/keenetic_api/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
"description": "Дополнительные настройки.",
"data": {
"scan_interval": "Интервал скарирования (секунд).",
"create_image_qr": "Создать объекты Image QG WiFi.",
"create_entity_all_cliens_button_policy": "Создать объекты Select политик для всех устройств.",
"cliens_select_policy": "Создать объекты Select политик по выбранным устройствам.",
"create_device_tracker": "Создать объекты device_tracker по всем устройствам.",
"create_entity_port_forwarding": "Создать объекты Switch по всем port forwarding.",
"create_image_qr": "Создать Image QG WiFi.",
"create_entity_all_cliens_button_policy": "Создать Select политик для всех устройств.",
"cliens_select_policy": "Создать Select политик по выбранным устройствам.",
"create_device_tracker": "Создать Device tracker по всем устройствам.",
"create_select_device_tracker": "Создать Device tracker по выбранным устройствам.",
"create_entity_port_forwarding": "Создать Switch по всем port forwarding.",
"backup_type_file": "Файлы бекапа для скачивания при обновлении."
}
},
Expand Down

0 comments on commit 2e9e60e

Please sign in to comment.