diff --git a/homeassistant/components/indevolt/binary_sensor.py b/homeassistant/components/indevolt/binary_sensor.py index 109a9488f6bb..1be4bf6364a5 100644 --- a/homeassistant/components/indevolt/binary_sensor.py +++ b/homeassistant/components/indevolt/binary_sensor.py @@ -46,6 +46,8 @@ BINARY_SENSORS: Final = ( key=IndevoltSystem.HEATING_STATE, generation=(1,), translation_key="electric_heating_state", + on_value=1000, + off_value=1001, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, ), diff --git a/homeassistant/components/indevolt/sensor.py b/homeassistant/components/indevolt/sensor.py index eb6379191938..1331d7b0356f 100644 --- a/homeassistant/components/indevolt/sensor.py +++ b/homeassistant/components/indevolt/sensor.py @@ -939,12 +939,24 @@ class IndevoltSensorEntity(IndevoltEntity, SensorEntity): @property def available(self) -> bool: - """Return False when the device is not in the required energy mode.""" + """Return False for sensors in a non-applicable state.""" + + # Check whether device is not in the required energy mode if self.entity_description.energy_mode is not None: energy_mode = self.coordinator.data.get(IndevoltConfig.READ_ENERGY_MODE) if energy_mode != self.entity_description.energy_mode: return False + # Check whether inverter is reporting 0 degrees with heater not active (thus reporting to indicate "idle") + # Pending fix by Indevolt: https://discord.com/channels/1417471269942591571/1510277757689659522 + if self.entity_description.key == IndevoltBattery.GEN_1_INVERTER_TEMPERATURE: + inverter_temp = self.coordinator.data.get( + IndevoltBattery.GEN_1_INVERTER_TEMPERATURE + ) + heating_state = self.coordinator.data.get(IndevoltSystem.HEATING_STATE) + if inverter_temp == 0 and heating_state != 1000: + return False + return super().available @property diff --git a/tests/components/indevolt/fixtures/gen_1.json b/tests/components/indevolt/fixtures/gen_1.json index 6dcb05a0612a..676d4ed1d221 100644 --- a/tests/components/indevolt/fixtures/gen_1.json +++ b/tests/components/indevolt/fixtures/gen_1.json @@ -31,7 +31,7 @@ "6006": 277.16, "6007": 256.39, "7120": 1000, - "7121": 1, + "7121": 1001, "7600": 35.2, "7605": 28.1, "7620": 27.4, diff --git a/tests/components/indevolt/snapshots/test_binary_sensor.ambr b/tests/components/indevolt/snapshots/test_binary_sensor.ambr index c122ef7f1da7..e18c4c3425e1 100644 --- a/tests/components/indevolt/snapshots/test_binary_sensor.ambr +++ b/tests/components/indevolt/snapshots/test_binary_sensor.ambr @@ -46,7 +46,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'on', + 'state': 'off', }) # --- # name: test_binary_sensor[1][binary_sensor.bk1600_meter_connected-entry] diff --git a/tests/components/indevolt/snapshots/test_diagnostics.ambr b/tests/components/indevolt/snapshots/test_diagnostics.ambr index 1e7a2cc4c9f3..6f143b00a579 100644 --- a/tests/components/indevolt/snapshots/test_diagnostics.ambr +++ b/tests/components/indevolt/snapshots/test_diagnostics.ambr @@ -34,7 +34,7 @@ '667': 0, '7101': 5, '7120': 1000, - '7121': 1, + '7121': 1001, '7600': 35.2, '7605': 28.1, '7620': 27.4, diff --git a/tests/components/indevolt/test_sensor.py b/tests/components/indevolt/test_sensor.py index 247dc34899bb..32597a0ecbb1 100644 --- a/tests/components/indevolt/test_sensor.py +++ b/tests/components/indevolt/test_sensor.py @@ -4,7 +4,12 @@ from datetime import timedelta from unittest.mock import AsyncMock, patch from freezegun.api import FrozenDateTimeFactory -from indevolt_api import IndevoltConfig, IndevoltEnergyMode +from indevolt_api import ( + IndevoltBattery, + IndevoltConfig, + IndevoltEnergyMode, + IndevoltSystem, +) import pytest from syrupy.assertion import SnapshotAssertion @@ -115,6 +120,43 @@ async def test_realtime_sensor_energy_mode_availability( ) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +@pytest.mark.parametrize("generation", [1], indirect=True) +async def test_inverter_sensor_temperature_availability( + hass: HomeAssistant, + mock_indevolt: AsyncMock, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test inverter sensors are only available when temperature is reported.""" + with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + # Default fixture reports a valid temperature (35.2) with heating off, sensor should be available + assert hass.states.get("sensor.bk1600_inverter_temperature").state == "35.2" + + # Set temperature to 0 with heating off - sensor should become unavailable + mock_indevolt.fetch_data.return_value[ + IndevoltBattery.GEN_1_INVERTER_TEMPERATURE + ] = 0 + + freezer.tick(delta=timedelta(seconds=SCAN_INTERVAL)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert ( + hass.states.get("sensor.bk1600_inverter_temperature").state == STATE_UNAVAILABLE + ) + + # Switch heating on, sensor should be available again with value 0 + mock_indevolt.fetch_data.return_value[IndevoltSystem.HEATING_STATE] = 1000 + freezer.tick(delta=timedelta(seconds=SCAN_INTERVAL)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get("sensor.bk1600_inverter_temperature").state == "0" + + # In individual tests, you can override the mock behavior async def test_battery_pack_filtering( hass: HomeAssistant,