Skip to content

Commit

Permalink
Render select entity unavailable when active feature is missing in Se…
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored Jan 8, 2025
1 parent 2704090 commit c5f80dd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
19 changes: 7 additions & 12 deletions homeassistant/components/sensibo/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
SelectEntityDescription,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import (
Expand Down Expand Up @@ -137,6 +136,13 @@ def __init__(
self.entity_description = entity_description
self._attr_unique_id = f"{device_id}-{entity_description.key}"

@property
def available(self) -> bool:
"""Return True if entity is available."""
if self.entity_description.key not in self.device_data.active_features:
return False
return super().available

@property
def current_option(self) -> str | None:
"""Return the current selected option."""
Expand All @@ -152,17 +158,6 @@ def options(self) -> list[str]:

async def async_select_option(self, option: str) -> None:
"""Set state to the selected option."""
if self.entity_description.key not in self.device_data.active_features:
hvac_mode = self.device_data.hvac_mode if self.device_data.hvac_mode else ""
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="select_option_not_available",
translation_placeholders={
"hvac_mode": hvac_mode,
"key": self.entity_description.key,
},
)

await self.async_send_api_call(
key=self.entity_description.data_key,
value=option,
Expand Down
3 changes: 0 additions & 3 deletions homeassistant/components/sensibo/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,6 @@
"service_raised": {
"message": "Could not perform action for {name} with error {error}"
},
"select_option_not_available": {
"message": "Current mode {hvac_mode} doesn't support setting {key}"
},
"climate_react_not_available": {
"message": "Use Sensibo Enable Climate React action once to enable switch or the Sensibo app"
},
Expand Down
23 changes: 18 additions & 5 deletions tests/components/sensibo/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from homeassistant.components.sensibo.const import DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er, issue_registry as ir
Expand Down Expand Up @@ -63,7 +63,7 @@ async def test_select_set_option(
"""Test the Sensibo select service."""

mock_client.async_get_devices_data.return_value.parsed[
"ABC999111"
"AAZZAAZZ"
].active_features = [
"timestamp",
"on",
Expand Down Expand Up @@ -97,13 +97,11 @@ async def test_select_set_option(
assert state.state == "on"

mock_client.async_get_devices_data.return_value.parsed[
"ABC999111"
"AAZZAAZZ"
].active_features = [
"timestamp",
"on",
"mode",
"targetTemperature",
"horizontalSwing",
"light",
]

Expand Down Expand Up @@ -142,6 +140,21 @@ async def test_select_set_option(
state = hass.states.get("select.kitchen_light")
assert state.state == "dim"

mock_client.async_get_devices_data.return_value.parsed[
"AAZZAAZZ"
].active_features = [
"timestamp",
"on",
"mode",
]

freezer.tick(timedelta(minutes=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()

state = hass.states.get("select.kitchen_light")
assert state.state == STATE_UNAVAILABLE


@pytest.mark.parametrize(
"load_platforms",
Expand Down

0 comments on commit c5f80dd

Please sign in to comment.