Skip to content

fix: decode observations that omit solar_radiation (KeyError)#129

Open
brentb2529 wants to merge 1 commit into
jeeftor:masterfrom
brentb2529:fix/optional-solar-radiation
Open

fix: decode observations that omit solar_radiation (KeyError)#129
brentb2529 wants to merge 1 commit into
jeeftor:masterfrom
brentb2529:fix/optional-solar-radiation

Conversation

@brentb2529

Copy link
Copy Markdown

Problem

KeyError: 'solar_radiation'Observation decode fails for stations that don't report solar radiation.

Some WeatherFlow/Tempest stations never report solar_radiation: it's absent from both obs[0] and outdoor_keys. Observation declares solar_radiation (along with brightness, dew_point, heat_index, pressure_trend, relative_humidity, uv) as a required field with no default. dataclasses_json decodes via a bare kvs[field.name] lookup, so a missing key raises KeyError and the entire observation is discarded. Downstream consumers (HA custom_components/tempest, core weatherflow_cloud) then go stale / unavailable.

This is the same bug class previously fixed for air_temperature (#128) and reported for brightness (home-assistant/core#156969) and pressure_trend.

Observed in HA as:

weatherflow4py.rest: Unable to convert data || {...} || to ||
  <class 'weatherflow4py.models.rest.observation.ObservationStationREST'> -- 'solar_radiation'

Fix

Make the sensor-derived observation fields optional with a None default, so a missing key decodes to None instead of raising. With dataclasses_json it's the default value (not just the | None hint) that makes the decoder skip the dict lookup.

  • brightness, dew_point, heat_index, pressure_trend, relative_humidity, solar_radiation, uv... | None = None
  • timestamp remains the only required field (present in every observation) and is moved first to satisfy dataclass field-ordering (a no-default field can't follow a defaulted one).
  • uv_index_color / uv_index_exposure now guard against a None uv and return None.

No version bump — left to maintainer per the existing "bump version" commit convention.

Tests

  • New regression test test_convert_json_to_observation_no_solar_radiation + fixture station_observation_no_solar_radiation.json (verbatim real payload from station 113968, which omits solar_radiation from both obs[0] and outdoor_keys).
  • Full suite green locally: 98 passed, ruff check clean.

Closes the solar_radiation case of the recurring missing-field decode bug.

🤖 Generated with Claude Code

…ields

Some WeatherFlow/Tempest stations never report solar_radiation (it is
absent from both obs[0] and outdoor_keys). dataclasses_json decodes via a
bare kvs[field.name] lookup, so a required field with no default raises
KeyError on a missing key and the entire observation is discarded -- the
station's consumers (HA tempest / weatherflow_cloud) go stale.

Make the sensor-derived observation fields optional with a None default so
a missing key decodes to None instead of raising. With dataclasses_json the
default value (not just the Optional hint) is what skips the dict lookup.
timestamp remains the sole required field and is moved first to satisfy
dataclass field-ordering. uv_index_color/uv_index_exposure now guard
against a None uv.

Same bug class as jeeftor#128 (air_temperature) and #156969 (brightness).

Adds a regression test + fixture (real station 113968 payload) covering an
observation with no solar_radiation.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant