Handle availability in Idasen Desk height sensor (#120277)

pull/120495/head
Abílio Costa 2024-06-26 08:02:49 +01:00 committed by GitHub
parent 9f4bf6f11a
commit 8ce53d28e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 2 deletions

View File

@ -66,7 +66,7 @@ class IdasenDeskCover(CoordinatorEntity[IdasenDeskCoordinator], CoverEntity):
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._desk.is_connected is True
return super().available and self._desk.is_connected is True
@property
def is_closed(self) -> bool:

View File

@ -79,12 +79,18 @@ class IdasenDeskSensor(CoordinatorEntity[IdasenDeskCoordinator], SensorEntity):
self._attr_unique_id = f"{description.key}-{address}"
self._attr_device_info = device_info
self._address = address
self._desk = coordinator.desk
async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self._update_native_value()
@property
def available(self) -> bool:
"""Return True if entity is available."""
return super().available and self._desk.is_connected is True
@callback
def _handle_coordinator_update(self, *args: Any) -> None:
"""Handle data update."""

View File

@ -4,10 +4,13 @@ from unittest.mock import MagicMock
import pytest
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from . import init_integration
EXPECTED_INITIAL_HEIGHT = "1"
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_height_sensor(hass: HomeAssistant, mock_desk_api: MagicMock) -> None:
@ -17,7 +20,7 @@ async def test_height_sensor(hass: HomeAssistant, mock_desk_api: MagicMock) -> N
entity_id = "sensor.test_height"
state = hass.states.get(entity_id)
assert state
assert state.state == "1"
assert state.state == EXPECTED_INITIAL_HEIGHT
mock_desk_api.height = 1.2
mock_desk_api.trigger_update_callback(None)
@ -25,3 +28,24 @@ async def test_height_sensor(hass: HomeAssistant, mock_desk_api: MagicMock) -> N
state = hass.states.get(entity_id)
assert state
assert state.state == "1.2"
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensor_available(
hass: HomeAssistant,
mock_desk_api: MagicMock,
) -> None:
"""Test sensor available property."""
await init_integration(hass)
entity_id = "sensor.test_height"
state = hass.states.get(entity_id)
assert state
assert state.state == EXPECTED_INITIAL_HEIGHT
mock_desk_api.is_connected = False
mock_desk_api.trigger_update_callback(None)
state = hass.states.get(entity_id)
assert state
assert state.state == STATE_UNAVAILABLE