Add controller software update to Bosch SHC (#188775)

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas
2026-11-24 14:46:58 +01:00
committed by GitHub
co-authored by Claude Sonnet 5.5
parent 112e354860
commit 8857c97524
6 changed files with 290 additions and 2 deletions
@@ -27,6 +27,7 @@ PLATFORMS = [
Platform.SELECT,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,
Platform.VALVE,
]
@@ -413,6 +413,11 @@
"name": "Vibration detection"
}
},
"update": {
"controller_update": {
"name": "Software update"
}
},
"valve": {
"valve": {
"name": "Valve"
@@ -422,6 +427,12 @@
"exceptions": {
"unsupported_hvac_mode": {
"message": "This entity does not support the HVAC mode {hvac_mode}."
},
"update_install_failed": {
"message": "Failed to start the firmware update on {name}"
},
"update_not_ready": {
"message": "Firmware update for {name} cannot be activated right now (current state: {state})."
}
}
}
@@ -0,0 +1,110 @@
"""Platform for update integration."""
from datetime import timedelta
from typing import TYPE_CHECKING, Any, override
from boschshcpy import SHCInformation
from boschshcpy.exceptions import SHCException
from homeassistant.components.update import (
UpdateDeviceClass,
UpdateEntity,
UpdateEntityFeature,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import BoschConfigEntry
from .const import DOMAIN
PARALLEL_UPDATES = 1
SCAN_INTERVAL = timedelta(minutes=15)
IN_PROGRESS_STATES = {
SHCInformation.UpdateState.DOWNLOADING,
SHCInformation.UpdateState.INSTALLING,
SHCInformation.UpdateState.UPDATE_IN_PROGRESS,
}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: BoschConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the SHC update platform."""
information = config_entry.runtime_data.information
if TYPE_CHECKING:
assert information is not None and information.unique_id is not None
async_add_entities(
[ControllerUpdate(information, information.unique_id, config_entry.title)],
update_before_add=True,
)
class ControllerUpdate(UpdateEntity):
"""Firmware update of the Bosch Smart Home Controller."""
_attr_has_entity_name = True
_attr_translation_key = "controller_update"
_attr_device_class = UpdateDeviceClass.FIRMWARE
_attr_supported_features = (
UpdateEntityFeature.INSTALL | UpdateEntityFeature.PROGRESS
)
def __init__(self, information: SHCInformation, unique_id: str, title: str) -> None:
"""Initialize the controller update entity."""
self._information = information
self._title = title
self._attr_unique_id = unique_id
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, unique_id)})
@property
@override
def installed_version(self) -> str | None:
"""Return the installed firmware version."""
return self._information.version
@property
@override
def latest_version(self) -> str | None:
"""Return the latest firmware version, or the installed one if none is offered."""
return self._information.available_version or self._information.version
@property
@override
def in_progress(self) -> bool:
"""Return whether an installation is running."""
return self._information.updateState in IN_PROGRESS_STATES
def update(self) -> None:
"""Refresh the software update state of the controller."""
try:
self._information.refresh()
except SHCException:
self._attr_available = False
else:
self._attr_available = True
@override
def install(self, version: str | None, backup: bool, **kwargs: Any) -> None:
"""Start the firmware update of the controller."""
state = self._information.updateState
if state is not SHCInformation.UpdateState.UPDATE_AVAILABLE:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="update_not_ready",
translation_placeholders={"name": self._title, "state": state.name},
)
try:
self._information.start_software_update()
except SHCException as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="update_install_failed",
translation_placeholders={"name": self._title},
) from err
self.update()
+3 -1
View File
@@ -24,6 +24,7 @@ from boschshcpy import (
SHCCameraOutdoorGen2,
SHCClimateControl,
SHCHeatingCircuit,
SHCInformation,
SHCIntrusionSystem,
SHCLightSwitchBSM,
SHCMicromoduleBlinds,
@@ -157,7 +158,8 @@ def mock_session(device_buckets: dict[str, Any]) -> Generator[MagicMock]:
"""Mock SHCSession, patched in for the duration of the test."""
session = MagicMock()
session.information.unique_id = "test-mac"
session.information.updateState.name = "UP_TO_DATE"
session.information.updateState = SHCInformation.UpdateState.NO_UPDATE_AVAILABLE
session.information.available_version = None
session.information.version = "2.0"
session.device_helper = SimpleNamespace(**device_buckets)
session.intrusion_system = intrusion_system_device()
@@ -45,7 +45,7 @@ async def test_diagnostics(
assert diagnostics["entry"]["hostname"] == REDACTED
assert diagnostics["shc"] == {
"version": "2.0",
"update_state": "UP_TO_DATE",
"update_state": "NO_UPDATE_AVAILABLE",
"macAddress": REDACTED,
"ip": REDACTED,
}
+164
View File
@@ -0,0 +1,164 @@
"""Tests for the Bosch SHC update platform."""
from collections.abc import Generator
from unittest.mock import MagicMock, patch
from boschshcpy import SHCInformation
from boschshcpy.exceptions import SHCException
import pytest
from pytest_homeassistant.common import MockConfigEntry
from homeassistant.components.update import (
DOMAIN as UPDATE_DOMAIN,
UpdateEntityStateAttribute,
UpdateService,
)
from homeassistant.const import (
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
Platform,
TargetKey,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from .conftest import setup_integration
ENTITY_ID = "update.mock_title_software_update"
@pytest.fixture(autouse=True)
def platforms() -> Generator[None]:
"""Restrict bosch_shc setup to the update platform."""
with patch("homeassistant.components.bosch_shc.PLATFORMS", [Platform.UPDATE]):
yield
def _set_information(
session: MagicMock,
state: SHCInformation.UpdateState,
available_version: str | None,
) -> None:
session.information.updateState = state
session.information.available_version = available_version
@pytest.mark.usefixtures("mock_session")
async def test_up_to_date(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Without an offered update the entity is off and refreshed on setup."""
_set_information(mock_session, SHCInformation.UpdateState.NO_UPDATE_AVAILABLE, None)
await setup_integration(hass, mock_config_entry)
state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.state == STATE_OFF
assert state.attributes[UpdateEntityStateAttribute.INSTALLED_VERSION] == "2.0"
assert state.attributes[UpdateEntityStateAttribute.LATEST_VERSION] == "2.0"
mock_session.information.refresh.assert_called()
@pytest.mark.usefixtures("mock_session")
async def test_refresh_failure_on_setup(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""A failing refresh on setup still creates the entity, unavailable."""
mock_session.information.refresh.side_effect = SHCException("offline")
await setup_integration(hass, mock_config_entry)
state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.state == STATE_UNAVAILABLE
@pytest.mark.parametrize(
("update_state", "in_progress"),
[
(SHCInformation.UpdateState.UPDATE_AVAILABLE, False),
(SHCInformation.UpdateState.INSTALLING, True),
],
)
@pytest.mark.usefixtures("mock_session")
async def test_update_available(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
update_state: SHCInformation.UpdateState,
in_progress: bool,
) -> None:
"""An offered version turns the entity on and installs report progress."""
_set_information(mock_session, update_state, "2.1")
await setup_integration(hass, mock_config_entry)
state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.state == STATE_ON
assert state.attributes[UpdateEntityStateAttribute.LATEST_VERSION] == "2.1"
assert state.attributes[UpdateEntityStateAttribute.IN_PROGRESS] is in_progress
@pytest.mark.usefixtures("mock_session")
async def test_install(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Installing starts the controller software update."""
_set_information(mock_session, SHCInformation.UpdateState.UPDATE_AVAILABLE, "2.1")
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
UPDATE_DOMAIN,
UpdateService.INSTALL,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)
mock_session.information.start_software_update.assert_called_once_with()
@pytest.mark.usefixtures("mock_session")
async def test_install_not_ready(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Installing while the controller is not ready to activate is rejected."""
_set_information(mock_session, SHCInformation.UpdateState.UPDATE_FAILED, "2.1")
await setup_integration(hass, mock_config_entry)
with pytest.raises(HomeAssistantError, match="cannot be activated"):
await hass.services.async_call(
UPDATE_DOMAIN,
UpdateService.INSTALL,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)
mock_session.information.start_software_update.assert_not_called()
@pytest.mark.usefixtures("mock_session")
async def test_install_failure(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""A failing start call is raised as a Home Assistant error."""
_set_information(mock_session, SHCInformation.UpdateState.UPDATE_AVAILABLE, "2.1")
mock_session.information.start_software_update.side_effect = SHCException("boom")
await setup_integration(hass, mock_config_entry)
with pytest.raises(HomeAssistantError, match="Failed to start"):
await hass.services.async_call(
UPDATE_DOMAIN,
UpdateService.INSTALL,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)