Add select platform to Alpha Bidet Infrared (#187409)

This commit is contained in:
Evan Severson
2026-10-20 00:14:45 +02:00
committed by GitHub
parent 45b7337a3c
commit 6b2b4e56eb
7 changed files with 367 additions and 3 deletions
@@ -4,7 +4,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
PLATFORMS = [Platform.BUTTON]
PLATFORMS = [Platform.BUTTON, Platform.SELECT]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@@ -34,6 +34,14 @@
"water_dry_up": {
"default": "mdi:chevron-up"
}
},
"select": {
"seat_temperature": {
"default": "mdi:thermometer"
},
"water_temperature": {
"default": "mdi:thermometer-water"
}
}
}
}
@@ -89,11 +89,11 @@ rules:
entity-category:
status: exempt
comment: |
The buttons are the bidet's primary controls and do not need a category.
The buttons and selects are the bidet's primary controls and do not need a category.
entity-device-class:
status: exempt
comment: |
No button device class fits the bidet's controls.
No button or select device class fits the bidet's controls.
entity-disabled-by-default:
status: exempt
comment: |
@@ -0,0 +1,91 @@
"""Select platform for the Alpha Bidet Infrared integration."""
from dataclasses import dataclass
from typing import override
from infrared_protocols.codes.alpha_bidet.jx2 import AlphaBidetJX2LevelCode
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MODEL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from .const import AlphaBidetModel
from .entity import AlphaBidetIrEntity
PARALLEL_UPDATES = 1
# The remote sends the level itself rather than a step, so each option maps to one.
LEVELS: dict[str, int] = {"off": 0, "low": 1, "medium": 2, "high": 3}
@dataclass(frozen=True, kw_only=True)
class AlphaBidetIrSelectEntityDescription(SelectEntityDescription):
"""Describes an Alpha Bidet IR select entity."""
level_code: AlphaBidetJX2LevelCode
MODEL_SELECTS: dict[
AlphaBidetModel, tuple[AlphaBidetIrSelectEntityDescription, ...]
] = {
AlphaBidetModel.JX2: (
AlphaBidetIrSelectEntityDescription(
key="water_temperature",
translation_key="water_temperature",
options=list(LEVELS),
level_code=AlphaBidetJX2LevelCode.WATER_TEMP,
),
AlphaBidetIrSelectEntityDescription(
key="seat_temperature",
translation_key="seat_temperature",
options=list(LEVELS),
level_code=AlphaBidetJX2LevelCode.SEAT_TEMP,
),
),
}
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Alpha Bidet IR selects from a config entry."""
async_add_entities(
AlphaBidetIrSelect(entry, description)
for description in MODEL_SELECTS[AlphaBidetModel(entry.data[CONF_MODEL])]
)
class AlphaBidetIrSelect(AlphaBidetIrEntity, SelectEntity, RestoreEntity):
"""Alpha Bidet IR select entity for a setting sent as an absolute level."""
_attr_assumed_state = True
entity_description: AlphaBidetIrSelectEntityDescription
def __init__(
self, entry: ConfigEntry, description: AlphaBidetIrSelectEntityDescription
) -> None:
"""Initialize Alpha Bidet IR select."""
super().__init__(entry, unique_id_suffix=description.key)
self.entity_description = description
@override
async def async_added_to_hass(self) -> None:
"""Restore the assumed level, as infrared cannot read it back from the bidet."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state is not None and last_state.state in LEVELS:
self._attr_current_option = last_state.state
@override
async def async_select_option(self, option: str) -> None:
"""Send the chosen level."""
await self._send_command(
self.entity_description.level_code.to_command(LEVELS[option])
)
self._attr_current_option = option
self.async_write_ha_state()
@@ -54,6 +54,26 @@
"water_dry_up": {
"name": "Water/dry up"
}
},
"select": {
"seat_temperature": {
"name": "Seat temperature",
"state": {
"high": "[%key:common::state::high%]",
"low": "[%key:common::state::low%]",
"medium": "[%key:common::state::medium%]",
"off": "[%key:common::state::off%]"
}
},
"water_temperature": {
"name": "Water temperature",
"state": {
"high": "[%key:common::state::high%]",
"low": "[%key:common::state::low%]",
"medium": "[%key:common::state::medium%]",
"off": "[%key:common::state::off%]"
}
}
}
}
}
@@ -0,0 +1,129 @@
# serializer version: 1
# name: test_entities[select.alpha_bidet_jx2_seat_temperature-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
<SelectEntityCapabilityAttribute.OPTIONS: 'options'>: list([
'off',
'low',
'medium',
'high',
]),
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'select',
'entity_category': None,
'entity_id': 'select.alpha_bidet_jx2_seat_temperature',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Seat temperature',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Seat temperature',
'platform': 'alpha_bidet_infrared',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'seat_temperature',
'unique_id': '01JTEST0000000000000000000_seat_temperature',
'unit_of_measurement': None,
})
# ---
# name: test_entities[select.alpha_bidet_jx2_seat_temperature-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.ASSUMED_STATE: 'assumed_state'>: True,
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Alpha Bidet JX2 Seat temperature',
<SelectEntityCapabilityAttribute.OPTIONS: 'options'>: list([
'off',
'low',
'medium',
'high',
]),
}),
'context': <ANY>,
'entity_id': 'select.alpha_bidet_jx2_seat_temperature',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_entities[select.alpha_bidet_jx2_water_temperature-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
<SelectEntityCapabilityAttribute.OPTIONS: 'options'>: list([
'off',
'low',
'medium',
'high',
]),
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'select',
'entity_category': None,
'entity_id': 'select.alpha_bidet_jx2_water_temperature',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Water temperature',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Water temperature',
'platform': 'alpha_bidet_infrared',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'water_temperature',
'unique_id': '01JTEST0000000000000000000_water_temperature',
'unit_of_measurement': None,
})
# ---
# name: test_entities[select.alpha_bidet_jx2_water_temperature-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.ASSUMED_STATE: 'assumed_state'>: True,
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Alpha Bidet JX2 Water temperature',
<SelectEntityCapabilityAttribute.OPTIONS: 'options'>: list([
'off',
'low',
'medium',
'high',
]),
}),
'context': <ANY>,
'entity_id': 'select.alpha_bidet_jx2_water_temperature',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
@@ -0,0 +1,116 @@
"""Tests for the Alpha Bidet Infrared select platform."""
from unittest.mock import patch
from infrared_protocols.codes.alpha_bidet.jx2 import AlphaBidetJX2LevelCode
import pytest
from pytest_homeassistant.common import (
MockConfigEntry,
mock_restore_cache,
snapshot_platform,
)
from pytest_homeassistant.components.common import (
assert_availability_follows_source_entity,
)
from pytest_homeassistant.components.infrared.common import MockInfraredEmitterEntity
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.select import (
ATTR_OPTION,
DOMAIN as SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import entity_registry as er
from .conftest import MOCK_INFRARED_EMITTER_ENTITY_ID
WATER_TEMPERATURE_ENTITY_ID = "select.alpha_bidet_jx2_water_temperature"
SEAT_TEMPERATURE_ENTITY_ID = "select.alpha_bidet_jx2_seat_temperature"
@pytest.fixture
def platforms() -> list[Platform]:
"""Return platforms to set up."""
return [Platform.SELECT]
@pytest.mark.usefixtures("init_integration")
async def test_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the select entities are created with the correct attributes."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.parametrize(
("entity_id", "level_code"),
[
(WATER_TEMPERATURE_ENTITY_ID, AlphaBidetJX2LevelCode.WATER_TEMP),
(SEAT_TEMPERATURE_ENTITY_ID, AlphaBidetJX2LevelCode.SEAT_TEMP),
],
)
@pytest.mark.parametrize(
("option", "level"), [("off", 0), ("low", 1), ("medium", 2), ("high", 3)]
)
@pytest.mark.usefixtures("init_integration")
async def test_select_option_sends_absolute_level(
hass: HomeAssistant,
mock_infrared_emitter_entity: MockInfraredEmitterEntity,
entity_id: str,
level_code: AlphaBidetJX2LevelCode,
option: str,
level: int,
) -> None:
"""Test selecting a level sends that level, not a step from the current one."""
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: option},
blocking=True,
)
assert len(mock_infrared_emitter_entity.send_command_calls) == 1
timings = mock_infrared_emitter_entity.send_command_calls[0].get_raw_timings()
assert timings == level_code.to_command(level).get_raw_timings()
state = hass.states.get(entity_id)
assert state is not None
assert state.state == option
@pytest.mark.parametrize(
("restored_state", "expected_state"),
[("medium", "medium"), ("bogus", STATE_UNKNOWN)],
)
async def test_state_restored_on_restart(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_infrared_emitter_entity: MockInfraredEmitterEntity,
platforms: list[Platform],
restored_state: str,
expected_state: str,
) -> None:
"""Test the assumed level is restored after a restart, ignoring unknown ones."""
mock_restore_cache(hass, [State(WATER_TEMPERATURE_ENTITY_ID, restored_state)])
mock_config_entry.add_to_hass(hass)
with patch("homeassistant.components.alpha_bidet_infrared.PLATFORMS", platforms):
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(WATER_TEMPERATURE_ENTITY_ID)
assert state is not None
assert state.state == expected_state
@pytest.mark.usefixtures("init_integration")
async def test_select_availability_follows_ir_entity(hass: HomeAssistant) -> None:
"""Test a select becomes unavailable when the IR entity is unavailable."""
await assert_availability_follows_source_entity(
hass, WATER_TEMPERATURE_ENTITY_ID, [MOCK_INFRARED_EMITTER_ENTITY_ID]
)