Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ozone state attribute and ozone sensors from Accuweather #91492

Merged
merged 5 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions homeassistant/components/accuweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from aiohttp.client_exceptions import ClientConnectorError
from async_timeout import timeout

from homeassistant.components.sensor import DOMAIN as SENSOR_PLATFORM
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -48,6 +50,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

# Remove ozone sensors from registry if they exist
ent_reg = er.async_get(hass)
for day in range(0, 5):
unique_id = f"{coordinator.location_key}-ozone-{day}"
if entity_id := ent_reg.async_get_entity_id(SENSOR_PLATFORM, DOMAIN, unique_id):
_LOGGER.debug("Removing ozone sensor entity %s", entity_id)
ent_reg.async_remove(entity_id)

return True


Expand Down
8 changes: 0 additions & 8 deletions homeassistant/components/accuweather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ class AccuWeatherSensorDescription(
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]},
),
AccuWeatherSensorDescription(
key="Ozone",
icon="mdi:vector-triangle",
name="Ozone",
entity_registry_enabled_default=False,
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]},
),
AccuWeatherSensorDescription(
key="Ragweed",
icon="mdi:sprout",
Expand Down
10 changes: 0 additions & 10 deletions homeassistant/components/accuweather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ def native_visibility(self) -> float:
"""Return the visibility."""
return cast(float, self.coordinator.data["Visibility"][API_METRIC]["Value"])

@property
def ozone(self) -> int | None:
"""Return the ozone level."""
# We only have ozone data for certain locations and only in the forecast data.
if self.coordinator.forecast and self.coordinator.data[ATTR_FORECAST][0].get(
"Ozone"
):
return cast(int, self.coordinator.data[ATTR_FORECAST][0]["Ozone"]["Value"])
return None

@property
def forecast(self) -> list[Forecast] | None:
"""Return the forecast array."""
Expand Down
20 changes: 20 additions & 0 deletions tests/components/accuweather/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from accuweather import ApiError

from homeassistant.components.accuweather.const import DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_PLATFORM
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow

from . import init_integration
Expand Down Expand Up @@ -113,3 +115,21 @@ async def test_update_interval_forecast(hass: HomeAssistant) -> None:

assert mock_current.call_count == 1
assert mock_forecast.call_count == 1


async def test_remove_ozone_sensors(hass: HomeAssistant) -> None:
"""Test remove ozone sensors from registry."""
registry = er.async_get(hass)

registry.async_get_or_create(
SENSOR_PLATFORM,
DOMAIN,
"0123456-ozone-0",
suggested_object_id="home_ozone_0d",
disabled_by=None,
)

await init_integration(hass)

entry = registry.async_get("sensor.home_ozone_0d")
assert entry is None
19 changes: 0 additions & 19 deletions tests/components/accuweather/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,6 @@ async def test_sensor_enabled_without_forecast(hass: HomeAssistant) -> None:
suggested_object_id="home_mold_pollen_0d",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"0123456-ozone-0",
suggested_object_id="home_ozone_0d",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
Expand Down Expand Up @@ -529,18 +522,6 @@ async def test_sensor_enabled_without_forecast(hass: HomeAssistant) -> None:
assert entry
assert entry.unique_id == "0123456-mold-0"

state = hass.states.get("sensor.home_ozone_0d")
assert state
assert state.state == "32"
assert state.attributes.get(ATTR_ATTRIBUTION) == ATTRIBUTION
assert state.attributes.get("level") == "Good"
assert state.attributes.get(ATTR_ICON) == "mdi:vector-triangle"
assert state.attributes.get(ATTR_STATE_CLASS) is None

entry = registry.async_get("sensor.home_ozone_0d")
assert entry
assert entry.unique_id == "0123456-ozone-0"

state = hass.states.get("sensor.home_ragweed_pollen_0d")
assert state
assert state.state == "0"
Expand Down
3 changes: 0 additions & 3 deletions tests/components/accuweather/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_OZONE,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_VISIBILITY,
Expand Down Expand Up @@ -46,7 +45,6 @@ async def test_weather_without_forecast(hass: HomeAssistant) -> None:
assert state.state == "sunny"
assert not state.attributes.get(ATTR_FORECAST)
assert state.attributes.get(ATTR_WEATHER_HUMIDITY) == 67
assert not state.attributes.get(ATTR_WEATHER_OZONE)
assert state.attributes.get(ATTR_WEATHER_PRESSURE) == 1012.0
assert state.attributes.get(ATTR_WEATHER_TEMPERATURE) == 22.6
assert state.attributes.get(ATTR_WEATHER_VISIBILITY) == 16.1
Expand All @@ -68,7 +66,6 @@ async def test_weather_with_forecast(hass: HomeAssistant) -> None:
assert state
assert state.state == "sunny"
assert state.attributes.get(ATTR_WEATHER_HUMIDITY) == 67
assert state.attributes.get(ATTR_WEATHER_OZONE) == 32
assert state.attributes.get(ATTR_WEATHER_PRESSURE) == 1012.0
assert state.attributes.get(ATTR_WEATHER_TEMPERATURE) == 22.6
assert state.attributes.get(ATTR_WEATHER_VISIBILITY) == 16.1
Expand Down