From 65f007ace7a8346661e97d1f98f81d02b4a61f5b Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Sun, 1 Sep 2024 00:28:35 +1000 Subject: [PATCH] Remove HVAC Modes when no scopes in Teslemetry (#124612) * Remove modes when not scoped * Fix inits * Re-add raise * Remove unused raise_for_scope * Set hvac_modes when not scoped * tests --- .../components/teslemetry/climate.py | 36 +++++---- .../teslemetry/snapshots/test_climate.ambr | 79 +++++++++++++++++++ tests/components/teslemetry/test_climate.py | 15 +++- 3 files changed, 112 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/teslemetry/climate.py b/homeassistant/components/teslemetry/climate.py index bd4fb0eba53..9fc68688271 100644 --- a/homeassistant/components/teslemetry/climate.py +++ b/homeassistant/components/teslemetry/climate.py @@ -84,8 +84,10 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity): ) -> None: """Initialize the climate.""" self.scoped = Scope.VEHICLE_CMDS in scopes + if not self.scoped: self._attr_supported_features = ClimateEntityFeature(0) + self._attr_hvac_modes = [] super().__init__( data, @@ -102,6 +104,10 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity): else: self._attr_hvac_mode = HVACMode.OFF + # If not scoped, prevent the user from changing the HVAC mode by making it the only option + if self._attr_hvac_mode and not self.scoped: + self._attr_hvac_modes = [self._attr_hvac_mode] + self._attr_current_temperature = self.get("climate_state_inside_temp") self._attr_target_temperature = self.get(f"climate_state_{self.key}_setting") self._attr_preset_mode = self.get("climate_state_climate_keeper_mode") @@ -114,7 +120,6 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity): async def async_turn_on(self) -> None: """Set the climate state to on.""" - self.raise_for_scope() await self.wake_up_if_asleep() await handle_vehicle_command(self.api.auto_conditioning_start()) @@ -124,7 +129,6 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity): async def async_turn_off(self) -> None: """Set the climate state to off.""" - self.raise_for_scope() await self.wake_up_if_asleep() await handle_vehicle_command(self.api.auto_conditioning_stop()) @@ -135,7 +139,6 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity): async def async_set_temperature(self, **kwargs: Any) -> None: """Set the climate temperature.""" - if temp := kwargs.get(ATTR_TEMPERATURE): await self.wake_up_if_asleep() await handle_vehicle_command( @@ -206,20 +209,21 @@ class TeslemetryCabinOverheatProtectionEntity(TeslemetryVehicleEntity, ClimateEn ) -> None: """Initialize the climate.""" + self.scoped = Scope.VEHICLE_CMDS in scopes + if self.scoped: + self._attr_supported_features = ( + ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF + ) + else: + self._attr_supported_features = ClimateEntityFeature(0) + self._attr_hvac_modes = [] + super().__init__(data, "climate_state_cabin_overheat_protection") - # Supported Features - self._attr_supported_features = ( - ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF - ) - if self.get("vehicle_config_cop_user_set_temp_supported"): + # Supported Features from data + if self.scoped and self.get("vehicle_config_cop_user_set_temp_supported"): self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE - # Scopes - self.scoped = Scope.VEHICLE_CMDS in scopes - if not self.scoped: - self._attr_supported_features = ClimateEntityFeature(0) - def _async_update_attrs(self) -> None: """Update the attributes of the entity.""" @@ -228,6 +232,10 @@ class TeslemetryCabinOverheatProtectionEntity(TeslemetryVehicleEntity, ClimateEn else: self._attr_hvac_mode = COP_MODES.get(state) + # If not scoped, prevent the user from changing the HVAC mode by making it the only option + if self._attr_hvac_mode and not self.scoped: + self._attr_hvac_modes = [self._attr_hvac_mode] + if (level := self.get("climate_state_cop_activation_temperature")) is None: self._attr_target_temperature = None else: @@ -245,8 +253,6 @@ class TeslemetryCabinOverheatProtectionEntity(TeslemetryVehicleEntity, ClimateEn async def async_set_temperature(self, **kwargs: Any) -> None: """Set the climate temperature.""" - self.raise_for_scope() - if not (temp := kwargs.get(ATTR_TEMPERATURE)): return diff --git a/tests/components/teslemetry/snapshots/test_climate.ambr b/tests/components/teslemetry/snapshots/test_climate.ambr index b65796fe10e..f5a95c7e3f2 100644 --- a/tests/components/teslemetry/snapshots/test_climate.ambr +++ b/tests/components/teslemetry/snapshots/test_climate.ambr @@ -280,6 +280,85 @@ 'state': 'off', }) # --- +# name: test_climate_noscope[climate.test_cabin_overheat_protection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'target_temp_step': 5, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': , + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_cabin_overheat_protection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cabin overheat protection', + 'platform': 'teslemetry', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_cabin_overheat_protection', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_cabin_overheat_protection', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate_noscope[climate.test_climate-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_climate', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Climate', + 'platform': 'teslemetry', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': , + 'unique_id': 'LRWXF7EK4KC700000-driver_temp', + 'unit_of_measurement': None, + }) +# --- # name: test_climate_offline[climate.test_cabin_overheat_protection-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/teslemetry/test_climate.py b/tests/components/teslemetry/test_climate.py index 31a39f1f21a..3cb4b67dc54 100644 --- a/tests/components/teslemetry/test_climate.py +++ b/tests/components/teslemetry/test_climate.py @@ -1,6 +1,6 @@ """Test the Teslemetry climate platform.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, patch from freezegun.api import FrozenDateTimeFactory import pytest @@ -371,12 +371,21 @@ async def test_asleep_or_offline( async def test_climate_noscope( hass: HomeAssistant, - mock_metadata, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + mock_metadata: AsyncMock, ) -> None: """Tests that the climate entity is correct.""" mock_metadata.return_value = METADATA_NOSCOPE - await setup_platform(hass, [Platform.CLIMATE]) + entry = await setup_platform(hass, [Platform.CLIMATE]) + + entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id) + + assert entity_entries + for entity_entry in entity_entries: + assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry") + entity_id = "climate.test_climate" with pytest.raises(ServiceValidationError):