1 """Support for IPP sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
10 from pyipp
import Marker, Printer
15 SensorEntityDescription,
23 from .
import IPPConfigEntry
27 ATTR_MARKER_HIGH_LEVEL,
28 ATTR_MARKER_LOW_LEVEL,
35 from .entity
import IPPEntity
38 @dataclass(frozen=True, kw_only=True)
40 """Describes IPP sensor entity."""
42 value_fn: Callable[[Printer], StateType | datetime]
43 attributes_fn: Callable[[Printer], dict[Any, StateType]] =
lambda _: {}
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])
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])
58 PRINTER_SENSORS: tuple[IPPSensorEntityDescription, ...] = (
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),
74 value_fn=
lambda printer: printer.state.printer_state,
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,
89 entry: IPPConfigEntry,
90 async_add_entities: AddEntitiesCallback,
92 """Set up IPP sensor based on a config entry."""
93 coordinator = entry.runtime_data
94 sensors: list[SensorEntity] = [
99 for description
in PRINTER_SENSORS
102 for index, marker
in enumerate(coordinator.data.markers):
107 key=f
"marker_{index}",
109 translation_key=
"marker",
110 native_unit_of_measurement=PERCENTAGE,
111 state_class=SensorStateClass.MEASUREMENT,
115 ATTR_MARKER_HIGH_LEVEL: marker.high_level,
116 ATTR_MARKER_LOW_LEVEL: marker.low_level,
117 ATTR_MARKER_TYPE: marker.marker_type,
122 lambda marker: marker.level
if marker.level >= 0
else None,
132 """Defines an IPP sensor."""
134 entity_description: IPPSensorEntityDescription
138 """Return the state attributes of the entity."""
143 """Return the state of the sensor."""
dict[str, Any] extra_state_attributes(self)
StateType|datetime native_value(self)
None async_setup_entry(HomeAssistant hass, IPPConfigEntry entry, AddEntitiesCallback async_add_entities)
Callable[[Printer], StateType|datetime] _get_marker_value_fn(int marker_index, Callable[[Marker], StateType|datetime] value_fn)
Callable[[Printer], dict[Any, StateType]] _get_marker_attributes_fn(int marker_index, Callable[[Marker], dict[Any, StateType]] attributes_fn)