Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LaMetric Config Flow for SKY #93483

Merged
merged 6 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/lametric/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,18 @@ async def _async_step_create_entry(self, host: str, api_key: str) -> FlowResult:
updates={CONF_HOST: lametric.host, CONF_API_KEY: lametric.api_key}
)

notify_sound: Sound | None = None
if device.model != "sa5":
notify_sound = Sound(sound=NotificationSound.WIN)

await lametric.notify(
notification=Notification(
priority=NotificationPriority.CRITICAL,
icon_type=NotificationIconType.INFO,
model=Model(
cycles=2,
frames=[Simple(text="Connected to Home Assistant!", icon=7956)],
sound=Sound(sound=NotificationSound.WIN),
sound=notify_sound,
),
)
)
Expand Down
12 changes: 9 additions & 3 deletions tests/components/lametric/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ def mock_lametric_cloud() -> Generator[MagicMock, None, None]:


@pytest.fixture
def mock_lametric() -> Generator[MagicMock, None, None]:
"""Return a mocked LaMetric client."""
def device_fixture() -> str:
"""Return the device fixture for a specific device."""
return "device"


@pytest.fixture
def mock_lametric(request, device_fixture: str) -> Generator[MagicMock, None, None]:
"""Return a mocked LaMetric TIME client."""
with patch(
"homeassistant.components.lametric.coordinator.LaMetricDevice", autospec=True
) as lametric_mock, patch(
Expand All @@ -79,7 +85,7 @@ def mock_lametric() -> Generator[MagicMock, None, None]:
lametric.api_key = "mock-api-key"
lametric.host = "127.0.0.1"
lametric.device.return_value = Device.parse_raw(
load_fixture("device.json", DOMAIN)
load_fixture(f"{device_fixture}.json", DOMAIN)
)
yield lametric

Expand Down
71 changes: 71 additions & 0 deletions tests/components/lametric/fixtures/device_sa5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"audio": {
"volume": 100,
"volume_limit": {
"max": 100,
"min": 0
},
"volume_range": {
"max": 100,
"min": 0
}
},
"bluetooth": {
"active": true,
"address": "AA:BB:CC:DD:EE:FF",
"available": true,
"discoverable": true,
"low_energy": {
"active": true,
"advertising": true,
"connectable": true
},
"name": "SKY0123",
"pairable": false
},
"display": {
"brightness": 66,
"brightness_limit": {
"max": 100,
"min": 2
},
"brightness_mode": "manual",
"brightness_range": {
"max": 100,
"min": 0
},
"height": 8,
"on": true,
"screensaver": {
"enabled": true,
"modes": {
"screen_off": {
"enabled": false
},
"time_based": {
"enabled": false
}
},
"widget": ""
},
"type": "mixed",
"width": 64
},
"id": "12345",
"mode": "manual",
"model": "sa5",
"name": "spyfly's LaMetric SKY",
"os_version": "3.0.13",
"serial_number": "SA52100000123TBNC",
"wifi": {
"active": true,
"mac": "AA:BB:CC:DD:EE:FF",
"available": true,
"encryption": "WPA",
"ssid": "IoT",
"ip": "127.0.0.1",
"mode": "dhcp",
"netmask": "255.255.255.0",
"strength": 58
}
}
52 changes: 52 additions & 0 deletions tests/components/lametric/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
LaMetricConnectionError,
LaMetricConnectionTimeoutError,
LaMetricError,
Notification,
NotificationSound,
Sound,
)
import pytest

Expand Down Expand Up @@ -238,6 +241,10 @@ async def test_full_manual(

assert len(mock_lametric.device.mock_calls) == 1
assert len(mock_lametric.notify.mock_calls) == 1

notification: Notification = mock_lametric.notify.mock_calls[0][2]["notification"]
assert notification.model.sound == Sound(sound=NotificationSound.WIN)

assert len(mock_setup_entry.mock_calls) == 1


Expand Down Expand Up @@ -894,3 +901,48 @@ async def test_reauth_manual(

assert len(mock_lametric.device.mock_calls) == 1
assert len(mock_lametric.notify.mock_calls) == 1


@pytest.mark.usefixtures("mock_setup_entry")
@pytest.mark.parametrize("device_fixture", ["device_sa5"])
async def test_reauth_manual_sky(
hass: HomeAssistant,
mock_lametric: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reauth flow with manual entry for LaMetric Sky."""
mock_config_entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_REAUTH,
"unique_id": mock_config_entry.unique_id,
"entry_id": mock_config_entry.entry_id,
},
data=mock_config_entry.data,
)

flow_id = result["flow_id"]

await hass.config_entries.flow.async_configure(
flow_id, user_input={"next_step_id": "manual_entry"}
)

result2 = await hass.config_entries.flow.async_configure(
flow_id, user_input={CONF_API_KEY: "mock-api-key"}
)

assert result2.get("type") == FlowResultType.ABORT
assert result2.get("reason") == "reauth_successful"
assert mock_config_entry.data == {
CONF_HOST: "127.0.0.1",
CONF_API_KEY: "mock-api-key",
CONF_MAC: "AA:BB:CC:DD:EE:FF",
}

assert len(mock_lametric.device.mock_calls) == 1
assert len(mock_lametric.notify.mock_calls) == 1

notification: Notification = mock_lametric.notify.mock_calls[0][2]["notification"]
assert notification.model.sound is None