Skip to content

Commit

Permalink
Fix AccuWeather sensors updates (#52031)
Browse files Browse the repository at this point in the history
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
  • Loading branch information
bieniu and balloob committed Jun 20, 2021
1 parent a127131 commit 5710609
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
30 changes: 23 additions & 7 deletions homeassistant/components/accuweather/sensor.py
Expand Up @@ -12,7 +12,7 @@
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
Expand Down Expand Up @@ -81,16 +81,11 @@ def __init__(
) -> 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
Expand Down Expand Up @@ -182,3 +177,24 @@ def extra_state_attributes(self) -> dict[str, Any]:
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]
33 changes: 33 additions & 0 deletions tests/components/accuweather/test_sensor.py
Expand Up @@ -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"

0 comments on commit 5710609

Please sign in to comment.