Fix add checks for None values and check if DHW is available (#151376)

This commit is contained in:
Willem-Jan van Rootselaar
2025-09-02 08:41:55 +00:00
committed by Franck Nijhof
parent e2a4a9393e
commit 9581c705b9
7 changed files with 191 additions and 6 deletions
+44
View File
@@ -91,6 +91,50 @@ async def test_climate_entity_properties(
assert state.attributes["preset_mode"] == PRESET_ECO
async def test_climate_without_current_temperature_sensor(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test climate entity when current temperature sensor is not available."""
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
# Set current_temperature to None to simulate no temperature sensor
mock_bsblan.state.return_value.current_temperature = None
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Should not crash and current_temperature should be None in attributes
state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.attributes["current_temperature"] is None
async def test_climate_without_target_temperature_sensor(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test climate entity when target temperature sensor is not available."""
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
# Set target_temperature to None to simulate no temperature sensor
mock_bsblan.state.return_value.target_temperature = None
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Should not crash and target temperature should be None in attributes
state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.attributes["temperature"] is None
@pytest.mark.parametrize(
"mode",
[HVACMode.HEAT, HVACMode.AUTO, HVACMode.OFF],
+42
View File
@@ -28,3 +28,45 @@ async def test_sensor_entity_properties(
"""Test the sensor entity properties."""
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_sensors_not_created_when_data_unavailable(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test sensors are not created when sensor data is not available."""
# Set all sensor data to None to simulate no sensors available
mock_bsblan.sensor.return_value.current_temperature = None
mock_bsblan.sensor.return_value.outside_temperature = None
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
# Should not create any sensor entities
entity_entries = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
sensor_entities = [entry for entry in entity_entries if entry.domain == "sensor"]
assert len(sensor_entities) == 0
async def test_partial_sensors_created_when_some_data_available(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test only available sensors are created when some sensor data is available."""
# Only current temperature available, outside temperature not
mock_bsblan.sensor.return_value.outside_temperature = None
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
# Should create only the current temperature sensor
entity_entries = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
sensor_entities = [entry for entry in entity_entries if entry.domain == "sensor"]
assert len(sensor_entities) == 1
assert sensor_entities[0].entity_id == ENTITY_CURRENT_TEMP
@@ -50,6 +50,33 @@ async def test_water_heater_states(
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_water_heater_no_dhw_capability(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that no water heater entity is created when DHW capability is missing."""
# Mock DHW data to simulate no water heater capability
mock_bsblan.hot_water_state.return_value.operating_mode = None
mock_bsblan.hot_water_state.return_value.nominal_setpoint = None
mock_bsblan.hot_water_state.return_value.dhw_actual_value_top_temperature = None
await setup_with_selected_platforms(
hass, mock_config_entry, [Platform.WATER_HEATER]
)
# Verify no water heater entity was created
entities = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
water_heater_entities = [
entity for entity in entities if entity.domain == Platform.WATER_HEATER
]
assert len(water_heater_entities) == 0
async def test_water_heater_entity_properties(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
@@ -208,3 +235,31 @@ async def test_operation_mode_error(
},
blocking=True,
)
async def test_water_heater_no_sensors(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test water heater when sensors are not available."""
await setup_with_selected_platforms(
hass, mock_config_entry, [Platform.WATER_HEATER]
)
# Set all sensors to None to simulate missing sensors
mock_bsblan.hot_water_state.return_value.operating_mode = None
mock_bsblan.hot_water_state.return_value.dhw_actual_value_top_temperature = None
mock_bsblan.hot_water_state.return_value.nominal_setpoint = None
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Should not crash and properties should return None
state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.attributes.get("current_operation") is None
assert state.attributes.get("current_temperature") is None
assert state.attributes.get("temperature") is None