diff --git a/custom_components/keenetic_api/__init__.py b/custom_components/keenetic_api/__init__.py index a4018cf..570c4d9 100644 --- a/custom_components/keenetic_api/__init__.py +++ b/custom_components/keenetic_api/__init__.py @@ -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" @@ -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) \ No newline at end of file + 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) \ No newline at end of file diff --git a/custom_components/keenetic_api/coordinator.py b/custom_components/keenetic_api/coordinator.py index 125cdfb..4c74ff4 100644 --- a/custom_components/keenetic_api/coordinator.py +++ b/custom_components/keenetic_api/coordinator.py @@ -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 @@ -25,7 +24,7 @@ _LOGGER = logging.getLogger(__name__) -class KeeneticRouterCoordinator(TimestampDataUpdateCoordinator): +class KeeneticRouterCoordinator(DataUpdateCoordinator): def __init__( self, hass: HomeAssistant, diff --git a/custom_components/keenetic_api/device_tracker.py b/custom_components/keenetic_api/device_tracker.py index 500b957..7c8cc90 100644 --- a/custom_components/keenetic_api/device_tracker.py +++ b/custom_components/keenetic_api/device_tracker.py @@ -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)) diff --git a/custom_components/keenetic_api/select.py b/custom_components/keenetic_api/select.py index b6884d6..c4d1b95 100644 --- a/custom_components/keenetic_api/select.py +++ b/custom_components/keenetic_api/select.py @@ -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)), ) diff --git a/custom_components/keenetic_api/sensor.py b/custom_components/keenetic_api/sensor.py index 36bb6df..97f7b7f 100644 --- a/custom_components/keenetic_api/sensor.py +++ b/custom_components/keenetic_api/sensor.py @@ -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 адреса.""" @@ -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",