home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.06k stars 29.73k forks source link

AirNow integration incorrectly assumes all responses have PM2.5 data #116074

Open saschmit opened 4 months ago

saschmit commented 4 months ago

The problem

I've been getting this error in the logs:

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:580
integration: Sensor ([documentation](https://www.home-assistant.io/integrations/sensor), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+sensor%22))
First occurred: 16:52:11 (1 occurrences)
Last logged: 16:52:11
Error adding entity sensor.airnow_air_quality_index for domain sensor with platform airnow

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 580, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 890, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1334, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 998, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1119, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1059, in __async_calculate_state
    attr.update(self.extra_state_attributes or {})
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/airnow/sensor.py", line 166, in extra_state_attributes
    return self.entity_description.extra_state_attributes_fn(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/airnow/sensor.py", line 84, in <lambda>
    f"{data[ATTR_API_REPORT_DATE]} {data[ATTR_API_REPORT_HOUR]}",
       ~~~~^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'DateObserved'

Digging into the Python code, this is because the code assumes that one of the observations will have PM2.5 data, but my response only has O3 data, so the date information, while present in the original response, doesn't get copied over.

The response JSON (redacted):

[
    {
        "DateObserved": "2024-04-23",
        "HourObserved": 20,
        "LocalTimeZone": "EST",
        "ReportingArea": "*redacted*",
        "StateCode": "*redacted*",
        "Latitude": 1.0,
        "Longitude": -1.0,
        "ParameterName": "O3",
        "AQI": 45,
        "Category": {
            "Number": 1,
            "Name": "Good"
        }
    }
]

What version of Home Assistant Core has the issue?

core-2024.4.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

AirNow

Link to integration documentation on our website

https://www.home-assistant.io/integrations/airnow

Diagnostics information

{ "home_assistant": { "installation_type": "Home Assistant OS", "version": "2024.4.4", "dev": false, "hassio": true, "virtualenv": false, "python_version": "3.12.2", "docker": true, "arch": "aarch64", "timezone": "America/New_York", "os_name": "Linux", "os_version": "6.6.25-haos", "supervisor": "2024.04.0", "host_os": "Home Assistant OS 12.2", "docker_version": "25.0.5", "chassis": "embedded", "run_as_root": true }, "custom_components": {}, "integration_manifest": { "domain": "airnow", "name": "AirNow", "codeowners": [ "@asymworks" ], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/airnow", "iot_class": "cloud_polling", "loggers": [ "pyairnow" ], "requirements": [ "pyairnow==1.2.1" ], "is_built_in": true }, "data": { "entry": { "entry_id": "33df49fccd4bbc3f7e7429e10a30c55a", "version": 2, "minor_version": 1, "domain": "airnow", "title": "REDACTED", "data": { "api_key": "REDACTED", "latitude": "REDACTED", "longitude": "REDACTED" }, "options": { "radius": 150 }, "pref_disable_new_entities": false, "pref_disable_polling": false, "source": "user", "unique_id": "REDACTED", "disabled_by": null }, "data": { "O3": 0.049, "AQI": 45, "Category.Number": 1, "Category.Name": "Good", "Pollutant": "O3" } }

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 4 months ago

Hey there @asymworks, mind taking a look at this issue as it has been labeled with an integration (airnow) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `airnow` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign airnow` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


airnow documentation airnow source (message by IssueLinks)

home-assistant[bot] commented 4 months ago

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `sensor` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign sensor` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


sensor documentation sensor source (message by IssueLinks)

issue-triage-workflows[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

saschmit commented 1 month ago

Sorry, I'm still seeing this in the latest version.

kd7gab commented 3 weeks ago

I am seeing the same issue and error logged in my instance of HA, too.

monsieurlatte commented 1 week ago

I am as well, seems the codebase owner hasn't been able to fix this yet :(