Remove ozone
state attribute and ozone
sensors from Accuweather (#91492)
This commit is contained in:
parent
24538a44fc
commit
9d68cdca18
6 changed files with 30 additions and 40 deletions
|
@ -10,9 +10,11 @@ from aiohttp import ClientSession
|
||||||
from aiohttp.client_exceptions import ClientConnectorError
|
from aiohttp.client_exceptions import ClientConnectorError
|
||||||
from async_timeout import timeout
|
from async_timeout import timeout
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import DOMAIN as SENSOR_PLATFORM
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform
|
from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
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.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
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)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -103,14 +103,6 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = (
|
||||||
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
|
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
|
||||||
attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]},
|
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(
|
AccuWeatherSensorDescription(
|
||||||
key="Ragweed",
|
key="Ragweed",
|
||||||
icon="mdi:sprout",
|
icon="mdi:sprout",
|
||||||
|
|
|
@ -109,16 +109,6 @@ class AccuWeatherEntity(
|
||||||
"""Return the visibility."""
|
"""Return the visibility."""
|
||||||
return cast(float, self.coordinator.data["Visibility"][API_METRIC]["Value"])
|
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
|
@property
|
||||||
def forecast(self) -> list[Forecast] | None:
|
def forecast(self) -> list[Forecast] | None:
|
||||||
"""Return the forecast array."""
|
"""Return the forecast array."""
|
||||||
|
|
|
@ -5,9 +5,11 @@ from unittest.mock import patch
|
||||||
from accuweather import ApiError
|
from accuweather import ApiError
|
||||||
|
|
||||||
from homeassistant.components.accuweather.const import DOMAIN
|
from homeassistant.components.accuweather.const import DOMAIN
|
||||||
|
from homeassistant.components.sensor import DOMAIN as SENSOR_PLATFORM
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import STATE_UNAVAILABLE
|
from homeassistant.const import STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from . import init_integration
|
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_current.call_count == 1
|
||||||
assert mock_forecast.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
|
||||||
|
|
|
@ -305,13 +305,6 @@ async def test_sensor_enabled_without_forecast(hass: HomeAssistant) -> None:
|
||||||
suggested_object_id="home_mold_pollen_0d",
|
suggested_object_id="home_mold_pollen_0d",
|
||||||
disabled_by=None,
|
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(
|
registry.async_get_or_create(
|
||||||
SENSOR_DOMAIN,
|
SENSOR_DOMAIN,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -529,18 +522,6 @@ async def test_sensor_enabled_without_forecast(hass: HomeAssistant) -> None:
|
||||||
assert entry
|
assert entry
|
||||||
assert entry.unique_id == "0123456-mold-0"
|
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")
|
state = hass.states.get("sensor.home_ragweed_pollen_0d")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "0"
|
assert state.state == "0"
|
||||||
|
|
|
@ -14,7 +14,6 @@ from homeassistant.components.weather import (
|
||||||
ATTR_FORECAST_WIND_BEARING,
|
ATTR_FORECAST_WIND_BEARING,
|
||||||
ATTR_FORECAST_WIND_SPEED,
|
ATTR_FORECAST_WIND_SPEED,
|
||||||
ATTR_WEATHER_HUMIDITY,
|
ATTR_WEATHER_HUMIDITY,
|
||||||
ATTR_WEATHER_OZONE,
|
|
||||||
ATTR_WEATHER_PRESSURE,
|
ATTR_WEATHER_PRESSURE,
|
||||||
ATTR_WEATHER_TEMPERATURE,
|
ATTR_WEATHER_TEMPERATURE,
|
||||||
ATTR_WEATHER_VISIBILITY,
|
ATTR_WEATHER_VISIBILITY,
|
||||||
|
@ -46,7 +45,6 @@ async def test_weather_without_forecast(hass: HomeAssistant) -> None:
|
||||||
assert state.state == "sunny"
|
assert state.state == "sunny"
|
||||||
assert not state.attributes.get(ATTR_FORECAST)
|
assert not state.attributes.get(ATTR_FORECAST)
|
||||||
assert state.attributes.get(ATTR_WEATHER_HUMIDITY) == 67
|
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_PRESSURE) == 1012.0
|
||||||
assert state.attributes.get(ATTR_WEATHER_TEMPERATURE) == 22.6
|
assert state.attributes.get(ATTR_WEATHER_TEMPERATURE) == 22.6
|
||||||
assert state.attributes.get(ATTR_WEATHER_VISIBILITY) == 16.1
|
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
|
||||||
assert state.state == "sunny"
|
assert state.state == "sunny"
|
||||||
assert state.attributes.get(ATTR_WEATHER_HUMIDITY) == 67
|
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_PRESSURE) == 1012.0
|
||||||
assert state.attributes.get(ATTR_WEATHER_TEMPERATURE) == 22.6
|
assert state.attributes.get(ATTR_WEATHER_TEMPERATURE) == 22.6
|
||||||
assert state.attributes.get(ATTR_WEATHER_VISIBILITY) == 16.1
|
assert state.attributes.get(ATTR_WEATHER_VISIBILITY) == 16.1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue