Remove ozone state attribute and ozone sensors from Accuweather (#91492)

This commit is contained in:
Maciej Bieniek 2023-04-16 14:01:22 +02:00 committed by GitHub
parent 24538a44fc
commit 9d68cdca18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 40 deletions

View File

@ -10,9 +10,11 @@ from aiohttp import ClientSession
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
@ -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

View File

@ -103,14 +103,6 @@ FORECAST_SENSOR_TYPES: tuple[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",

View File

@ -109,16 +109,6 @@ class AccuWeatherEntity(
"""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."""

View File

@ -5,9 +5,11 @@ from unittest.mock import patch
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
@ -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

View File

@ -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,
@ -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"

View File

@ -14,7 +14,6 @@ from homeassistant.components.weather import (
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_OZONE,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_VISIBILITY,
@ -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
@ -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