Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for IronOS integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from enum import StrEnum
8 
9 from pynecil import LiveDataResponse, OperatingMode, PowerSource
10 
12  EntityCategory,
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16  SensorStateClass,
17 )
18 from homeassistant.const import (
19  PERCENTAGE,
20  UnitOfElectricPotential,
21  UnitOfPower,
22  UnitOfTemperature,
23  UnitOfTime,
24 )
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.typing import StateType
28 
29 from . import IronOSConfigEntry
30 from .const import OHM
31 from .entity import IronOSBaseEntity
32 
33 
34 class PinecilSensor(StrEnum):
35  """Pinecil Sensors."""
36 
37  LIVE_TEMP = "live_temperature"
38  SETPOINT_TEMP = "setpoint_temperature"
39  DC_VOLTAGE = "voltage"
40  HANDLETEMP = "handle_temperature"
41  PWMLEVEL = "power_pwm_level"
42  POWER_SRC = "power_source"
43  TIP_RESISTANCE = "tip_resistance"
44  UPTIME = "uptime"
45  MOVEMENT_TIME = "movement_time"
46  MAX_TIP_TEMP_ABILITY = "max_tip_temp_ability"
47  TIP_VOLTAGE = "tip_voltage"
48  HALL_SENSOR = "hall_sensor"
49  OPERATING_MODE = "operating_mode"
50  ESTIMATED_POWER = "estimated_power"
51 
52 
53 @dataclass(frozen=True, kw_only=True)
55  """IronOS sensor entity descriptions."""
56 
57  value_fn: Callable[[LiveDataResponse], StateType]
58 
59 
60 PINECIL_SENSOR_DESCRIPTIONS: tuple[IronOSSensorEntityDescription, ...] = (
62  key=PinecilSensor.LIVE_TEMP,
63  translation_key=PinecilSensor.LIVE_TEMP,
64  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
65  device_class=SensorDeviceClass.TEMPERATURE,
66  state_class=SensorStateClass.MEASUREMENT,
67  value_fn=lambda data: data.live_temp,
68  ),
70  key=PinecilSensor.DC_VOLTAGE,
71  translation_key=PinecilSensor.DC_VOLTAGE,
72  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
73  device_class=SensorDeviceClass.VOLTAGE,
74  state_class=SensorStateClass.MEASUREMENT,
75  value_fn=lambda data: data.dc_voltage,
76  entity_category=EntityCategory.DIAGNOSTIC,
77  ),
79  key=PinecilSensor.HANDLETEMP,
80  translation_key=PinecilSensor.HANDLETEMP,
81  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
82  device_class=SensorDeviceClass.TEMPERATURE,
83  state_class=SensorStateClass.MEASUREMENT,
84  value_fn=lambda data: data.handle_temp,
85  ),
87  key=PinecilSensor.PWMLEVEL,
88  translation_key=PinecilSensor.PWMLEVEL,
89  native_unit_of_measurement=PERCENTAGE,
90  suggested_display_precision=0,
91  device_class=SensorDeviceClass.POWER_FACTOR,
92  state_class=SensorStateClass.MEASUREMENT,
93  value_fn=lambda data: data.pwm_level,
94  entity_category=EntityCategory.DIAGNOSTIC,
95  ),
97  key=PinecilSensor.POWER_SRC,
98  translation_key=PinecilSensor.POWER_SRC,
99  device_class=SensorDeviceClass.ENUM,
100  options=[item.name.lower() for item in PowerSource],
101  value_fn=lambda data: data.power_src.name.lower() if data.power_src else None,
102  entity_category=EntityCategory.DIAGNOSTIC,
103  ),
105  key=PinecilSensor.TIP_RESISTANCE,
106  translation_key=PinecilSensor.TIP_RESISTANCE,
107  native_unit_of_measurement=OHM,
108  value_fn=lambda data: data.tip_resistance,
109  entity_category=EntityCategory.DIAGNOSTIC,
110  ),
112  key=PinecilSensor.UPTIME,
113  translation_key=PinecilSensor.UPTIME,
114  native_unit_of_measurement=UnitOfTime.SECONDS,
115  device_class=SensorDeviceClass.DURATION,
116  state_class=SensorStateClass.TOTAL_INCREASING,
117  value_fn=lambda data: data.uptime,
118  entity_category=EntityCategory.DIAGNOSTIC,
119  ),
121  key=PinecilSensor.MOVEMENT_TIME,
122  translation_key=PinecilSensor.MOVEMENT_TIME,
123  native_unit_of_measurement=UnitOfTime.SECONDS,
124  device_class=SensorDeviceClass.DURATION,
125  state_class=SensorStateClass.MEASUREMENT,
126  value_fn=lambda data: data.movement_time,
127  entity_category=EntityCategory.DIAGNOSTIC,
128  ),
130  key=PinecilSensor.MAX_TIP_TEMP_ABILITY,
131  translation_key=PinecilSensor.MAX_TIP_TEMP_ABILITY,
132  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
133  device_class=SensorDeviceClass.TEMPERATURE,
134  value_fn=lambda data: data.max_tip_temp_ability,
135  entity_category=EntityCategory.DIAGNOSTIC,
136  ),
138  key=PinecilSensor.TIP_VOLTAGE,
139  translation_key=PinecilSensor.TIP_VOLTAGE,
140  native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
141  device_class=SensorDeviceClass.VOLTAGE,
142  state_class=SensorStateClass.MEASUREMENT,
143  suggested_display_precision=3,
144  value_fn=lambda data: data.tip_voltage,
145  entity_category=EntityCategory.DIAGNOSTIC,
146  ),
148  key=PinecilSensor.HALL_SENSOR,
149  translation_key=PinecilSensor.HALL_SENSOR,
150  state_class=SensorStateClass.MEASUREMENT,
151  entity_registry_enabled_default=False,
152  value_fn=lambda data: data.hall_sensor,
153  entity_category=EntityCategory.DIAGNOSTIC,
154  ),
156  key=PinecilSensor.OPERATING_MODE,
157  translation_key=PinecilSensor.OPERATING_MODE,
158  device_class=SensorDeviceClass.ENUM,
159  options=[item.name.lower() for item in OperatingMode],
160  value_fn=(
161  lambda data: data.operating_mode.name.lower()
162  if data.operating_mode
163  else None
164  ),
165  ),
167  key=PinecilSensor.ESTIMATED_POWER,
168  translation_key=PinecilSensor.ESTIMATED_POWER,
169  native_unit_of_measurement=UnitOfPower.WATT,
170  device_class=SensorDeviceClass.POWER,
171  state_class=SensorStateClass.MEASUREMENT,
172  value_fn=lambda data: data.estimated_power,
173  ),
174 )
175 
176 
178  hass: HomeAssistant,
179  entry: IronOSConfigEntry,
180  async_add_entities: AddEntitiesCallback,
181 ) -> None:
182  """Set up sensors from a config entry."""
183  coordinator = entry.runtime_data
184 
186  IronOSSensorEntity(coordinator, description)
187  for description in PINECIL_SENSOR_DESCRIPTIONS
188  )
189 
190 
192  """Representation of a IronOS sensor entity."""
193 
194  entity_description: IronOSSensorEntityDescription
195 
196  @property
197  def native_value(self) -> StateType:
198  """Return sensor state."""
199  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None async_setup_entry(HomeAssistant hass, IronOSConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:181