Skip to content

Commit

Permalink
отвязка устройств если у них нет объектов от интеграции; правка uptim…
Browse files Browse the repository at this point in the history
…e; удален identifiers из дочерних объектов;
  • Loading branch information
malinovsku committed Aug 4, 2024
1 parent 4680941 commit eac1df8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
22 changes: 10 additions & 12 deletions custom_components/keenetic_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ async def get_api(hass: HomeAssistant, data: dict[str, Any]) -> Router:
@callback
def remove_entities_or_devices(hass, entry) -> None:
entity_registry = er.async_get(hass)
for entity in er.async_entries_for_config_entry(entity_registry, entry.entry_id):
entity_conf = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
for entity in entity_conf:
delete_ent = False
if (
entity.domain == "device_tracker"
Expand Down Expand Up @@ -194,14 +195,11 @@ def remove_entities_or_devices(hass, entry) -> None:

device_registry = dr.async_get(hass)
for device_entry in dr.async_entries_for_config_entry(device_registry, entry.entry_id):
if (
len(
er.async_entries_for_device(
entity_registry,
device_entry.id
)
)
== 0
):
_LOGGER.debug(f"Removing device: {device_entry.name} / {device_entry.identifiers}")
device_registry.async_remove_device(device_entry.id)
entity_dev = er.async_entries_for_device(entity_registry, device_entry.id)

if (len(entity_dev) == 0):
_LOGGER.debug(f"Removing device: {device_entry}")
device_registry.async_remove_device(device_entry.id)
elif not any(x in {x.entity_id for x in entity_conf} for x in {x.entity_id for x in entity_dev}):
_LOGGER.debug(f"Update device, remove_config_entry_id: {device_entry}")
device_registry.async_update_device(device_entry.id, remove_config_entry_id=entry.entry_id)
3 changes: 1 addition & 2 deletions custom_components/keenetic_api/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
UpdateFailed,
TimestampDataUpdateCoordinator,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
Expand All @@ -25,7 +24,7 @@
_LOGGER = logging.getLogger(__name__)


class KeeneticRouterCoordinator(TimestampDataUpdateCoordinator):
class KeeneticRouterCoordinator(DataUpdateCoordinator):
def __init__(
self,
hass: HomeAssistant,
Expand Down
1 change: 0 additions & 1 deletion custom_components/keenetic_api/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def is_connected(self) -> bool:
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
connections={(CONNECTION_NETWORK_MAC, self._mac)},
name=self._attr_name,
# via_device=(DOMAIN, format_mac(self._via_device_mac))
Expand Down
2 changes: 0 additions & 2 deletions custom_components/keenetic_api/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ def __init__(
self._attr_options = list([select_options[policy] for policy in select_options])
self._attr_device_info = DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, self._mac)},
identifiers={(DOMAIN, self._mac)},
# default_name=self._hostname,
name=self._hostname,
# via_device=(DOMAIN, format_mac(self.coordinator.router.mac)),
)
Expand Down
9 changes: 3 additions & 6 deletions custom_components/keenetic_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ class KeeneticRouterSensorEntityDescription(SensorEntityDescription):
attributes_fn: Callable[[KeeneticFullData], dict[str, Any]] | None = None


def convert_uptime(update_success_time: str, uptime: str) -> datetime:
def convert_uptime(uptime: str) -> datetime:
"""Convert uptime."""
time_start = update_success_time.replace(microsecond=0) - timedelta(seconds=int(uptime))
_LOGGER.debug(f"Convert uptime: {update_success_time} / {uptime} / {time_start}")
return time_start

return (datetime.now(tz=UTC) - timedelta(seconds=int(uptime))).replace(second=0, microsecond=0)

def ind_wan_ip_adress(fdata: KeeneticFullData):
"""Определение внешнего IP адреса."""
Expand Down Expand Up @@ -85,7 +82,7 @@ def ind_wan_ip_adress(fdata: KeeneticFullData):
key="uptime",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
value=lambda coordinator, key: convert_uptime(coordinator.last_update_success_time, coordinator.data.show_system[key]),
value=lambda coordinator, key: convert_uptime(coordinator.data.show_system[key]),
),
KeeneticRouterSensorEntityDescription(
key="wan_ip_adress",
Expand Down

0 comments on commit eac1df8

Please sign in to comment.