|
1 | 1 | """Tests for the homewizard component.""" |
2 | 2 |
|
3 | 3 | from datetime import timedelta |
4 | | -from unittest.mock import MagicMock |
| 4 | +from unittest.mock import MagicMock, patch |
5 | 5 | import weakref |
6 | 6 |
|
7 | 7 | from freezegun.api import FrozenDateTimeFactory |
8 | 8 | from homewizard_energy.errors import DisabledError, UnauthorizedError |
9 | 9 | import pytest |
10 | 10 |
|
| 11 | +from homeassistant.components.homewizard import get_main_device |
11 | 12 | from homeassistant.components.homewizard.const import DOMAIN |
12 | 13 | from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState |
13 | 14 | from homeassistant.core import HomeAssistant |
| 15 | +from homeassistant.helpers import device_registry as dr, issue_registry as ir |
14 | 16 |
|
15 | 17 | from tests.common import MockConfigEntry, async_fire_time_changed |
16 | 18 |
|
@@ -122,6 +124,78 @@ async def test_load_detect_invalid_token( |
122 | 124 | assert flow["context"].get("entry_id") == mock_config_entry_v2.entry_id |
123 | 125 |
|
124 | 126 |
|
| 127 | +@pytest.mark.usefixtures("mock_homewizardenergy") |
| 128 | +async def test_load_creates_repair_issue( |
| 129 | + hass: HomeAssistant, |
| 130 | + mock_config_entry: MockConfigEntry, |
| 131 | + mock_homewizardenergy: MagicMock, |
| 132 | +) -> None: |
| 133 | + """Test setup creates repair issue for v2 API upgrade.""" |
| 134 | + mock_config_entry.add_to_hass(hass) |
| 135 | + await hass.async_block_till_done() |
| 136 | + |
| 137 | + with patch("homeassistant.components.homewizard.has_v2_api", return_value=True): |
| 138 | + await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 139 | + |
| 140 | + await hass.async_block_till_done() |
| 141 | + |
| 142 | + issue_registry = ir.async_get(hass) |
| 143 | + |
| 144 | + issue = issue_registry.async_get_issue( |
| 145 | + domain=DOMAIN, issue_id=f"migrate_to_v2_api_{mock_config_entry.entry_id}" |
| 146 | + ) |
| 147 | + assert issue is not None |
| 148 | + |
| 149 | + # Make sure title placeholder is set correctly |
| 150 | + assert issue.translation_placeholders["title"] == "Device" |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.usefixtures("mock_homewizardenergy") |
| 154 | +async def test_load_creates_repair_issue_when_name_is_updated( |
| 155 | + hass: HomeAssistant, |
| 156 | + mock_config_entry: MockConfigEntry, |
| 157 | + mock_homewizardenergy: MagicMock, |
| 158 | +) -> None: |
| 159 | + """Test setup creates repair issue for v2 API upgrade and updates title when device name changes.""" |
| 160 | + mock_config_entry.add_to_hass(hass) |
| 161 | + await hass.async_block_till_done() |
| 162 | + |
| 163 | + with patch("homeassistant.components.homewizard.has_v2_api", return_value=True): |
| 164 | + await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 165 | + |
| 166 | + await hass.async_block_till_done() |
| 167 | + |
| 168 | + issue_registry = ir.async_get(hass) |
| 169 | + issue_id = f"migrate_to_v2_api_{mock_config_entry.entry_id}" |
| 170 | + |
| 171 | + issue = issue_registry.async_get_issue(domain=DOMAIN, issue_id=issue_id) |
| 172 | + assert issue is not None |
| 173 | + |
| 174 | + # Initial title should be "Device" |
| 175 | + assert issue.translation_placeholders["title"] == "Device" |
| 176 | + |
| 177 | + # Update the device name |
| 178 | + device_registry = dr.async_get(hass) |
| 179 | + device = get_main_device(hass, mock_config_entry) |
| 180 | + |
| 181 | + # Update device name |
| 182 | + device_registry.async_update_device( |
| 183 | + device_id=device.id, |
| 184 | + name_by_user="My HomeWizard Device", |
| 185 | + ) |
| 186 | + |
| 187 | + # Reload integration to trigger issue update |
| 188 | + with patch("homeassistant.components.homewizard.has_v2_api", return_value=True): |
| 189 | + await hass.config_entries.async_reload(mock_config_entry.entry_id) |
| 190 | + await hass.async_block_till_done() |
| 191 | + |
| 192 | + issue = issue_registry.async_get_issue(domain=DOMAIN, issue_id=issue_id) |
| 193 | + assert issue is not None |
| 194 | + |
| 195 | + # Title should now reflect updated device name |
| 196 | + assert issue.translation_placeholders["title"] == "Device (My HomeWizard Device)" |
| 197 | + |
| 198 | + |
125 | 199 | @pytest.mark.usefixtures("mock_homewizardenergy") |
126 | 200 | async def test_load_removes_reauth_flow( |
127 | 201 | hass: HomeAssistant, |
|
0 commit comments