with(['entities.state', 'room']) ->orderBy('name') ->get(); } public function summary(Collection $devices): array { $entities = $devices->flatMap->entities; return [ 'devices_online' => $devices->filter->isOnline()->count(), 'devices_total' => $devices->count(), 'lights_on' => $entities->filter(fn ($e) => in_array($e->type, ['light', 'switch']) && data_get($e->state, 'state.on') === true)->count(), 'contacts_open' => $entities->filter(fn ($e) => $e->type === 'contact' && data_get($e->state, 'state.open') === true)->count(), 'low_battery' => $entities->filter(fn ($e) => $e->type === 'battery' && (int) data_get($e->state, 'state.percent', 100) < self::LOW_BATTERY)->count(), ]; } /** @return array */ public function warnings(Collection $devices): array { $entities = $devices->flatMap->entities; $warnings = []; foreach ($devices->reject->isOnline() as $device) { $warnings[] = [ 'level' => 'offline', 'icon' => 'devices', 'title' => $device->name, 'meta' => __('devices.offline'), 'link' => null, ]; } foreach ($entities as $entity) { if ($entity->type === 'contact' && data_get($entity->state, 'state.open') === true) { $warnings[] = [ 'level' => 'warning', 'icon' => 'window', 'title' => $entity->device->name, 'meta' => __('devices.open'), 'link' => null, ]; } if ($entity->type === 'battery') { $percent = (int) data_get($entity->state, 'state.percent', 100); if ($percent < self::LOW_BATTERY) { $warnings[] = [ 'level' => 'warning', 'icon' => 'alert', 'title' => $entity->device->name, 'meta' => __('devices.battery').': '.$percent.'%', 'link' => null, ]; } } } if (! $this->health->allOnline()) { $warnings[] = [ 'level' => 'warning', 'icon' => 'activity', 'title' => __('dashboard.warn_host'), 'meta' => __('dashboard.warn_host_meta'), 'link' => route('host'), ]; } return $warnings; } }