Skip to content

Commit

Permalink
Fix unreachable Netatmo sensor returning false values (home-assistant…
Browse files Browse the repository at this point in the history
…#105954)

* Fix unreachable sensor returning false values

* Clean up unnecessary code
  • Loading branch information
cgtobi authored Dec 18, 2023
1 parent 94d22c9 commit 57a6eff
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/netatmo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"integration_type": "hub",
"iot_class": "cloud_polling",
"loggers": ["pyatmo"],
"requirements": ["pyatmo==7.6.0"]
"requirements": ["pyatmo==8.0.0"]
}
22 changes: 13 additions & 9 deletions homeassistant/components/netatmo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,16 @@ def __init__(
}
)

@property
def available(self) -> bool:
"""Return entity availability."""
return self.state is not None

@callback
def async_update_callback(self) -> None:
"""Update the entity's state."""
if (
state := getattr(self._module, self.entity_description.netatmo_name)
) is None:
not self._module.reachable
or (state := getattr(self._module, self.entity_description.netatmo_name))
is None
):
if self.available:
self._attr_available = False
return

if self.entity_description.netatmo_name in {
Expand All @@ -475,6 +474,7 @@ def async_update_callback(self) -> None:
else:
self._attr_native_value = state

self._attr_available = True
self.async_write_ha_state()


Expand Down Expand Up @@ -519,7 +519,6 @@ def async_update_callback(self) -> None:
if not self._module.reachable:
if self.available:
self._attr_available = False
self._attr_native_value = None
return

self._attr_available = True
Expand Down Expand Up @@ -565,9 +564,15 @@ def __init__(
@callback
def async_update_callback(self) -> None:
"""Update the entity's state."""
if not self._module.reachable:
if self.available:
self._attr_available = False
return

if (state := getattr(self._module, self.entity_description.key)) is None:
return

self._attr_available = True
self._attr_native_value = state

self.async_write_ha_state()
Expand Down Expand Up @@ -777,7 +782,6 @@ def async_update_callback(self) -> None:
self.entity_description.key,
self._area_name,
)
self._attr_native_value = None

self._attr_available = False
return
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ pyasuswrt==0.1.21
pyatag==0.3.5.3

# homeassistant.components.netatmo
pyatmo==7.6.0
pyatmo==8.0.0

# homeassistant.components.apple_tv
pyatv==0.14.3
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ pyasuswrt==0.1.21
pyatag==0.3.5.3

# homeassistant.components.netatmo
pyatmo==7.6.0
pyatmo==8.0.0

# homeassistant.components.apple_tv
pyatv==0.14.3
Expand Down
14 changes: 2 additions & 12 deletions tests/components/netatmo/fixtures/getstationsdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,22 +475,12 @@
"last_setup": 1558709954,
"data_type": ["Temperature", "Humidity"],
"battery_percent": 27,
"reachable": true,
"reachable": false,
"firmware": 50,
"last_message": 1644582699,
"last_seen": 1644582699,
"rf_status": 68,
"battery_vp": 4678,
"dashboard_data": {
"time_utc": 1644582648,
"Temperature": 9.4,
"Humidity": 57,
"min_temp": 6.7,
"max_temp": 9.8,
"date_max_temp": 1644534223,
"date_min_temp": 1644569369,
"temp_trend": "up"
}
"battery_vp": 4678
},
{
"_id": "12:34:56:80:c1:ea",
Expand Down
6 changes: 4 additions & 2 deletions tests/components/netatmo/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -561,26 +561,28 @@
'access_doorbell',
'access_presence',
'read_bubendorff',
'read_bfi',
'read_camera',
'read_carbonmonoxidedetector',
'read_doorbell',
'read_homecoach',
'read_magellan',
'read_mhs1',
'read_mx',
'read_presence',
'read_smarther',
'read_smokedetector',
'read_station',
'read_thermostat',
'read_mhs1',
'write_bubendorff',
'write_bfi',
'write_camera',
'write_magellan',
'write_mhs1',
'write_mx',
'write_presence',
'write_smarther',
'write_thermostat',
'write_mhs1',
]),
'type': 'Bearer',
}),
Expand Down
16 changes: 14 additions & 2 deletions tests/components/netatmo/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from .common import TEST_TIME, selected_platforms


async def test_weather_sensor(hass: HomeAssistant, config_entry, netatmo_auth) -> None:
"""Test weather sensor setup."""
async def test_indoor_sensor(hass: HomeAssistant, config_entry, netatmo_auth) -> None:
"""Test indoor sensor setup."""
with patch("time.time", return_value=TEST_TIME), selected_platforms(["sensor"]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

Expand All @@ -25,6 +25,18 @@ async def test_weather_sensor(hass: HomeAssistant, config_entry, netatmo_auth) -
assert hass.states.get(f"{prefix}pressure").state == "1014.5"


async def test_weather_sensor(hass: HomeAssistant, config_entry, netatmo_auth) -> None:
"""Test weather sensor unreachable."""
with patch("time.time", return_value=TEST_TIME), selected_platforms(["sensor"]):
assert await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()

prefix = "sensor.villa_outdoor_"

assert hass.states.get(f"{prefix}temperature").state == "unavailable"


async def test_public_weather_sensor(
hass: HomeAssistant, config_entry, netatmo_auth
) -> None:
Expand Down

0 comments on commit 57a6eff

Please sign in to comment.