From 5a198e05ad48b15b01665c574a34e55d2f8eb554 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 Jan 2024 10:27:58 -1000 Subject: [PATCH] Small cleanups to ESPHome (#107924) - Remove unused variables - Remove unneeded static info lookups --- .../components/esphome/alarm_control_panel.py | 12 ++++++------ homeassistant/components/esphome/climate.py | 4 ++-- homeassistant/components/esphome/entity.py | 11 ++++------- homeassistant/components/esphome/number.py | 2 +- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/esphome/alarm_control_panel.py b/homeassistant/components/esphome/alarm_control_panel.py index 6f3f903f248..e4f44dfd1fd 100644 --- a/homeassistant/components/esphome/alarm_control_panel.py +++ b/homeassistant/components/esphome/alarm_control_panel.py @@ -89,17 +89,17 @@ class EsphomeAlarmControlPanel( super()._on_static_info_update(static_info) static_info = self._static_info feature = 0 - if self._static_info.supported_features & EspHomeACPFeatures.ARM_HOME: + if static_info.supported_features & EspHomeACPFeatures.ARM_HOME: feature |= AlarmControlPanelEntityFeature.ARM_HOME - if self._static_info.supported_features & EspHomeACPFeatures.ARM_AWAY: + if static_info.supported_features & EspHomeACPFeatures.ARM_AWAY: feature |= AlarmControlPanelEntityFeature.ARM_AWAY - if self._static_info.supported_features & EspHomeACPFeatures.ARM_NIGHT: + if static_info.supported_features & EspHomeACPFeatures.ARM_NIGHT: feature |= AlarmControlPanelEntityFeature.ARM_NIGHT - if self._static_info.supported_features & EspHomeACPFeatures.TRIGGER: + if static_info.supported_features & EspHomeACPFeatures.TRIGGER: feature |= AlarmControlPanelEntityFeature.TRIGGER - if self._static_info.supported_features & EspHomeACPFeatures.ARM_CUSTOM_BYPASS: + if static_info.supported_features & EspHomeACPFeatures.ARM_CUSTOM_BYPASS: feature |= AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS - if self._static_info.supported_features & EspHomeACPFeatures.ARM_VACATION: + if static_info.supported_features & EspHomeACPFeatures.ARM_VACATION: feature |= AlarmControlPanelEntityFeature.ARM_VACATION self._attr_supported_features = AlarmControlPanelEntityFeature(feature) self._attr_code_format = ( diff --git a/homeassistant/components/esphome/climate.py b/homeassistant/components/esphome/climate.py index 08ed2f1109d..5c265068216 100644 --- a/homeassistant/components/esphome/climate.py +++ b/homeassistant/components/esphome/climate.py @@ -167,11 +167,11 @@ class EsphomeClimateEntity(EsphomeEntity[ClimateInfo, ClimateState], ClimateEnti self._attr_min_humidity = round(static_info.visual_min_humidity) self._attr_max_humidity = round(static_info.visual_max_humidity) features = ClimateEntityFeature(0) - if self._static_info.supports_two_point_target_temperature: + if static_info.supports_two_point_target_temperature: features |= ClimateEntityFeature.TARGET_TEMPERATURE_RANGE else: features |= ClimateEntityFeature.TARGET_TEMPERATURE - if self._static_info.supports_target_humidity: + if static_info.supports_target_humidity: features |= ClimateEntityFeature.TARGET_HUMIDITY if self.preset_modes: features |= ClimateEntityFeature.PRESET_MODE diff --git a/homeassistant/components/esphome/entity.py b/homeassistant/components/esphome/entity.py index 7bd769231ac..1abf60be18a 100644 --- a/homeassistant/components/esphome/entity.py +++ b/homeassistant/components/esphome/entity.py @@ -145,7 +145,6 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]): state_type: type[_StateT], ) -> None: """Initialize.""" - self._entry_data = entry_data self._on_entry_data_changed() self._key = entity_info.key @@ -157,7 +156,6 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]): self._attr_device_info = DeviceInfo( connections={(dr.CONNECTION_NETWORK_MAC, device_info.mac_address)} ) - self._entry_id = entry_data.entry_id # # If `friendly_name` is set, we use the Friendly naming rules, if # `friendly_name` is not set we make an exception to the naming rules for @@ -183,10 +181,11 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]): entry_data = self._entry_data hass = self.hass key = self._key + static_info = self._static_info self.async_on_remove( entry_data.async_register_key_static_info_remove_callback( - self._static_info, + static_info, functools.partial(self.async_remove, force_remove=True), ) ) @@ -204,7 +203,7 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]): ) self.async_on_remove( entry_data.async_register_key_static_info_updated_callback( - self._static_info, self._on_static_info_update + static_info, self._on_static_info_update ) ) self._update_state_from_entry_data() @@ -236,12 +235,10 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]): @callback def _update_state_from_entry_data(self) -> None: """Update state from entry data.""" - state = self._entry_data.state key = self._key state_type = self._state_type - has_state = key in state[state_type] - if has_state: + if has_state := key in state[state_type]: self._state = cast(_StateT, state[state_type][key]) self._has_state = has_state diff --git a/homeassistant/components/esphome/number.py b/homeassistant/components/esphome/number.py index bc694ec39cf..f1902bdb39d 100644 --- a/homeassistant/components/esphome/number.py +++ b/homeassistant/components/esphome/number.py @@ -54,7 +54,7 @@ class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity): super()._on_static_info_update(static_info) static_info = self._static_info self._attr_device_class = try_parse_enum( - NumberDeviceClass, self._static_info.device_class + NumberDeviceClass, static_info.device_class ) self._attr_native_min_value = static_info.min_value self._attr_native_max_value = static_info.max_value