Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for IPP sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime
8 from typing import Any
9 
10 from pyipp import Marker, Printer
11 
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16  SensorStateClass,
17 )
18 from homeassistant.const import ATTR_LOCATION, PERCENTAGE, EntityCategory
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import StateType
22 
23 from . import IPPConfigEntry
24 from .const import (
25  ATTR_COMMAND_SET,
26  ATTR_INFO,
27  ATTR_MARKER_HIGH_LEVEL,
28  ATTR_MARKER_LOW_LEVEL,
29  ATTR_MARKER_TYPE,
30  ATTR_SERIAL,
31  ATTR_STATE_MESSAGE,
32  ATTR_STATE_REASON,
33  ATTR_URI_SUPPORTED,
34 )
35 from .entity import IPPEntity
36 
37 
38 @dataclass(frozen=True, kw_only=True)
40  """Describes IPP sensor entity."""
41 
42  value_fn: Callable[[Printer], StateType | datetime]
43  attributes_fn: Callable[[Printer], dict[Any, StateType]] = lambda _: {}
44 
45 
47  marker_index: int, attributes_fn: Callable[[Marker], dict[Any, StateType]]
48 ) -> Callable[[Printer], dict[Any, StateType]]:
49  return lambda printer: attributes_fn(printer.markers[marker_index])
50 
51 
53  marker_index: int, value_fn: Callable[[Marker], StateType | datetime]
54 ) -> Callable[[Printer], StateType | datetime]:
55  return lambda printer: value_fn(printer.markers[marker_index])
56 
57 
58 PRINTER_SENSORS: tuple[IPPSensorEntityDescription, ...] = (
60  key="printer",
61  name=None,
62  translation_key="printer",
63  device_class=SensorDeviceClass.ENUM,
64  options=["idle", "printing", "stopped"],
65  attributes_fn=lambda printer: {
66  ATTR_INFO: printer.info.printer_info,
67  ATTR_SERIAL: printer.info.serial,
68  ATTR_LOCATION: printer.info.location,
69  ATTR_STATE_MESSAGE: printer.state.message,
70  ATTR_STATE_REASON: printer.state.reasons,
71  ATTR_COMMAND_SET: printer.info.command_set,
72  ATTR_URI_SUPPORTED: ",".join(printer.info.printer_uri_supported),
73  },
74  value_fn=lambda printer: printer.state.printer_state,
75  ),
77  key="uptime",
78  translation_key="uptime",
79  device_class=SensorDeviceClass.TIMESTAMP,
80  entity_category=EntityCategory.DIAGNOSTIC,
81  entity_registry_enabled_default=False,
82  value_fn=lambda printer: printer.booted_at,
83  ),
84 )
85 
86 
88  hass: HomeAssistant,
89  entry: IPPConfigEntry,
90  async_add_entities: AddEntitiesCallback,
91 ) -> None:
92  """Set up IPP sensor based on a config entry."""
93  coordinator = entry.runtime_data
94  sensors: list[SensorEntity] = [
95  IPPSensor(
96  coordinator,
97  description,
98  )
99  for description in PRINTER_SENSORS
100  ]
101 
102  for index, marker in enumerate(coordinator.data.markers):
103  sensors.append(
104  IPPSensor(
105  coordinator,
107  key=f"marker_{index}",
108  name=marker.name,
109  translation_key="marker",
110  native_unit_of_measurement=PERCENTAGE,
111  state_class=SensorStateClass.MEASUREMENT,
112  attributes_fn=_get_marker_attributes_fn(
113  index,
114  lambda marker: {
115  ATTR_MARKER_HIGH_LEVEL: marker.high_level,
116  ATTR_MARKER_LOW_LEVEL: marker.low_level,
117  ATTR_MARKER_TYPE: marker.marker_type,
118  },
119  ),
120  value_fn=_get_marker_value_fn(
121  index,
122  lambda marker: marker.level if marker.level >= 0 else None,
123  ),
124  ),
125  )
126  )
127 
128  async_add_entities(sensors, True)
129 
130 
132  """Defines an IPP sensor."""
133 
134  entity_description: IPPSensorEntityDescription
135 
136  @property
137  def extra_state_attributes(self) -> dict[str, Any]:
138  """Return the state attributes of the entity."""
139  return self.entity_descriptionentity_description.attributes_fn(self.coordinator.data)
140 
141  @property
142  def native_value(self) -> StateType | datetime:
143  """Return the state of the sensor."""
144  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
dict[str, Any] extra_state_attributes(self)
Definition: sensor.py:137
StateType|datetime native_value(self)
Definition: sensor.py:142
None async_setup_entry(HomeAssistant hass, IPPConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:91
Callable[[Printer], StateType|datetime] _get_marker_value_fn(int marker_index, Callable[[Marker], StateType|datetime] value_fn)
Definition: sensor.py:54
Callable[[Printer], dict[Any, StateType]] _get_marker_attributes_fn(int marker_index, Callable[[Marker], dict[Any, StateType]] attributes_fn)
Definition: sensor.py:48