2020-03-30 23:13:47 +00:00
|
|
|
"""Support for IPP sensors."""
|
2021-03-18 09:02:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-08-23 14:34:21 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
2021-11-23 21:55:32 +00:00
|
|
|
from datetime import datetime, timedelta
|
2021-04-29 10:28:14 +00:00
|
|
|
from typing import Any
|
2020-03-30 23:13:47 +00:00
|
|
|
|
2023-08-23 14:34:21 +00:00
|
|
|
from pyipp import Marker, Printer
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2020-03-30 23:13:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-08-23 14:34:21 +00:00
|
|
|
from homeassistant.const import ATTR_LOCATION, PERCENTAGE, EntityCategory
|
2021-04-23 07:55:20 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-08-23 14:34:21 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-03-30 23:13:47 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_COMMAND_SET,
|
|
|
|
ATTR_INFO,
|
|
|
|
ATTR_MARKER_HIGH_LEVEL,
|
|
|
|
ATTR_MARKER_LOW_LEVEL,
|
|
|
|
ATTR_MARKER_TYPE,
|
|
|
|
ATTR_SERIAL,
|
|
|
|
ATTR_STATE_MESSAGE,
|
|
|
|
ATTR_STATE_REASON,
|
|
|
|
ATTR_URI_SUPPORTED,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2021-06-29 08:02:49 +00:00
|
|
|
from .coordinator import IPPDataUpdateCoordinator
|
|
|
|
from .entity import IPPEntity
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
|
2023-08-23 14:34:21 +00:00
|
|
|
@dataclass
|
|
|
|
class IPPSensorEntityDescriptionMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[Printer], StateType | datetime]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class IPPSensorEntityDescription(
|
|
|
|
SensorEntityDescription, IPPSensorEntityDescriptionMixin
|
|
|
|
):
|
|
|
|
"""Describes IPP sensor entity."""
|
|
|
|
|
|
|
|
attributes_fn: Callable[[Printer], dict[Any, StateType]] = lambda _: {}
|
|
|
|
|
|
|
|
|
|
|
|
def _get_marker_attributes_fn(
|
|
|
|
marker_index: int, attributes_fn: Callable[[Marker], dict[Any, StateType]]
|
|
|
|
) -> Callable[[Printer], dict[Any, StateType]]:
|
|
|
|
return lambda printer: attributes_fn(printer.markers[marker_index])
|
|
|
|
|
|
|
|
|
|
|
|
def _get_marker_value_fn(
|
|
|
|
marker_index: int, value_fn: Callable[[Marker], StateType | datetime]
|
|
|
|
) -> Callable[[Printer], StateType | datetime]:
|
|
|
|
return lambda printer: value_fn(printer.markers[marker_index])
|
|
|
|
|
|
|
|
|
|
|
|
PRINTER_SENSORS: tuple[IPPSensorEntityDescription, ...] = (
|
|
|
|
IPPSensorEntityDescription(
|
|
|
|
key="printer",
|
|
|
|
name=None,
|
|
|
|
translation_key="printer",
|
|
|
|
icon="mdi:printer",
|
|
|
|
device_class=SensorDeviceClass.ENUM,
|
|
|
|
options=["idle", "printing", "stopped"],
|
|
|
|
attributes_fn=lambda printer: {
|
|
|
|
ATTR_INFO: printer.info.printer_info,
|
|
|
|
ATTR_SERIAL: printer.info.serial,
|
|
|
|
ATTR_LOCATION: printer.info.location,
|
|
|
|
ATTR_STATE_MESSAGE: printer.state.message,
|
|
|
|
ATTR_STATE_REASON: printer.state.reasons,
|
|
|
|
ATTR_COMMAND_SET: printer.info.command_set,
|
|
|
|
ATTR_URI_SUPPORTED: ",".join(printer.info.printer_uri_supported),
|
|
|
|
},
|
|
|
|
value_fn=lambda printer: printer.state.printer_state,
|
|
|
|
),
|
|
|
|
IPPSensorEntityDescription(
|
|
|
|
key="uptime",
|
|
|
|
translation_key="uptime",
|
|
|
|
icon="mdi:clock-outline",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
value_fn=lambda printer: (utcnow() - timedelta(seconds=printer.info.uptime)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-30 23:13:47 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-23 07:55:20 +00:00
|
|
|
hass: HomeAssistant,
|
2020-03-30 23:13:47 +00:00
|
|
|
entry: ConfigEntry,
|
2021-04-29 10:28:14 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-03-30 23:13:47 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up IPP sensor based on a config entry."""
|
|
|
|
coordinator: IPPDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2023-08-23 14:34:21 +00:00
|
|
|
sensors: list[SensorEntity] = [
|
|
|
|
IPPSensor(
|
|
|
|
coordinator,
|
|
|
|
description,
|
|
|
|
)
|
|
|
|
for description in PRINTER_SENSORS
|
|
|
|
]
|
2020-03-30 23:13:47 +00:00
|
|
|
|
2023-08-23 14:34:21 +00:00
|
|
|
for index, marker in enumerate(coordinator.data.markers):
|
2020-04-30 07:21:53 +00:00
|
|
|
sensors.append(
|
2023-08-23 14:34:21 +00:00
|
|
|
IPPSensor(
|
|
|
|
coordinator,
|
|
|
|
IPPSensorEntityDescription(
|
|
|
|
key=f"marker_{index}",
|
|
|
|
name=marker.name,
|
|
|
|
icon="mdi:water",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
attributes_fn=_get_marker_attributes_fn(
|
|
|
|
index,
|
|
|
|
lambda marker: {
|
|
|
|
ATTR_MARKER_HIGH_LEVEL: marker.high_level,
|
|
|
|
ATTR_MARKER_LOW_LEVEL: marker.low_level,
|
|
|
|
ATTR_MARKER_TYPE: marker.marker_type,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
value_fn=_get_marker_value_fn(index, lambda marker: marker.level),
|
|
|
|
),
|
|
|
|
)
|
2020-04-30 07:21:53 +00:00
|
|
|
)
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class IPPSensor(IPPEntity, SensorEntity):
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Defines an IPP sensor."""
|
|
|
|
|
2023-08-23 14:34:21 +00:00
|
|
|
entity_description: IPPSensorEntityDescription
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
@property
|
2023-08-23 14:34:21 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state attributes of the entity."""
|
2023-08-23 14:34:21 +00:00
|
|
|
return self.entity_description.attributes_fn(self.coordinator.data)
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
@property
|
2023-08-23 14:34:21 +00:00
|
|
|
def native_value(self) -> StateType | datetime:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state of the sensor."""
|
2023-08-23 14:34:21 +00:00
|
|
|
return self.entity_description.value_fn(self.coordinator.data)
|