PHP code example of daedaloslabs / filament-shelly

1. Go to this page and download the library: Download daedaloslabs/filament-shelly library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

daedaloslabs / filament-shelly example snippets


use DaedalosLabs\FilamentShelly\ShellyPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugin(ShellyPlugin::make());
}

ShellyPlugin::make()
    // Reuse a device status for N seconds before asking Shelly Cloud again (default 60).
    ->cacheFor(120)

    // How often the widgets refresh in the browser (default '60s', null = never).
    ->pollingInterval('30s')

    // Where the stats render on each page: any Filament\View\PanelsRenderHook constant.
    // Default: PanelsRenderHook::PAGE_HEADER_WIDGETS_BEFORE
    ->renderHook(\Filament\View\PanelsRenderHook::PAGE_FOOTER_WIDGETS_AFTER)

    // Navigation of the settings page.
    ->navigationGroup('Settings')
    ->navigationSort(90)

    // Who can manage sensors (default: everyone who can access the panel).
    ->authorize(fn (): bool => auth()->user()->is_admin)

    // Add your own sensor types (see below). Use merge: false to drop the built-in ones.
    ->sensorTypes([SoilMoisture::class])

use DaedalosLabs\FilamentShelly\Widgets\ShellyStatsWidget;

protected function getHeaderWidgets(): array
{
    return [
        ShellyStatsWidget::make(['page' => static::class]),
    ];
}

namespace App\Shelly;

use Filament\Forms\Components\TextInput;
use DaedalosLabs\FilamentShelly\SensorTypes\Metric;
use DaedalosLabs\FilamentShelly\SensorTypes\SensorType;

class Flood extends SensorType
{
    public function key(): string
    {
        return 'flood'; // stored in the database, never change it
    }

    public function label(): string
    {
        return 'Flood sensor';
    }

    public function metrics(array $options): array
    {
        return [
            new Metric(
                key: 'flood',
                label: 'Water leak',
                paths: ['flood:0.alarm', 'flood'],        // first path that exists wins (Gen2+, then Gen1)
                icon: 'heroicon-o-exclamation-triangle',
                format: fn (bool $alarm): string => $alarm ? 'Leak!' : 'Dry',
            ),
            new Metric('battery', 'Battery', ['devicepower:0.battery.percent', 'bat.value'], unit: '%', decimals: 0),
        ];
    }

    // Optional extra fields per sensor, saved in $options. Use ->live(onBlur: true) if metrics() reads them.
    public function form(): array
    {
        return [];
    }
}

ShellyPlugin::make()->sensorTypes([\App\Shelly\Flood::class]);
bash
php artisan vendor:publish --tag="filament-shelly-migrations"
php artisan migrate