Skip to content

Commit 580d65e

Browse files
committed
Migrate update methods to aiohasupervisor
1 parent 1cfdabe commit 580d65e

File tree

5 files changed

+35
-104
lines changed

5 files changed

+35
-104
lines changed

homeassistant/components/hassio/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@
106106
async_reboot_host,
107107
async_set_green_settings,
108108
async_set_yellow_settings,
109-
async_update_core,
110109
async_update_diagnostics,
111-
async_update_os,
112-
async_update_supervisor,
113110
get_supervisor_client,
114111
)
115112
from .http import HassIOView

homeassistant/components/hassio/handler.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -91,52 +91,6 @@ async def async_create_backup(
9191
return await hassio.send_command(command, payload=payload, timeout=None)
9292

9393

94-
@bind_hass
95-
@api_data
96-
async def async_update_os(hass: HomeAssistant, version: str | None = None) -> dict:
97-
"""Update Home Assistant Operating System.
98-
99-
The caller of the function should handle HassioAPIError.
100-
"""
101-
hassio: HassIO = hass.data[DOMAIN]
102-
command = "/os/update"
103-
return await hassio.send_command(
104-
command,
105-
payload={"version": version},
106-
timeout=None,
107-
)
108-
109-
110-
@bind_hass
111-
@api_data
112-
async def async_update_supervisor(hass: HomeAssistant) -> dict:
113-
"""Update Home Assistant Supervisor.
114-
115-
The caller of the function should handle HassioAPIError.
116-
"""
117-
hassio: HassIO = hass.data[DOMAIN]
118-
command = "/supervisor/update"
119-
return await hassio.send_command(command, timeout=None)
120-
121-
122-
@bind_hass
123-
@api_data
124-
async def async_update_core(
125-
hass: HomeAssistant, version: str | None = None, backup: bool = False
126-
) -> dict:
127-
"""Update Home Assistant Core.
128-
129-
The caller of the function should handle HassioAPIError.
130-
"""
131-
hassio: HassIO = hass.data[DOMAIN]
132-
command = "/core/update"
133-
return await hassio.send_command(
134-
command,
135-
payload={"version": version, "backup": backup},
136-
timeout=None,
137-
)
138-
139-
14094
@bind_hass
14195
@_api_bool
14296
async def async_apply_suggestion(hass: HomeAssistant, suggestion_uuid: str) -> dict:

homeassistant/components/hassio/update.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from typing import Any
66

77
from aiohasupervisor import SupervisorError
8-
from aiohasupervisor.models import StoreAddonUpdate
8+
from aiohasupervisor.models import (
9+
HomeAssistantUpdateOptions,
10+
OSUpdate,
11+
StoreAddonUpdate,
12+
)
913
from awesomeversion import AwesomeVersion, AwesomeVersionStrategy
1014

1115
from homeassistant.components.update import (
@@ -36,12 +40,6 @@
3640
HassioOSEntity,
3741
HassioSupervisorEntity,
3842
)
39-
from .handler import (
40-
HassioAPIError,
41-
async_update_core,
42-
async_update_os,
43-
async_update_supervisor,
44-
)
4543

4644
ENTITY_DESCRIPTION = UpdateEntityDescription(
4745
name="Update",
@@ -213,8 +211,10 @@ async def async_install(
213211
) -> None:
214212
"""Install an update."""
215213
try:
216-
await async_update_os(self.hass, version)
217-
except HassioAPIError as err:
214+
await self.coordinator.supervisor_client.os.update(
215+
OSUpdate(version=version)
216+
)
217+
except SupervisorError as err:
218218
raise HomeAssistantError(
219219
f"Error updating Home Assistant Operating System: {err}"
220220
) from err
@@ -259,8 +259,8 @@ async def async_install(
259259
) -> None:
260260
"""Install an update."""
261261
try:
262-
await async_update_supervisor(self.hass)
263-
except HassioAPIError as err:
262+
await self.coordinator.supervisor_client.supervisor.update()
263+
except SupervisorError as err:
264264
raise HomeAssistantError(
265265
f"Error updating Home Assistant Supervisor: {err}"
266266
) from err
@@ -304,8 +304,10 @@ async def async_install(
304304
) -> None:
305305
"""Install an update."""
306306
try:
307-
await async_update_core(self.hass, version=version, backup=backup)
308-
except HassioAPIError as err:
307+
await self.coordinator.supervisor_client.homeassistant.update(
308+
HomeAssistantUpdateOptions(version=version, backup=backup)
309+
)
310+
except SupervisorError as err:
309311
raise HomeAssistantError(
310312
f"Error updating Home Assistant Core: {err}"
311313
) from err

tests/components/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ def supervisor_client() -> Generator[AsyncMock]:
470470
supervisor_client = AsyncMock()
471471
supervisor_client.addons = AsyncMock()
472472
supervisor_client.discovery = AsyncMock()
473+
supervisor_client.homeassistant = AsyncMock()
474+
supervisor_client.os = AsyncMock()
475+
supervisor_client.supervisor = AsyncMock()
473476
with (
474477
patch(
475478
"homeassistant.components.hassio.get_supervisor_client",

tests/components/hassio/test_update.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from aiohasupervisor.models import StoreAddonUpdate
99
import pytest
1010

11-
from homeassistant.components.hassio import DOMAIN, HassioAPIError
11+
from homeassistant.components.hassio import DOMAIN
1212
from homeassistant.components.hassio.const import REQUEST_REFRESH_DELAY
1313
from homeassistant.core import HomeAssistant
1414
from homeassistant.exceptions import HomeAssistantError
@@ -239,9 +239,7 @@ async def test_update_addon(hass: HomeAssistant, update_addon: AsyncMock) -> Non
239239
update_addon.assert_called_once_with("test", StoreAddonUpdate(backup=False))
240240

241241

242-
async def test_update_os(
243-
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
244-
) -> None:
242+
async def test_update_os(hass: HomeAssistant, supervisor_client: AsyncMock) -> None:
245243
"""Test updating OS update entity."""
246244
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
247245
config_entry.add_to_hass(hass)
@@ -255,22 +253,17 @@ async def test_update_os(
255253
assert result
256254
await hass.async_block_till_done()
257255

258-
aioclient_mock.post(
259-
"http://127.0.0.1/os/update",
260-
json={"result": "ok", "data": {}},
261-
)
262-
256+
supervisor_client.os.update.return_value = None
263257
await hass.services.async_call(
264258
"update",
265259
"install",
266260
{"entity_id": "update.home_assistant_operating_system_update"},
267261
blocking=True,
268262
)
263+
supervisor_client.os.update.assert_called_once()
269264

270265

271-
async def test_update_core(
272-
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
273-
) -> None:
266+
async def test_update_core(hass: HomeAssistant, supervisor_client: AsyncMock) -> None:
274267
"""Test updating core update entity."""
275268
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
276269
config_entry.add_to_hass(hass)
@@ -284,21 +277,18 @@ async def test_update_core(
284277
assert result
285278
await hass.async_block_till_done()
286279

287-
aioclient_mock.post(
288-
"http://127.0.0.1/core/update",
289-
json={"result": "ok", "data": {}},
290-
)
291-
280+
supervisor_client.homeassistant.update.return_value = None
292281
await hass.services.async_call(
293282
"update",
294283
"install",
295-
{"entity_id": "update.home_assistant_os_update"},
284+
{"entity_id": "update.home_assistant_core_update"},
296285
blocking=True,
297286
)
287+
supervisor_client.homeassistant.update.assert_called_once()
298288

299289

300290
async def test_update_supervisor(
301-
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
291+
hass: HomeAssistant, supervisor_client: AsyncMock
302292
) -> None:
303293
"""Test updating supervisor update entity."""
304294
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
@@ -313,17 +303,14 @@ async def test_update_supervisor(
313303
assert result
314304
await hass.async_block_till_done()
315305

316-
aioclient_mock.post(
317-
"http://127.0.0.1/supervisor/update",
318-
json={"result": "ok", "data": {}},
319-
)
320-
306+
supervisor_client.supervisor.update.return_value = None
321307
await hass.services.async_call(
322308
"update",
323309
"install",
324310
{"entity_id": "update.home_assistant_supervisor_update"},
325311
blocking=True,
326312
)
313+
supervisor_client.supervisor.update.assert_called_once()
327314

328315

329316
async def test_update_addon_with_error(
@@ -353,7 +340,7 @@ async def test_update_addon_with_error(
353340

354341

355342
async def test_update_os_with_error(
356-
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
343+
hass: HomeAssistant, supervisor_client: AsyncMock
357344
) -> None:
358345
"""Test updating OS update entity with error."""
359346
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
@@ -367,11 +354,7 @@ async def test_update_os_with_error(
367354
)
368355
await hass.async_block_till_done()
369356

370-
aioclient_mock.post(
371-
"http://127.0.0.1/os/update",
372-
exc=HassioAPIError,
373-
)
374-
357+
supervisor_client.os.update.side_effect = SupervisorError
375358
with pytest.raises(
376359
HomeAssistantError, match=r"^Error updating Home Assistant Operating System:"
377360
):
@@ -384,7 +367,7 @@ async def test_update_os_with_error(
384367

385368

386369
async def test_update_supervisor_with_error(
387-
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
370+
hass: HomeAssistant, supervisor_client: AsyncMock
388371
) -> None:
389372
"""Test updating supervisor update entity with error."""
390373
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
@@ -398,11 +381,7 @@ async def test_update_supervisor_with_error(
398381
)
399382
await hass.async_block_till_done()
400383

401-
aioclient_mock.post(
402-
"http://127.0.0.1/supervisor/update",
403-
exc=HassioAPIError,
404-
)
405-
384+
supervisor_client.supervisor.update.side_effect = SupervisorError
406385
with pytest.raises(
407386
HomeAssistantError, match=r"^Error updating Home Assistant Supervisor:"
408387
):
@@ -415,7 +394,7 @@ async def test_update_supervisor_with_error(
415394

416395

417396
async def test_update_core_with_error(
418-
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
397+
hass: HomeAssistant, supervisor_client: AsyncMock
419398
) -> None:
420399
"""Test updating core update entity with error."""
421400
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
@@ -429,11 +408,7 @@ async def test_update_core_with_error(
429408
)
430409
await hass.async_block_till_done()
431410

432-
aioclient_mock.post(
433-
"http://127.0.0.1/core/update",
434-
exc=HassioAPIError,
435-
)
436-
411+
supervisor_client.homeassistant.update.side_effect = SupervisorError
437412
with pytest.raises(
438413
HomeAssistantError, match=r"^Error updating Home Assistant Core:"
439414
):

0 commit comments

Comments
 (0)