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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AccuWeather sensors updates #52031

Merged
merged 5 commits into from Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 25 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,26 @@ 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]

async def async_added_to_hass(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default of CoordinatorEntity and shouldn't be needed?

"""When entity is added to HASS."""
self.async_on_remove(self.coordinator.async_add_listener(self._update_callback))

@callback
balloob marked this conversation as resolved.
Show resolved Hide resolved
def _update_callback(self) -> None:
balloob marked this conversation as resolved.
Show resolved Hide resolved
"""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 None:
if kind == "Precipitation":
return sensors["PrecipitationSummary"][kind]
return sensors[kind]
return sensors[ATTR_FORECAST][forecast_day][kind]
balloob marked this conversation as resolved.
Show resolved Hide resolved
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"