Fix AccuWeather sensors updates (#52031)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>pull/52042/head
parent
a127131c1b
commit
57106098f9
|
@ -12,7 +12,7 @@ from homeassistant.const import (
|
|||
CONF_NAME,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
@ -81,16 +81,11 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
|
|||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
self._sensor_data = _get_sensor_data(coordinator.data, forecast_day, kind)
|
||||
if forecast_day is None:
|
||||
self._description = SENSOR_TYPES[kind]
|
||||
self._sensor_data: dict[str, Any]
|
||||
if kind == "Precipitation":
|
||||
self._sensor_data = coordinator.data["PrecipitationSummary"][kind]
|
||||
else:
|
||||
self._sensor_data = coordinator.data[kind]
|
||||
else:
|
||||
self._description = FORECAST_SENSOR_TYPES[kind]
|
||||
self._sensor_data = coordinator.data[ATTR_FORECAST][forecast_day][kind]
|
||||
self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL
|
||||
self._name = name
|
||||
self.kind = kind
|
||||
|
@ -182,3 +177,24 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
|
|||
def entity_registry_enabled_default(self) -> bool:
|
||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
||||
return self._description[ATTR_ENABLED]
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle data update."""
|
||||
self._sensor_data = _get_sensor_data(
|
||||
self.coordinator.data, self.forecast_day, self.kind
|
||||
)
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
def _get_sensor_data(
|
||||
sensors: dict[str, Any], forecast_day: int | None, kind: str
|
||||
) -> Any:
|
||||
"""Get sensor data."""
|
||||
if forecast_day is not None:
|
||||
return sensors[ATTR_FORECAST][forecast_day][kind]
|
||||
|
||||
if kind == "Precipitation":
|
||||
return sensors["PrecipitationSummary"][kind]
|
||||
|
||||
return sensors[kind]
|
||||
|
|
|
@ -673,3 +673,36 @@ async def test_sensor_imperial_units(hass):
|
|||
assert state.attributes.get(ATTR_ATTRIBUTION) == ATTRIBUTION
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:weather-fog"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_FEET
|
||||
|
||||
|
||||
async def test_state_update(hass):
|
||||
"""Ensure the sensor state changes after updating the data."""
|
||||
await init_integration(hass)
|
||||
|
||||
state = hass.states.get("sensor.home_cloud_ceiling")
|
||||
assert state
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
assert state.state == "3200"
|
||||
|
||||
future = utcnow() + timedelta(minutes=60)
|
||||
|
||||
current_condition = json.loads(
|
||||
load_fixture("accuweather/current_conditions_data.json")
|
||||
)
|
||||
current_condition["Ceiling"]["Metric"]["Value"] = 3300
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.accuweather.AccuWeather.async_get_current_conditions",
|
||||
return_value=current_condition,
|
||||
), patch(
|
||||
"homeassistant.components.accuweather.AccuWeather.requests_remaining",
|
||||
new_callable=PropertyMock,
|
||||
return_value=10,
|
||||
):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.home_cloud_ceiling")
|
||||
assert state
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
assert state.state == "3300"
|
||||
|
|
Loading…
Reference in New Issue