Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Matter Number Inputs."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from chip.clusters import Objects as clusters
8 from matter_server.common import custom_clusters
9 from matter_server.common.helpers.util import create_attribute_path_from_attribute
10 
12  NumberDeviceClass,
13  NumberEntity,
14  NumberEntityDescription,
15  NumberMode,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import EntityCategory, Platform, UnitOfLength, UnitOfTime
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .entity import MatterEntity, MatterEntityDescription
23 from .helpers import get_matter
24 from .models import MatterDiscoverySchema
25 
26 
28  hass: HomeAssistant,
29  config_entry: ConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Set up Matter Number Input from Config Entry."""
33  matter = get_matter(hass)
34  matter.register_platform_handler(Platform.NUMBER, async_add_entities)
35 
36 
37 @dataclass(frozen=True)
39  """Describe Matter Number Input entities."""
40 
41 
43  """Representation of a Matter Attribute as a Number entity."""
44 
45  entity_description: MatterNumberEntityDescription
46 
47  async def async_set_native_value(self, value: float) -> None:
48  """Update the current value."""
49  matter_attribute = self._entity_info_entity_info.primary_attribute
50  sendvalue = int(value)
51  if value_convert := self.entity_descriptionentity_description.ha_to_native_value:
52  sendvalue = value_convert(value)
53  await self.matter_clientmatter_client.write_attribute(
54  node_id=self._endpoint_endpoint.node.node_id,
55  attribute_path=create_attribute_path_from_attribute(
56  self._endpoint_endpoint.endpoint_id,
57  matter_attribute,
58  ),
59  value=sendvalue,
60  )
61 
62  @callback
63  def _update_from_device(self) -> None:
64  """Update from device."""
65  value = self.get_matter_attribute_valueget_matter_attribute_value(self._entity_info_entity_info.primary_attribute)
66  if value_convert := self.entity_descriptionentity_description.measurement_to_ha:
67  value = value_convert(value)
68  self._attr_native_value_attr_native_value = value
69 
70 
71 # Discovery schema(s) to map Matter Attributes to HA entities
72 DISCOVERY_SCHEMAS = [
74  platform=Platform.NUMBER,
75  entity_description=MatterNumberEntityDescription(
76  key="on_level",
77  entity_category=EntityCategory.CONFIG,
78  translation_key="on_level",
79  native_max_value=255,
80  native_min_value=0,
81  mode=NumberMode.BOX,
82  # use 255 to indicate that the value should revert to the default
83  measurement_to_ha=lambda x: 255 if x is None else x,
84  ha_to_native_value=lambda x: None if x == 255 else int(x),
85  native_step=1,
86  native_unit_of_measurement=None,
87  ),
88  entity_class=MatterNumber,
89  required_attributes=(clusters.LevelControl.Attributes.OnLevel,),
90  ),
92  platform=Platform.NUMBER,
93  entity_description=MatterNumberEntityDescription(
94  key="on_transition_time",
95  entity_category=EntityCategory.CONFIG,
96  translation_key="on_transition_time",
97  native_max_value=65534,
98  native_min_value=0,
99  measurement_to_ha=lambda x: None if x is None else x / 10,
100  ha_to_native_value=lambda x: round(x * 10),
101  native_step=0.1,
102  native_unit_of_measurement=UnitOfTime.SECONDS,
103  mode=NumberMode.BOX,
104  ),
105  entity_class=MatterNumber,
106  required_attributes=(clusters.LevelControl.Attributes.OnTransitionTime,),
107  ),
109  platform=Platform.NUMBER,
110  entity_description=MatterNumberEntityDescription(
111  key="off_transition_time",
112  entity_category=EntityCategory.CONFIG,
113  translation_key="off_transition_time",
114  native_max_value=65534,
115  native_min_value=0,
116  measurement_to_ha=lambda x: None if x is None else x / 10,
117  ha_to_native_value=lambda x: round(x * 10),
118  native_step=0.1,
119  native_unit_of_measurement=UnitOfTime.SECONDS,
120  mode=NumberMode.BOX,
121  ),
122  entity_class=MatterNumber,
123  required_attributes=(clusters.LevelControl.Attributes.OffTransitionTime,),
124  ),
126  platform=Platform.NUMBER,
127  entity_description=MatterNumberEntityDescription(
128  key="on_off_transition_time",
129  entity_category=EntityCategory.CONFIG,
130  translation_key="on_off_transition_time",
131  native_max_value=65534,
132  native_min_value=0,
133  measurement_to_ha=lambda x: None if x is None else x / 10,
134  ha_to_native_value=lambda x: round(x * 10),
135  native_step=0.1,
136  native_unit_of_measurement=UnitOfTime.SECONDS,
137  mode=NumberMode.BOX,
138  ),
139  entity_class=MatterNumber,
140  required_attributes=(clusters.LevelControl.Attributes.OnOffTransitionTime,),
141  ),
143  platform=Platform.NUMBER,
144  entity_description=MatterNumberEntityDescription(
145  key="EveWeatherAltitude",
146  device_class=NumberDeviceClass.DISTANCE,
147  entity_category=EntityCategory.CONFIG,
148  translation_key="altitude",
149  native_max_value=9000,
150  native_min_value=0,
151  native_unit_of_measurement=UnitOfLength.METERS,
152  native_step=1,
153  mode=NumberMode.BOX,
154  ),
155  entity_class=MatterNumber,
156  required_attributes=(custom_clusters.EveCluster.Attributes.Altitude,),
157  ),
158 ]
Any get_matter_attribute_value(self, type[ClusterAttributeDescriptor] attribute, bool null_as_none=True)
Definition: entity.py:206
None async_set_native_value(self, float value)
Definition: number.py:47
MatterAdapter get_matter(HomeAssistant hass)
Definition: helpers.py:35
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: number.py:31