2020-03-30 23:13:47 +00:00
|
|
|
"""Support for IPP sensors."""
|
2021-03-18 09:02:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-03-30 23:13:47 +00:00
|
|
|
from datetime import timedelta
|
2021-04-29 10:28:14 +00:00
|
|
|
from typing import Any
|
2020-03-30 23:13:47 +00:00
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-03-30 23:13:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-03-02 11:52:00 +00:00
|
|
|
from homeassistant.const import ATTR_LOCATION, DEVICE_CLASS_TIMESTAMP, PERCENTAGE
|
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
|
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
|
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
2020-04-30 07:21:53 +00:00
|
|
|
# config flow sets this to either UUID, serial number or None
|
2021-10-17 18:24:34 +00:00
|
|
|
if (unique_id := entry.unique_id) is None:
|
2020-04-30 07:21:53 +00:00
|
|
|
unique_id = entry.entry_id
|
|
|
|
|
2020-03-30 23:13:47 +00:00
|
|
|
sensors = []
|
|
|
|
|
2020-04-30 07:21:53 +00:00
|
|
|
sensors.append(IPPPrinterSensor(entry.entry_id, unique_id, coordinator))
|
|
|
|
sensors.append(IPPUptimeSensor(entry.entry_id, unique_id, coordinator))
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
for marker_index in range(len(coordinator.data.markers)):
|
2020-04-30 07:21:53 +00:00
|
|
|
sensors.append(
|
|
|
|
IPPMarkerSensor(entry.entry_id, unique_id, coordinator, marker_index)
|
|
|
|
)
|
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."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
coordinator: IPPDataUpdateCoordinator,
|
|
|
|
enabled_default: bool = True,
|
|
|
|
entry_id: str,
|
2020-04-30 07:21:53 +00:00
|
|
|
unique_id: str,
|
2020-03-30 23:13:47 +00:00
|
|
|
icon: str,
|
|
|
|
key: str,
|
|
|
|
name: str,
|
2021-03-18 09:02:00 +00:00
|
|
|
unit_of_measurement: str | None = None,
|
2020-03-30 23:13:47 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize IPP sensor."""
|
|
|
|
self._key = key
|
2021-06-29 08:02:49 +00:00
|
|
|
self._attr_unique_id = f"{unique_id}_{key}"
|
2021-08-11 19:17:47 +00:00
|
|
|
self._attr_native_unit_of_measurement = unit_of_measurement
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
entry_id=entry_id,
|
2020-04-30 07:21:53 +00:00
|
|
|
device_id=unique_id,
|
2020-03-30 23:13:47 +00:00
|
|
|
coordinator=coordinator,
|
|
|
|
name=name,
|
|
|
|
icon=icon,
|
|
|
|
enabled_default=enabled_default,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class IPPMarkerSensor(IPPSensor):
|
|
|
|
"""Defines an IPP marker sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
2020-04-30 07:21:53 +00:00
|
|
|
self,
|
|
|
|
entry_id: str,
|
|
|
|
unique_id: str,
|
|
|
|
coordinator: IPPDataUpdateCoordinator,
|
|
|
|
marker_index: int,
|
2020-03-30 23:13:47 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize IPP marker sensor."""
|
|
|
|
self.marker_index = marker_index
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entry_id=entry_id,
|
2020-04-30 07:21:53 +00:00
|
|
|
unique_id=unique_id,
|
2020-03-30 23:13:47 +00:00
|
|
|
icon="mdi:water",
|
|
|
|
key=f"marker_{marker_index}",
|
|
|
|
name=f"{coordinator.data.info.name} {coordinator.data.markers[marker_index].name}",
|
2020-09-05 19:09:14 +00:00
|
|
|
unit_of_measurement=PERCENTAGE,
|
2020-03-30 23:13:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-03-18 09:02:00 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state attributes of the entity."""
|
|
|
|
return {
|
|
|
|
ATTR_MARKER_HIGH_LEVEL: self.coordinator.data.markers[
|
|
|
|
self.marker_index
|
|
|
|
].high_level,
|
|
|
|
ATTR_MARKER_LOW_LEVEL: self.coordinator.data.markers[
|
|
|
|
self.marker_index
|
|
|
|
].low_level,
|
|
|
|
ATTR_MARKER_TYPE: self.coordinator.data.markers[
|
|
|
|
self.marker_index
|
|
|
|
].marker_type,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self) -> int | None:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state of the sensor."""
|
2020-04-10 00:44:04 +00:00
|
|
|
level = self.coordinator.data.markers[self.marker_index].level
|
|
|
|
|
|
|
|
if level >= 0:
|
|
|
|
return level
|
|
|
|
|
|
|
|
return None
|
2020-03-30 23:13:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IPPPrinterSensor(IPPSensor):
|
|
|
|
"""Defines an IPP printer sensor."""
|
|
|
|
|
2020-04-30 07:21:53 +00:00
|
|
|
def __init__(
|
|
|
|
self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator
|
|
|
|
) -> None:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Initialize IPP printer sensor."""
|
|
|
|
super().__init__(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entry_id=entry_id,
|
2020-04-30 07:21:53 +00:00
|
|
|
unique_id=unique_id,
|
2020-03-30 23:13:47 +00:00
|
|
|
icon="mdi:printer",
|
|
|
|
key="printer",
|
|
|
|
name=coordinator.data.info.name,
|
|
|
|
unit_of_measurement=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-03-18 09:02:00 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state attributes of the entity."""
|
|
|
|
return {
|
|
|
|
ATTR_INFO: self.coordinator.data.info.printer_info,
|
|
|
|
ATTR_SERIAL: self.coordinator.data.info.serial,
|
|
|
|
ATTR_LOCATION: self.coordinator.data.info.location,
|
|
|
|
ATTR_STATE_MESSAGE: self.coordinator.data.state.message,
|
|
|
|
ATTR_STATE_REASON: self.coordinator.data.state.reasons,
|
|
|
|
ATTR_COMMAND_SET: self.coordinator.data.info.command_set,
|
|
|
|
ATTR_URI_SUPPORTED: self.coordinator.data.info.printer_uri_supported,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self) -> str:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self.coordinator.data.state.printer_state
|
|
|
|
|
|
|
|
|
|
|
|
class IPPUptimeSensor(IPPSensor):
|
|
|
|
"""Defines a IPP uptime sensor."""
|
|
|
|
|
2021-06-01 19:43:55 +00:00
|
|
|
_attr_device_class = DEVICE_CLASS_TIMESTAMP
|
|
|
|
|
2020-04-30 07:21:53 +00:00
|
|
|
def __init__(
|
|
|
|
self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator
|
|
|
|
) -> None:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Initialize IPP uptime sensor."""
|
|
|
|
super().__init__(
|
|
|
|
coordinator=coordinator,
|
|
|
|
enabled_default=False,
|
|
|
|
entry_id=entry_id,
|
2020-04-30 07:21:53 +00:00
|
|
|
unique_id=unique_id,
|
2020-03-30 23:13:47 +00:00
|
|
|
icon="mdi:clock-outline",
|
|
|
|
key="uptime",
|
|
|
|
name=f"{coordinator.data.info.name} Uptime",
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self) -> str:
|
2020-03-30 23:13:47 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
|
|
|
return uptime.replace(microsecond=0).isoformat()
|