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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matter TemperatureLevel feature from TemperatureControl cluster #134532

Merged
merged 23 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions homeassistant/components/matter/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
}
}
},
"select": {
"temperature_level": {
"default": "mdi:thermometer"
}
},
"sensor": {
"contamination_state": {
"default": "mdi:air-filter"
Expand Down
72 changes: 66 additions & 6 deletions homeassistant/components/matter/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

from chip.clusters import Objects as clusters
from chip.clusters.ClusterObjects import ClusterAttributeDescriptor, ClusterCommand
from chip.clusters.Types import Nullable
from matter_server.common.helpers.util import create_attribute_path_from_attribute

Expand Down Expand Up @@ -47,7 +49,18 @@
"""Describe Matter select entities."""


class MatterSelectEntity(MatterEntity, SelectEntity):
@dataclass(frozen=True, kw_only=True)
class MatterListSelectEntityDescription(MatterSelectEntityDescription):
"""Describe Matter select entities for MatterListSelectEntity."""

# command: a callback to create the command to send to the device
# the callback's argument will be the index of the selected list value
command: Callable[[int], ClusterCommand]
# list attribute: the attribute descriptor to get the list of values (= list of strings)
list_attribute: type[ClusterAttributeDescriptor]


class MatterAttributeSelectEntity(MatterEntity, SelectEntity):
"""Representation of a select entity from Matter Attribute read/write."""

entity_description: MatterSelectEntityDescription
Expand Down Expand Up @@ -76,7 +89,7 @@
self._attr_current_option = value_convert(value)


class MatterModeSelectEntity(MatterSelectEntity):
class MatterModeSelectEntity(MatterAttributeSelectEntity):
"""Representation of a select entity from Matter (Mode) Cluster attribute(s)."""

async def async_select_option(self, option: str) -> None:
Expand Down Expand Up @@ -111,6 +124,37 @@
self._attr_name = desc


class MatterListSelectEntity(MatterEntity, SelectEntity):
"""Representation of a select entity from Matter list and selected item Cluster attribute(s)."""

entity_description: MatterListSelectEntityDescription

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
option_id = self._attr_options.index(option)
await self.matter_client.send_device_command(
node_id=self._endpoint.node.node_id,
endpoint_id=self._endpoint.endpoint_id,
command=self.entity_description.command(option_id),
)

@callback
def _update_from_device(self) -> None:
"""Update from device."""
list_values = cast(
list[str],
self.get_matter_attribute_value(self.entity_description.list_attribute),
)
self._attr_options = list_values
current_option_idx: int | None = self.get_matter_attribute_value(
self._entity_info.primary_attribute
)
if current_option_idx is not None:
self._attr_current_option = list_values[current_option_idx]
else:
self._attr_current_option = None

Check warning on line 155 in homeassistant/components/matter/select.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/matter/select.py#L155

Added line #L155 was not covered by tests


# Discovery schema(s) to map Matter Attributes to HA entities
DISCOVERY_SCHEMAS = [
MatterDiscoverySchema(
Expand Down Expand Up @@ -230,7 +274,7 @@
"previous": None,
}.get,
),
entity_class=MatterSelectEntity,
entity_class=MatterAttributeSelectEntity,
required_attributes=(clusters.OnOff.Attributes.StartUpOnOff,),
),
MatterDiscoverySchema(
Expand All @@ -251,7 +295,7 @@
"low": 2,
}.get,
),
entity_class=MatterSelectEntity,
entity_class=MatterAttributeSelectEntity,
required_attributes=(clusters.SmokeCoAlarm.Attributes.SmokeSensitivityLevel,),
),
MatterDiscoverySchema(
Expand All @@ -270,9 +314,25 @@
"Fahrenheit": 1,
}.get,
),
entity_class=MatterSelectEntity,
entity_class=MatterAttributeSelectEntity,
required_attributes=(
clusters.ThermostatUserInterfaceConfiguration.Attributes.TemperatureDisplayMode,
),
),
MatterDiscoverySchema(
platform=Platform.SELECT,
entity_description=MatterListSelectEntityDescription(
key="TemperatureControlSelectedTemperatureLevel",
translation_key="temperature_level",
command=lambda selected_index: clusters.TemperatureControl.Commands.SetTemperature(
targetTemperatureLevel=selected_index
),
list_attribute=clusters.TemperatureControl.Attributes.SupportedTemperatureLevels,
),
entity_class=MatterListSelectEntity,
required_attributes=(
clusters.TemperatureControl.Attributes.SelectedTemperatureLevel,
clusters.TemperatureControl.Attributes.SupportedTemperatureLevels,
),
),
]
3 changes: 3 additions & 0 deletions homeassistant/components/matter/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@
"previous": "Previous"
}
},
"temperature_level": {
"name": "Temperature level"
},
"temperature_display_mode": {
"name": "Temperature display mode"
}
Expand Down
1 change: 1 addition & 0 deletions tests/components/matter/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ async def integration_fixture(
"pressure_sensor",
"room_airconditioner",
"silabs_dishwasher",
"silabs_laundrywasher",
"smoke_detector",
"switch_unit",
"temperature_sensor",
Expand Down
Loading
Loading