diff --git a/homeassistant/components/solaredge/sensor.py b/homeassistant/components/solaredge/sensor.py index 479362eb22d6..5a9913a31c65 100644 --- a/homeassistant/components/solaredge/sensor.py +++ b/homeassistant/components/solaredge/sensor.py @@ -543,7 +543,8 @@ class SolarEdgeEnergyDetailsSensor(SolarEdgeSensorEntity): """Initialize the power flow sensor.""" super().__init__(sensor_type, data_service) - self._attr_native_unit_of_measurement = data_service.unit + if data_service.unit is not None: + self._attr_native_unit_of_measurement = data_service.unit @property @override @@ -571,7 +572,8 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity): """Initialize the power flow sensor.""" super().__init__(description, data_service) - self._attr_native_unit_of_measurement = data_service.unit + if data_service.unit is not None: + self._attr_native_unit_of_measurement = data_service.unit @property @override diff --git a/tests/components/solaredge/test_sensor.py b/tests/components/solaredge/test_sensor.py index 72e9647eebd5..721c40fc6d41 100644 --- a/tests/components/solaredge/test_sensor.py +++ b/tests/components/solaredge/test_sensor.py @@ -213,6 +213,24 @@ async def test_energy_details_sensor_unknown_when_no_meters( assert state.state == STATE_UNKNOWN +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_energy_details_sensor_keeps_unit_when_api_fails( + recorder_mock: Recorder, + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + solaredge_api: Mock, +) -> None: + """Test energy detail sensors keep their unit when the initial API call fails.""" + solaredge_api.get_energy_details.return_value = {} + + await setup_integration(hass, mock_config_entry) + + state = hass.states.get("sensor.solaredge_produced_energy") + assert state is not None + assert state.state == STATE_UNAVAILABLE + assert state.attributes["unit_of_measurement"] == "Wh" + + @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_energy_details_filters_meters( recorder_mock: Recorder,