def get_merged_device_data(coordinator): """Fetch and merge data from the coordinator for the given entry.""" ret = {} for family in coordinator.data: ret[family['id']] = family.copy() # Use copy to avoid modifying the original family data _devices = family.get("devices", []).copy() # Use copy to avoid modifying the original list ret[family['id']]['devices'] = {} if "devices" not in family: continue for device in _devices: device = {**device, **device.get("statistics", {})} # Merge statistics into device if device["deviceType"] == "ENERGY_STORAGE_BATTERY": space = family["spaces"].get(device["spaceId"], {}) device = {**device, **space.get("battery",{})} # Merge statistics into device ret[family['id']]['devices'][device['deviceId']] = device return ret