diff --git a/homeassistant/components/switchbot_cloud/entity.py b/homeassistant/components/switchbot_cloud/entity.py index b4ddaedd7c0a..6275f1478ff6 100644 --- a/homeassistant/components/switchbot_cloud/entity.py +++ b/homeassistant/components/switchbot_cloud/entity.py @@ -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, diff --git a/tests/components/switchbot_cloud/test_entity.py b/tests/components/switchbot_cloud/test_entity.py new file mode 100644 index 000000000000..ad138e564d61 --- /dev/null +++ b/tests/components/switchbot_cloud/test_entity.py @@ -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