-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Tibber notify service (#116893)
* Migrate tibber notify service * Tests and repair flow * Use notify repair flow helper * Cleanup strings after using helper, use HomeAssistantError * Add entry state assertions to unload test * Update comment * Update comment
- Loading branch information
Showing
8 changed files
with
229 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""Test loading of the Tibber config entry.""" | ||
|
||
from unittest.mock import MagicMock | ||
|
||
from homeassistant.components.recorder import Recorder | ||
from homeassistant.components.tibber import DOMAIN | ||
from homeassistant.config_entries import ConfigEntryState | ||
from homeassistant.core import HomeAssistant | ||
|
||
|
||
async def test_entry_unload( | ||
recorder_mock: Recorder, hass: HomeAssistant, mock_tibber_setup: MagicMock | ||
) -> None: | ||
"""Test unloading the entry.""" | ||
entry = hass.config_entries.async_entry_for_domain_unique_id(DOMAIN, "tibber") | ||
assert entry.state == ConfigEntryState.LOADED | ||
|
||
await hass.config_entries.async_unload(entry.entry_id) | ||
mock_tibber_setup.rt_disconnect.assert_called_once() | ||
await hass.async_block_till_done(wait_background_tasks=True) | ||
assert entry.state == ConfigEntryState.NOT_LOADED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Tests for tibber notification service.""" | ||
|
||
from asyncio import TimeoutError | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from homeassistant.components.recorder import Recorder | ||
from homeassistant.components.tibber import DOMAIN | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError | ||
|
||
|
||
async def test_notification_services( | ||
recorder_mock: Recorder, hass: HomeAssistant, mock_tibber_setup: MagicMock | ||
) -> None: | ||
"""Test create entry from user input.""" | ||
# Assert notify entity has been added | ||
notify_state = hass.states.get("notify.tibber") | ||
assert notify_state is not None | ||
|
||
# Assert legacy notify service hass been added | ||
assert hass.services.has_service("notify", DOMAIN) | ||
|
||
# Test legacy notify service | ||
service = "tibber" | ||
service_data = {"message": "The message", "title": "A title"} | ||
await hass.services.async_call("notify", service, service_data, blocking=True) | ||
calls: MagicMock = mock_tibber_setup.send_notification | ||
|
||
calls.assert_called_once_with(message="The message", title="A title") | ||
calls.reset_mock() | ||
|
||
# Test notify entity service | ||
service = "send_message" | ||
service_data = { | ||
"entity_id": "notify.tibber", | ||
"message": "The message", | ||
"title": "A title", | ||
} | ||
await hass.services.async_call("notify", service, service_data, blocking=True) | ||
calls.assert_called_once_with("A title", "The message") | ||
calls.reset_mock() | ||
|
||
calls.side_effect = TimeoutError | ||
|
||
with pytest.raises(HomeAssistantError): | ||
# Test legacy notify service | ||
service = "tibber" | ||
service_data = {"message": "The message", "title": "A title"} | ||
await hass.services.async_call("notify", service, service_data, blocking=True) | ||
|
||
with pytest.raises(HomeAssistantError): | ||
# Test notify entity service | ||
service = "send_message" | ||
service_data = { | ||
"entity_id": "notify.tibber", | ||
"message": "The message", | ||
"title": "A title", | ||
} | ||
await hass.services.async_call("notify", service, service_data, blocking=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Test loading of the Tibber config entry.""" | ||
|
||
from http import HTTPStatus | ||
from unittest.mock import MagicMock | ||
|
||
from homeassistant.components.recorder import Recorder | ||
from homeassistant.components.repairs.websocket_api import ( | ||
RepairsFlowIndexView, | ||
RepairsFlowResourceView, | ||
) | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import issue_registry as ir | ||
|
||
from tests.typing import ClientSessionGenerator | ||
|
||
|
||
async def test_repair_flow( | ||
recorder_mock: Recorder, | ||
hass: HomeAssistant, | ||
issue_registry: ir.IssueRegistry, | ||
mock_tibber_setup: MagicMock, | ||
hass_client: ClientSessionGenerator, | ||
) -> None: | ||
"""Test unloading the entry.""" | ||
|
||
# Test legacy notify service | ||
service = "tibber" | ||
service_data = {"message": "The message", "title": "A title"} | ||
await hass.services.async_call("notify", service, service_data, blocking=True) | ||
calls: MagicMock = mock_tibber_setup.send_notification | ||
|
||
calls.assert_called_once_with(message="The message", title="A title") | ||
calls.reset_mock() | ||
|
||
http_client = await hass_client() | ||
# Assert the issue is present | ||
assert issue_registry.async_get_issue( | ||
domain="notify", | ||
issue_id="migrate_notify_tibber", | ||
) | ||
assert len(issue_registry.issues) == 1 | ||
|
||
url = RepairsFlowIndexView.url | ||
resp = await http_client.post( | ||
url, json={"handler": "notify", "issue_id": "migrate_notify_tibber"} | ||
) | ||
assert resp.status == HTTPStatus.OK | ||
data = await resp.json() | ||
|
||
flow_id = data["flow_id"] | ||
assert data["step_id"] == "confirm" | ||
|
||
# Simulate the users confirmed the repair flow | ||
url = RepairsFlowResourceView.url.format(flow_id=flow_id) | ||
resp = await http_client.post(url) | ||
assert resp.status == HTTPStatus.OK | ||
data = await resp.json() | ||
assert data["type"] == "create_entry" | ||
await hass.async_block_till_done() | ||
|
||
# Assert the issue is no longer present | ||
assert not issue_registry.async_get_issue( | ||
domain="notify", | ||
issue_id="migrate_notify_tibber", | ||
) | ||
assert len(issue_registry.issues) == 0 |