diff --git a/homeassistant/components/my_pv/water_heater.py b/homeassistant/components/my_pv/water_heater.py index f9010c286176..233335ecee48 100644 --- a/homeassistant/components/my_pv/water_heater.py +++ b/homeassistant/components/my_pv/water_heater.py @@ -17,7 +17,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .const import DOMAIN from .coordinator import MyPVConfigEntry, MyPVCoordinator -from .entity import MyPVDataEntity +from .entity import MyPVBaseEntity async def async_setup_entry( @@ -29,8 +29,10 @@ async def async_setup_entry( coordinator = config_entry.runtime_data entities = [] - if coordinator.device.supports_main_mode(MyPVDeviceMainMode.HOT_WATER) and ( - configuration := coordinator.device.get_setup_configuration("ww1target") + if ( + coordinator.device.supports_main_mode(MyPVDeviceMainMode.HOT_WATER) + and coordinator.device.current_temperature is not None + and (configuration := coordinator.device.get_setup_configuration("ww1target")) ): entity_description = WaterHeaterEntityDescription( key="temp1", @@ -47,7 +49,7 @@ async def async_setup_entry( async_add_entities(entities) -class MyPVWaterHeater(MyPVDataEntity, WaterHeaterEntity): +class MyPVWaterHeater(MyPVBaseEntity, WaterHeaterEntity): """my-PV water heater.""" _attr_name = None diff --git a/tests/components/my_pv/test_water_heater.py b/tests/components/my_pv/test_water_heater.py index c33f1477966e..428ea78e68f2 100644 --- a/tests/components/my_pv/test_water_heater.py +++ b/tests/components/my_pv/test_water_heater.py @@ -1,6 +1,6 @@ """Test the my-PV water heater.""" -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import AsyncMock, patch from my_pv.exceptions import MyPVAuthenticationError, MyPVConnectionError import pytest @@ -47,6 +47,23 @@ async def test_water_heater( await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) +async def test_water_heater_no_temp_sensor( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_my_pv_client: AsyncMock, +) -> None: + """Test if a water_heater is not created when there is no temperature sensor connected.""" + mock_config_entry.add_to_hass(hass) + + mock_my_pv_client.current_temperature = None + + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get("water_heater.my_pv_ac_elwa_2") + assert state is None + + async def test_water_heater_unavailable_not_connected( hass: HomeAssistant, mock_config_entry: MockConfigEntry, @@ -65,24 +82,6 @@ async def test_water_heater_unavailable_not_connected( assert state.state == STATE_UNAVAILABLE -async def test_water_heater_unavailable_data_value_none( - hass: HomeAssistant, - mock_config_entry: MockConfigEntry, - mock_my_pv_client: AsyncMock, -) -> None: - """Test if a water_heater is unavailable when data value is None.""" - - mock_config_entry.add_to_hass(hass) - - mock_my_pv_client.get_data_value = Mock(return_value=None) - - assert await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() - - state = hass.states.get("water_heater.my_pv_ac_elwa_2") - assert state.state == STATE_UNAVAILABLE - - async def test_water_heater_turn_off( hass: HomeAssistant, mock_config_entry: MockConfigEntry,