56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\HomeAssistant;
|
|
|
|
use function explode;
|
|
use function in_array;
|
|
|
|
final readonly class EntityState
|
|
{
|
|
public function __construct(
|
|
public string $entityId,
|
|
public string $state,
|
|
public array $attributes,
|
|
public string $lastChanged,
|
|
public string $lastUpdated,
|
|
public array|string|null $context = null,
|
|
) {
|
|
}
|
|
|
|
public static function fromArray(array $data): self
|
|
{
|
|
return new self(
|
|
$data['entity_id'],
|
|
$data['state'],
|
|
$data['attributes'] ?? [],
|
|
$data['last_changed'] ?? '',
|
|
$data['last_updated'] ?? '',
|
|
$data['context'] ?? null,
|
|
);
|
|
}
|
|
|
|
public function isOn(): bool
|
|
{
|
|
return in_array($this->state, ['on', 'home', 'open', 'unlocked', 'active'], true);
|
|
}
|
|
|
|
public function isOff(): bool
|
|
{
|
|
return in_array($this->state, ['off', 'away', 'closed', 'locked', 'inactive'], true);
|
|
}
|
|
|
|
public function getDomain(): string
|
|
{
|
|
$parts = explode('.', $this->entityId, 2);
|
|
|
|
return $parts[0];
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->attributes['friendly_name'] ?? $this->entityId;
|
|
}
|
|
}
|