Repository: home-assistant/core
File: homeassistant/components/template/weather.py (Python)

Code before:
        """Return a dict representation of the event data."""
        return asdict(self)

    @classmethod
    def from_dict(cls, restored: dict[str, Any]) -> Self | None:
        """Initialize a stored event state from a dict."""
        for key, vtypes in (
            ("last_apparent_temperature", (float, int)),
            ("last_cloud_coverage", (float, int)),
            ("last_dew_point", (float, int)),
            ("last_humidity", (float, int)),
            ("last_ozone", (float, int)),
            ("last_pressure", (float, int)),
            ("last_temperature", (float, int)),
            ("last_uv_index", (float, int)),
            ("last_visibility", (float, int)),
            ("last_wind_bearing", (float, int, str)),
            ("last_wind_gust_speed", (float, int)),
            ("last_wind_speed", (float, int)),
        ):
            if key not in restored or (
                (value := restored[key]) is not None and not isinstance(value, vtypes)
            ):
                return None

        return cls(
            last_apparent_temperature=restored["last_apparent_temperature"],
            last_cloud_coverage=restored["last_cloud_coverage"],
            last_dew_point=restored["last_dew_point"],
            last_humidity=restored["last_humidity"],
            last_ozone=restored["last_ozone"],
            last_pressure=restored["last_pressure"],
            last_temperature=restored["last_temperature"],
            last_uv_index=restored["last_uv_index"],
            last_visibility=restored["last_visibility"],
            last_wind_bearing=restored["last_wind_bearing"],
            last_wind_gust_speed=restored["last_wind_gust_speed"],
            last_wind_speed=restored["last_wind_speed"],
        )


class TriggerWeatherEntity(TriggerEntity, AbstractTemplateWeather, RestoreEntity):
    """Weather entity based on trigger data."""

    domain = WEATHER_DOMAIN
    extra_template_keys = (
        CONF_CONDITION,
        CONF_TEMPERATURE,
        CONF_HUMIDITY,
    )


Review comment (bug):
Please add a comment motivating why we need this check (maybe just say it's to guard against incorrect stored states due to previous bugs?)

Code after:
        """Return a dict representation of the event data."""
        return asdict(self)

    @classmethod
    def from_dict(cls, restored: dict[str, Any]) -> Self | None:
        """Initialize a stored event state from a dict."""
        for key, vtypes in (
            ("last_apparent_temperature", (float, int)),
            ("last_cloud_coverage", (float, int)),
            ("last_dew_point", (float, int)),
            ("last_humidity", (float, int)),
            ("last_ozone", (float, int)),
            ("last_pressure", (float, int)),
            ("last_temperature", (float, int)),
            ("last_uv_index", (float, int)),
            ("last_visibility", (float, int)),
            ("last_wind_bearing", (float, int, str)),
            ("last_wind_gust_speed", (float, int)),
            ("last_wind_speed", (float, int)),
        ):
            # This is needed to safeguard against previous restore data that has strings
            # instead of floats or ints.
            if key not in restored or (
                (value := restored[key]) is not None and not isinstance(value, vtypes)
            ):
                return None

        return cls(
            last_apparent_temperature=restored["last_apparent_temperature"],
            last_cloud_coverage=restored["last_cloud_coverage"],
            last_dew_point=restored["last_dew_point"],
            last_humidity=restored["last_humidity"],
            last_ozone=restored["last_ozone"],
            last_pressure=restored["last_pressure"],
            last_temperature=restored["last_temperature"],
            last_uv_index=restored["last_uv_index"],
            last_visibility=restored["last_visibility"],
            last_wind_bearing=restored["last_wind_bearing"],
            last_wind_gust_speed=restored["last_wind_gust_speed"],
            last_wind_speed=restored["last_wind_speed"],
        )


class TriggerWeatherEntity(TriggerEntity, AbstractTemplateWeather, RestoreEntity):
    """Weather entity based on trigger data."""

    domain = WEATHER_DOMAIN
    extra_template_keys = (
        CONF_CONDITION,
        CONF_TEMPERATURE,
        CONF_HUMIDITY,