diff --git a/homeassistant/components/husqvarna_automower/manifest.json b/homeassistant/components/husqvarna_automower/manifest.json index 49a554f2e0a..ed013f2e0c2 100644 --- a/homeassistant/components/husqvarna_automower/manifest.json +++ b/homeassistant/components/husqvarna_automower/manifest.json @@ -7,5 +7,5 @@ "documentation": "https://www.home-assistant.io/integrations/husqvarna_automower", "iot_class": "cloud_push", "loggers": ["aioautomower"], - "requirements": ["aioautomower==2024.3.2"] + "requirements": ["aioautomower==2024.3.3"] } diff --git a/homeassistant/components/husqvarna_automower/sensor.py b/homeassistant/components/husqvarna_automower/sensor.py index 40f676df541..857d3039e22 100644 --- a/homeassistant/components/husqvarna_automower/sensor.py +++ b/homeassistant/components/husqvarna_automower/sensor.py @@ -70,6 +70,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( device_class=SensorDeviceClass.DURATION, native_unit_of_measurement=UnitOfTime.SECONDS, suggested_unit_of_measurement=UnitOfTime.HOURS, + exists_fn=lambda data: data.statistics.total_charging_time is not None, value_fn=lambda data: data.statistics.total_charging_time, ), AutomowerSensorEntityDescription( @@ -80,6 +81,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( device_class=SensorDeviceClass.DURATION, native_unit_of_measurement=UnitOfTime.SECONDS, suggested_unit_of_measurement=UnitOfTime.HOURS, + exists_fn=lambda data: data.statistics.total_cutting_time is not None, value_fn=lambda data: data.statistics.total_cutting_time, ), AutomowerSensorEntityDescription( @@ -90,6 +92,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( device_class=SensorDeviceClass.DURATION, native_unit_of_measurement=UnitOfTime.SECONDS, suggested_unit_of_measurement=UnitOfTime.HOURS, + exists_fn=lambda data: data.statistics.total_running_time is not None, value_fn=lambda data: data.statistics.total_running_time, ), AutomowerSensorEntityDescription( @@ -100,6 +103,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( device_class=SensorDeviceClass.DURATION, native_unit_of_measurement=UnitOfTime.SECONDS, suggested_unit_of_measurement=UnitOfTime.HOURS, + exists_fn=lambda data: data.statistics.total_searching_time is not None, value_fn=lambda data: data.statistics.total_searching_time, ), AutomowerSensorEntityDescription( @@ -107,6 +111,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( translation_key="number_of_charging_cycles", entity_category=EntityCategory.DIAGNOSTIC, state_class=SensorStateClass.TOTAL, + exists_fn=lambda data: data.statistics.number_of_charging_cycles is not None, value_fn=lambda data: data.statistics.number_of_charging_cycles, ), AutomowerSensorEntityDescription( @@ -114,6 +119,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( translation_key="number_of_collisions", entity_category=EntityCategory.DIAGNOSTIC, state_class=SensorStateClass.TOTAL, + exists_fn=lambda data: data.statistics.number_of_collisions is not None, value_fn=lambda data: data.statistics.number_of_collisions, ), AutomowerSensorEntityDescription( @@ -124,6 +130,7 @@ SENSOR_TYPES: tuple[AutomowerSensorEntityDescription, ...] = ( device_class=SensorDeviceClass.DISTANCE, native_unit_of_measurement=UnitOfLength.METERS, suggested_unit_of_measurement=UnitOfLength.KILOMETERS, + exists_fn=lambda data: data.statistics.total_drive_distance is not None, value_fn=lambda data: data.statistics.total_drive_distance, ), AutomowerSensorEntityDescription( diff --git a/requirements_all.txt b/requirements_all.txt index 71580e8c804..24c87de9198 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -203,7 +203,7 @@ aioaseko==0.1.1 aioasuswrt==1.4.0 # homeassistant.components.husqvarna_automower -aioautomower==2024.3.2 +aioautomower==2024.3.3 # homeassistant.components.azure_devops aioazuredevops==1.3.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 9b80eb8a119..9094e7efd0d 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -182,7 +182,7 @@ aioaseko==0.1.1 aioasuswrt==1.4.0 # homeassistant.components.husqvarna_automower -aioautomower==2024.3.2 +aioautomower==2024.3.3 # homeassistant.components.azure_devops aioazuredevops==1.3.5 diff --git a/tests/components/husqvarna_automower/test_sensor.py b/tests/components/husqvarna_automower/test_sensor.py index dd12e0942c7..bc464b2ce78 100644 --- a/tests/components/husqvarna_automower/test_sensor.py +++ b/tests/components/husqvarna_automower/test_sensor.py @@ -6,6 +6,7 @@ from unittest.mock import AsyncMock, patch from aioautomower.model import MowerModes from aioautomower.utils import mower_list_to_dictionary_dataclass from freezegun.api import FrozenDateTimeFactory +import pytest from syrupy import SnapshotAssertion from homeassistant.components.husqvarna_automower.const import DOMAIN @@ -60,17 +61,36 @@ async def test_cutting_blade_usage_time_sensor( assert state is not None assert state.state == "0.034" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await hass.config_entries.async_remove(entry.entry_id) - await hass.async_block_till_done() + +@pytest.mark.parametrize( + ("sensor_to_test"), + [ + ("cutting_blade_usage_time"), + ("number_of_charging_cycles"), + ("number_of_collisions"), + ("total_charging_time"), + ("total_cutting_time"), + ("total_running_time"), + ("total_searching_time"), + ("total_drive_distance"), + ], +) +async def test_statistics_not_available( + hass: HomeAssistant, + mock_automower_client: AsyncMock, + mock_config_entry: MockConfigEntry, + sensor_to_test: str, +) -> None: + """Test if this sensor is only added, if data is available.""" + values = mower_list_to_dictionary_dataclass( load_json_value_fixture("mower.json", DOMAIN) ) - delattr(values[TEST_MOWER_ID].statistics, "cutting_blade_usage_time") + delattr(values[TEST_MOWER_ID].statistics, sensor_to_test) mock_automower_client.get_status.return_value = values await setup_integration(hass, mock_config_entry) - state = hass.states.get("sensor.test_mower_1_cutting_blade_usage_time") + state = hass.states.get(f"sensor.test_mower_1_{sensor_to_test}") assert state is None