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

Code before:
        self._attr_name = nasweb_thermostat.name
        self._attr_target_temperature_step = 1.0
        self._attr_min_temp = -50
        self._attr_max_temp = 50
        self._attr_has_entity_name = True
        self._attr_should_poll = False
        self._attr_unique_id = f"{DOMAIN}.{self._thermostat.webio_serial}.thermostat"
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, self._thermostat.webio_serial)}
        )

    async def async_added_to_hass(self) -> None:
        """When entity is added to hass."""
        await super().async_added_to_hass()
        self._handle_coordinator_update()

    def _set_attr_available(
        self, entity_last_update: float, available: bool | None
    ) -> None:
        if (
            self.coordinator.last_update is None
            or time.time() - entity_last_update >= STATUS_UPDATE_MAX_TIME_INTERVAL
        ):
            self._attr_available = False
        else:
            self._attr_available = available if available is not None else False

    @callback
    def _handle_coordinator_update(self) -> None:
        """Handle updated data from the coordinator."""
        self._attr_current_temperature = self._thermostat.current_temp
        self._attr_target_temperature_low = self._thermostat.temp_target_min
        self._attr_target_temperature_high = self._thermostat.temp_target_max
        self._attr_hvac_mode = self._get_current_hvac_mode()
        self._attr_hvac_action = self._get_current_action()
        self._attr_name = self._thermostat.name if self._thermostat.name else None
        self._set_attr_available(
            self._thermostat.last_update, self._thermostat.available
        )
        self.async_write_ha_state()

    def _get_current_hvac_mode(self) -> HVACMode:
        have_cooling = self._thermostat.enabled_above_output
        have_heating = self._thermostat.enabled_below_output
        if have_cooling and have_heating:
            return HVACMode.HEAT_COOL
        if have_cooling:
            return HVACMode.COOL
        if have_heating:
            return HVACMode.HEAT
        if self._thermostat.enabled_inrange_output:

Review comment (suggestion):
This could be moved to a new class `NaswebEntity`, it's the same for all nasweb entities. OK to do that in a separate PR.

Code after:
    ) -> None:
        """Initialize Thermostat."""
        super().__init__(coordinator)
        self._thermostat = nasweb_thermostat
        self._attr_available = False
        self._attr_name = nasweb_thermostat.name
        self._attr_unique_id = f"{DOMAIN}.{self._thermostat.webio_serial}.thermostat"
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, self._thermostat.webio_serial)}
        )

    async def async_added_to_hass(self) -> None:
        """When entity is added to hass."""
        await super().async_added_to_hass()
        self._handle_coordinator_update()

    def _set_attr_available(
        self, entity_last_update: float, available: bool | None
    ) -> None:
        if (
            self.coordinator.last_update is None
            or time.time() - entity_last_update >= STATUS_UPDATE_MAX_TIME_INTERVAL
        ):
            self._attr_available = False
        else:
            self._attr_available = available if available is not None else False

    @callback
    def _handle_coordinator_update(self) -> None:
        """Handle updated data from the coordinator."""
        self._attr_current_temperature = self._thermostat.current_temp
        self._attr_target_temperature_low = self._thermostat.temp_target_min
        self._attr_target_temperature_high = self._thermostat.temp_target_max
        self._attr_hvac_mode = self._get_current_hvac_mode()
        self._attr_hvac_action = self._get_current_action()
        self._attr_name = self._thermostat.name if self._thermostat.name else None
        self._set_attr_available(
            self._thermostat.last_update, self._thermostat.available
        )
        self.async_write_ha_state()

    def _get_current_hvac_mode(self) -> HVACMode:
        have_cooling = self._thermostat.enabled_above_output
        have_heating = self._thermostat.enabled_below_output
        if have_cooling and have_heating:
            return HVACMode.HEAT_COOL
        if have_cooling:
            return HVACMode.COOL
        if have_heating:
            return HVACMode.HEAT
        if self._thermostat.enabled_inrange_output: