Cast SwitchBot Cloud device sw_version to string (#174167)

This commit is contained in:
Bernát Gábor authored and GitHub committed 2026-06-18 09:38:45 +02:00
1 parent 605f69f056
commit 409ac3fd82
2 files changed
+57 -2

No files matched your search

@@ -36,8 +36,11 @@ class SwitchBotCloudEntity(CoordinatorEntity[SwitchBotCoordinator]):
self._api = api
self._attr_unique_id = device.device_id
_sw_version = None
if self.coordinator.data is not None:
_sw_version = self.coordinator.data.get("version")
if (
self.coordinator.data is not None
and (_version := self.coordinator.data.get("version")) is not None
):
_sw_version = str(_version)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device.device_id)},
name=device.device_name,
@@ -0,0 +1,52 @@
"""Test for the switchbot_cloud base entity."""
from unittest.mock import patch
from homeassistant.components.switchbot_cloud.const import DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from . import METER_INFO, configure_integration
async def test_sw_version_cast_to_string(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_list_devices,
mock_get_status,
mock_setup_webhook,
) -> None:
"""Test the device sw_version is a string when the API returns an int."""
mock_list_devices.return_value = [METER_INFO]
mock_get_status.return_value = {"version": 123}
with patch("homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.SENSOR]):
await configure_integration(hass)
device = device_registry.async_get_device(
identifiers={(DOMAIN, METER_INFO.device_id)}
)
assert device is not None
assert device.sw_version == "123"
async def test_sw_version_none_when_missing(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_list_devices,
mock_get_status,
mock_setup_webhook,
) -> None:
"""Test the device sw_version is None when the API omits the version."""
mock_list_devices.return_value = [METER_INFO]
mock_get_status.return_value = {}
with patch("homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.SENSOR]):
await configure_integration(hass)
device = device_registry.async_get_device(
identifiers={(DOMAIN, METER_INFO.device_id)}
)
assert device is not None
assert device.sw_version is None