Repository: home-assistant/core
File: homeassistant/components/bsblan/climate.py (Python)

Code before:

        # Set temperature range if available, otherwise use Home Assistant defaults
        if data.static.min_temp is not None and data.static.min_temp.value is not None:
            self._attr_min_temp = data.static.min_temp.value
        if data.static.max_temp is not None and data.static.max_temp.value is not None:
            self._attr_max_temp = data.static.max_temp.value
        self._attr_temperature_unit = data.fast_coordinator.client.get_temperature_unit

    @property
    def current_temperature(self) -> float | None:
        """Return the current temperature."""
        if (current_temp := self.coordinator.data.state.current_temperature) is None:
            return None
        return current_temp.value

    @property
    def target_temperature(self) -> float | None:
        """Return the temperature we try to reach."""
        if (target_temp := self.coordinator.data.state.target_temperature) is None:
            return None
        return target_temp.value

    @property
    def _hvac_mode_value(self) -> int | None:
        """Return the raw hvac_mode value from the coordinator."""
        return getattr(self.coordinator.data.state.hvac_mode, "value", None)

    @property
    def hvac_mode(self) -> HVACMode | None:
        """Return hvac operation ie. heat, cool mode."""
        if (hvac_mode_value := self._hvac_mode_value) is None:
            return None
        return BSBLAN_TO_HA_HVAC_MODE.get(hvac_mode_value)

    @property
    def hvac_action(self) -> HVACAction | None:
        """Return the current running hvac action."""
        if (
            action := self.coordinator.data.state.hvac_action
        ) is None or action.value is None:
            return None
        category = get_hvac_action_category(action.value)
        return HVACAction(category.name.lower())

    @property
    def preset_mode(self) -> str | None:
        """Return the current preset mode."""
        # BSB-Lan mode 2 is eco/reduced mode
        if self._hvac_mode_value == 2:
            return PRESET_ECO
        return PRESET_NONE

Review comment (suggestion):
I actually prefer the old style. `getattr` is not typesafe and thus this makes typing worse. Unless `hvac_mode` always returns something that is not `None`, then we can just always do `hvac_mode.value`

Code after:

        # Set temperature range if available, otherwise use Home Assistant defaults
        if data.static.min_temp is not None and data.static.min_temp.value is not None:
            self._attr_min_temp = data.static.min_temp.value
        if data.static.max_temp is not None and data.static.max_temp.value is not None:
            self._attr_max_temp = data.static.max_temp.value
        self._attr_temperature_unit = data.fast_coordinator.client.get_temperature_unit

    @property
    def current_temperature(self) -> float | None:
        """Return the current temperature."""
        if (current_temp := self.coordinator.data.state.current_temperature) is None:
            return None
        return current_temp.value

    @property
    def target_temperature(self) -> float | None:
        """Return the temperature we try to reach."""
        if (target_temp := self.coordinator.data.state.target_temperature) is None:
            return None
        return target_temp.value

    @property
    def _hvac_mode_value(self) -> int | None:
        """Return the raw hvac_mode value from the coordinator."""
        if (hvac_mode := self.coordinator.data.state.hvac_mode) is None:
            return None
        return hvac_mode.value

    @property
    def hvac_mode(self) -> HVACMode | None:
        """Return hvac operation ie. heat, cool mode."""
        if (hvac_mode_value := self._hvac_mode_value) is None:
            return None
        return BSBLAN_TO_HA_HVAC_MODE.get(hvac_mode_value)

    @property
    def hvac_action(self) -> HVACAction | None:
        """Return the current running hvac action."""
        if (
            action := self.coordinator.data.state.hvac_action
        ) is None or action.value is None:
            return None
        category = get_hvac_action_category(action.value)
        return HVACAction(category.name.lower())

    @property
    def preset_mode(self) -> str | None:
        """Return the current preset mode."""
        # BSB-Lan mode 2 is eco/reduced mode
        if self._hvac_mode_value == 2: