72 lines
3.1 KiB
PHP
72 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Support\Mqtt\ShellyNormalizer;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ShellyNormalizerTest extends TestCase
|
|
{
|
|
public function test_digital_input_is_a_generic_input_not_a_contact(): void
|
|
{
|
|
// A wall-switch input must NOT be assumed to be a window contact — that's opt-in per device.
|
|
$on = ShellyNormalizer::normalize('input:0', ['id' => 0, 'state' => true]);
|
|
$this->assertSame('input:0', $on[0]['key']);
|
|
$this->assertSame('input', $on[0]['type']);
|
|
$this->assertSame(['on' => true], $on[0]['state']);
|
|
|
|
$off = ShellyNormalizer::normalize('input:1', ['id' => 1, 'state' => false]);
|
|
$this->assertSame(['on' => false], $off[0]['state']);
|
|
}
|
|
|
|
public function test_analog_or_count_input_is_not_a_contact(): void
|
|
{
|
|
$this->assertSame([], ShellyNormalizer::normalize('input:0', ['id' => 0, 'percent' => 40]));
|
|
$this->assertSame([], ShellyNormalizer::normalize('input:0', ['id' => 0, 'state' => null]));
|
|
}
|
|
|
|
public function test_contact_component_supports_three_state_tilt(): void
|
|
{
|
|
$tilted = ShellyNormalizer::normalize('contact:0', ['position' => 'tilted']);
|
|
$this->assertSame('contact', $tilted[0]['type']);
|
|
$this->assertSame(['open' => true, 'position' => 'tilted'], $tilted[0]['state']);
|
|
|
|
$window = ShellyNormalizer::normalize('window:0', ['open' => true]);
|
|
$this->assertSame(['open' => true, 'position' => 'open'], $window[0]['state']);
|
|
|
|
$closed = ShellyNormalizer::normalize('contact:0', ['state' => false]);
|
|
$this->assertSame(['open' => false, 'position' => 'closed'], $closed[0]['state']);
|
|
}
|
|
|
|
public function test_switch_with_power_still_normalizes(): void
|
|
{
|
|
$updates = ShellyNormalizer::normalize('switch:0', ['output' => true, 'apower' => 12.7]);
|
|
$this->assertSame('switch', $updates[0]['type']);
|
|
$this->assertTrue($updates[0]['state']['on']);
|
|
$this->assertSame('power', $updates[1]['type']);
|
|
$this->assertSame(13, $updates[1]['state']['watts']);
|
|
}
|
|
|
|
public function test_contact_is_a_primary_onboarding_type(): void
|
|
{
|
|
$this->assertContains('contact', ShellyNormalizer::PRIMARY_TYPES);
|
|
}
|
|
|
|
public function test_light_carries_brightness_and_rgb_when_present(): void
|
|
{
|
|
$rgbw = ShellyNormalizer::normalize('light:0', ['output' => true, 'brightness' => 60, 'rgb' => [255, 10, 0]]);
|
|
$this->assertSame(['on' => true, 'brightness' => 60, 'rgb' => [255, 10, 0]], $rgbw[0]['state']);
|
|
|
|
// A plain on/off light reports no attributes.
|
|
$plain = ShellyNormalizer::normalize('light:0', ['output' => false]);
|
|
$this->assertSame(['on' => false], $plain[0]['state']);
|
|
}
|
|
|
|
public function test_housekeeping_components_are_dropped(): void
|
|
{
|
|
$this->assertSame([], ShellyNormalizer::normalize('sys', ['restart_required' => false]));
|
|
$this->assertSame([], ShellyNormalizer::normalize('wifi', ['status' => 'got ip']));
|
|
$this->assertSame([], ShellyNormalizer::normalize('cloud', ['connected' => false]));
|
|
}
|
|
}
|