mirror of
https://github.com/home-assistant/core.git
synced 2025-04-28 19:27:51 +00:00
Rework Brother sensor platform (#79864)
* Rework BrotherSensorEntityDescription * Rework state attributes * Cleaning * Add _handle_coordinator_update() * Suggested change * Re-add consts
This commit is contained in:
parent
9019fcb5c5
commit
1e13433d4f
@ -1,10 +1,13 @@
|
|||||||
"""Support for the Brother service."""
|
"""Support for the Brother service."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, cast
|
from typing import Any
|
||||||
|
|
||||||
|
from brother import BrotherSensors
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
DOMAIN as PLATFORM,
|
DOMAIN as PLATFORM,
|
||||||
@ -15,7 +18,7 @@ from homeassistant.components.sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, PERCENTAGE
|
from homeassistant.const import CONF_HOST, PERCENTAGE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -25,69 +28,285 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|||||||
from . import BrotherDataUpdateCoordinator
|
from . import BrotherDataUpdateCoordinator
|
||||||
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
|
|
||||||
ATTR_BELT_UNIT_REMAINING_LIFE = "belt_unit_remaining_life"
|
|
||||||
ATTR_BLACK_DRUM_COUNTER = "black_drum_counter"
|
|
||||||
ATTR_BLACK_DRUM_REMAINING_LIFE = "black_drum_remaining_life"
|
|
||||||
ATTR_BLACK_DRUM_REMAINING_PAGES = "black_drum_remaining_pages"
|
|
||||||
ATTR_BLACK_INK_REMAINING = "black_ink_remaining"
|
|
||||||
ATTR_BLACK_TONER_REMAINING = "black_toner_remaining"
|
|
||||||
ATTR_BW_COUNTER = "bw_counter"
|
|
||||||
ATTR_COLOR_COUNTER = "color_counter"
|
|
||||||
ATTR_COUNTER = "counter"
|
ATTR_COUNTER = "counter"
|
||||||
ATTR_CYAN_DRUM_COUNTER = "cyan_drum_counter"
|
|
||||||
ATTR_CYAN_DRUM_REMAINING_LIFE = "cyan_drum_remaining_life"
|
|
||||||
ATTR_CYAN_DRUM_REMAINING_PAGES = "cyan_drum_remaining_pages"
|
|
||||||
ATTR_CYAN_INK_REMAINING = "cyan_ink_remaining"
|
|
||||||
ATTR_CYAN_TONER_REMAINING = "cyan_toner_remaining"
|
|
||||||
ATTR_DRUM_COUNTER = "drum_counter"
|
|
||||||
ATTR_DRUM_REMAINING_LIFE = "drum_remaining_life"
|
|
||||||
ATTR_DRUM_REMAINING_PAGES = "drum_remaining_pages"
|
|
||||||
ATTR_DUPLEX_COUNTER = "duplex_unit_pages_counter"
|
|
||||||
ATTR_FUSER_REMAINING_LIFE = "fuser_remaining_life"
|
|
||||||
ATTR_LASER_REMAINING_LIFE = "laser_remaining_life"
|
|
||||||
ATTR_MAGENTA_DRUM_COUNTER = "magenta_drum_counter"
|
|
||||||
ATTR_MAGENTA_DRUM_REMAINING_LIFE = "magenta_drum_remaining_life"
|
|
||||||
ATTR_MAGENTA_DRUM_REMAINING_PAGES = "magenta_drum_remaining_pages"
|
|
||||||
ATTR_MAGENTA_INK_REMAINING = "magenta_ink_remaining"
|
|
||||||
ATTR_MAGENTA_TONER_REMAINING = "magenta_toner_remaining"
|
|
||||||
ATTR_MANUFACTURER = "Brother"
|
|
||||||
ATTR_PAGE_COUNTER = "page_counter"
|
|
||||||
ATTR_PF_KIT_1_REMAINING_LIFE = "pf_kit_1_remaining_life"
|
|
||||||
ATTR_PF_KIT_MP_REMAINING_LIFE = "pf_kit_mp_remaining_life"
|
|
||||||
ATTR_REMAINING_PAGES = "remaining_pages"
|
ATTR_REMAINING_PAGES = "remaining_pages"
|
||||||
ATTR_STATUS = "status"
|
|
||||||
ATTR_UPTIME = "uptime"
|
|
||||||
ATTR_YELLOW_DRUM_COUNTER = "yellow_drum_counter"
|
|
||||||
ATTR_YELLOW_DRUM_REMAINING_LIFE = "yellow_drum_remaining_life"
|
|
||||||
ATTR_YELLOW_DRUM_REMAINING_PAGES = "yellow_drum_remaining_pages"
|
|
||||||
ATTR_YELLOW_INK_REMAINING = "yellow_ink_remaining"
|
|
||||||
ATTR_YELLOW_TONER_REMAINING = "yellow_toner_remaining"
|
|
||||||
|
|
||||||
UNIT_PAGES = "p"
|
UNIT_PAGES = "p"
|
||||||
|
|
||||||
ATTRS_MAP: dict[str, tuple[str, str]] = {
|
|
||||||
ATTR_DRUM_REMAINING_LIFE: (ATTR_DRUM_REMAINING_PAGES, ATTR_DRUM_COUNTER),
|
|
||||||
ATTR_BLACK_DRUM_REMAINING_LIFE: (
|
|
||||||
ATTR_BLACK_DRUM_REMAINING_PAGES,
|
|
||||||
ATTR_BLACK_DRUM_COUNTER,
|
|
||||||
),
|
|
||||||
ATTR_CYAN_DRUM_REMAINING_LIFE: (
|
|
||||||
ATTR_CYAN_DRUM_REMAINING_PAGES,
|
|
||||||
ATTR_CYAN_DRUM_COUNTER,
|
|
||||||
),
|
|
||||||
ATTR_MAGENTA_DRUM_REMAINING_LIFE: (
|
|
||||||
ATTR_MAGENTA_DRUM_REMAINING_PAGES,
|
|
||||||
ATTR_MAGENTA_DRUM_COUNTER,
|
|
||||||
),
|
|
||||||
ATTR_YELLOW_DRUM_REMAINING_LIFE: (
|
|
||||||
ATTR_YELLOW_DRUM_REMAINING_PAGES,
|
|
||||||
ATTR_YELLOW_DRUM_COUNTER,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BrotherSensorRequiredKeysMixin:
|
||||||
|
"""Class for Brother entity required keys."""
|
||||||
|
|
||||||
|
value: Callable[[BrotherSensors], StateType | datetime]
|
||||||
|
extra_state_attrs: Callable[[BrotherSensors], dict[str, Any]]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BrotherSensorEntityDescription(
|
||||||
|
SensorEntityDescription, BrotherSensorRequiredKeysMixin
|
||||||
|
):
|
||||||
|
"""A class that describes sensor entities."""
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[BrotherSensorEntityDescription, ...] = (
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="status",
|
||||||
|
icon="mdi:printer",
|
||||||
|
name="Status",
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.status,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="page_counter",
|
||||||
|
icon="mdi:file-document-outline",
|
||||||
|
name="Page counter",
|
||||||
|
native_unit_of_measurement=UNIT_PAGES,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.page_counter,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="bw_counter",
|
||||||
|
icon="mdi:file-document-outline",
|
||||||
|
name="B/W counter",
|
||||||
|
native_unit_of_measurement=UNIT_PAGES,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.bw_counter,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="color_counter",
|
||||||
|
icon="mdi:file-document-outline",
|
||||||
|
name="Color counter",
|
||||||
|
native_unit_of_measurement=UNIT_PAGES,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.color_counter,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="duplex_unit_pages_counter",
|
||||||
|
icon="mdi:file-document-outline",
|
||||||
|
name="Duplex unit pages counter",
|
||||||
|
native_unit_of_measurement=UNIT_PAGES,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.duplex_unit_pages_counter,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="drum_remaining_life",
|
||||||
|
icon="mdi:chart-donut",
|
||||||
|
name="Drum remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.drum_remaining_life,
|
||||||
|
extra_state_attrs=lambda data: {
|
||||||
|
ATTR_REMAINING_PAGES: data.drum_remaining_pages,
|
||||||
|
ATTR_COUNTER: data.drum_counter,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="black_drum_remaining_life",
|
||||||
|
icon="mdi:chart-donut",
|
||||||
|
name="Black drum remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.black_drum_remaining_life,
|
||||||
|
extra_state_attrs=lambda data: {
|
||||||
|
ATTR_REMAINING_PAGES: data.black_drum_remaining_pages,
|
||||||
|
ATTR_COUNTER: data.black_drum_counter,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="cyan_drum_remaining_life",
|
||||||
|
icon="mdi:chart-donut",
|
||||||
|
name="Cyan drum remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.cyan_drum_remaining_life,
|
||||||
|
extra_state_attrs=lambda data: {
|
||||||
|
ATTR_REMAINING_PAGES: data.cyan_drum_remaining_pages,
|
||||||
|
ATTR_COUNTER: data.cyan_drum_counter,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="magenta_drum_remaining_life",
|
||||||
|
icon="mdi:chart-donut",
|
||||||
|
name="Magenta drum remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.magenta_drum_remaining_life,
|
||||||
|
extra_state_attrs=lambda data: {
|
||||||
|
ATTR_REMAINING_PAGES: data.magenta_drum_remaining_pages,
|
||||||
|
ATTR_COUNTER: data.magenta_drum_counter,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="yellow_drum_remaining_life",
|
||||||
|
icon="mdi:chart-donut",
|
||||||
|
name="Yellow drum remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.yellow_drum_remaining_life,
|
||||||
|
extra_state_attrs=lambda data: {
|
||||||
|
ATTR_REMAINING_PAGES: data.yellow_drum_remaining_pages,
|
||||||
|
ATTR_COUNTER: data.yellow_drum_counter,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="belt_unit_remaining_life",
|
||||||
|
icon="mdi:current-ac",
|
||||||
|
name="Belt unit remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.belt_unit_remaining_life,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="fuser_remaining_life",
|
||||||
|
icon="mdi:water-outline",
|
||||||
|
name="Fuser remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.fuser_remaining_life,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="laser_remaining_life",
|
||||||
|
icon="mdi:spotlight-beam",
|
||||||
|
name="Laser remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.laser_remaining_life,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="pf_kit_1_remaining_life",
|
||||||
|
icon="mdi:printer-3d",
|
||||||
|
name="PF Kit 1 remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.pf_kit_1_remaining_life,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="pf_kit_mp_remaining_life",
|
||||||
|
icon="mdi:printer-3d",
|
||||||
|
name="PF Kit MP remaining life",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.pf_kit_mp_remaining_life,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="black_toner_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Black toner remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.black_toner_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="cyan_toner_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Cyan toner remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.cyan_toner_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="magenta_toner_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Magenta toner remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.magenta_toner_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="yellow_toner_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Yellow toner remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.yellow_toner_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="black_ink_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Black ink remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.black_ink_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="cyan_ink_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Cyan ink remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.cyan_ink_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="magenta_ink_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Magenta ink remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.magenta_ink_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="yellow_ink_remaining",
|
||||||
|
icon="mdi:printer-3d-nozzle",
|
||||||
|
name="Yellow ink remaining",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.yellow_ink_remaining,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
BrotherSensorEntityDescription(
|
||||||
|
key="uptime",
|
||||||
|
name="Uptime",
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value=lambda data: data.uptime,
|
||||||
|
extra_state_attrs=lambda _: {},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -115,17 +334,15 @@ async def async_setup_entry(
|
|||||||
device_info = DeviceInfo(
|
device_info = DeviceInfo(
|
||||||
configuration_url=f"http://{entry.data[CONF_HOST]}/",
|
configuration_url=f"http://{entry.data[CONF_HOST]}/",
|
||||||
identifiers={(DOMAIN, coordinator.data.serial)},
|
identifiers={(DOMAIN, coordinator.data.serial)},
|
||||||
manufacturer=ATTR_MANUFACTURER,
|
manufacturer="Brother",
|
||||||
model=coordinator.data.model,
|
model=coordinator.data.model,
|
||||||
name=coordinator.data.model,
|
name=coordinator.data.model,
|
||||||
sw_version=coordinator.data.firmware,
|
sw_version=coordinator.data.firmware,
|
||||||
)
|
)
|
||||||
|
|
||||||
for description in SENSOR_TYPES:
|
for description in SENSOR_TYPES:
|
||||||
if getattr(coordinator.data, description.key) is not None:
|
if description.value(coordinator.data) is not None:
|
||||||
sensors.append(
|
sensors.append(BrotherPrinterSensor(coordinator, description, device_info))
|
||||||
description.entity_class(coordinator, description, device_info)
|
|
||||||
)
|
|
||||||
async_add_entities(sensors, False)
|
async_add_entities(sensors, False)
|
||||||
|
|
||||||
|
|
||||||
@ -133,6 +350,7 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
|
|||||||
"""Define an Brother Printer sensor."""
|
"""Define an Brother Printer sensor."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
entity_description: BrotherSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -142,239 +360,19 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attrs: dict[str, Any] = {}
|
|
||||||
self._attr_device_info = device_info
|
self._attr_device_info = device_info
|
||||||
|
self._attr_extra_state_attributes = description.extra_state_attrs(
|
||||||
|
coordinator.data
|
||||||
|
)
|
||||||
|
self._attr_native_value = description.value(coordinator.data)
|
||||||
self._attr_unique_id = f"{coordinator.data.serial.lower()}_{description.key}"
|
self._attr_unique_id = f"{coordinator.data.serial.lower()}_{description.key}"
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def native_value(self) -> StateType | datetime:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Return the state."""
|
"""Handle updated data from the coordinator."""
|
||||||
return cast(
|
self._attr_native_value = self.entity_description.value(self.coordinator.data)
|
||||||
StateType, getattr(self.coordinator.data, self.entity_description.key)
|
self._attr_extra_state_attributes = self.entity_description.extra_state_attrs(
|
||||||
|
self.coordinator.data
|
||||||
)
|
)
|
||||||
|
self.async_write_ha_state()
|
||||||
@property
|
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
|
||||||
"""Return the state attributes."""
|
|
||||||
remaining_pages, drum_counter = ATTRS_MAP.get(
|
|
||||||
self.entity_description.key, (None, None)
|
|
||||||
)
|
|
||||||
if remaining_pages and drum_counter:
|
|
||||||
self._attrs[ATTR_REMAINING_PAGES] = getattr(
|
|
||||||
self.coordinator.data, remaining_pages
|
|
||||||
)
|
|
||||||
self._attrs[ATTR_COUNTER] = getattr(self.coordinator.data, drum_counter)
|
|
||||||
return self._attrs
|
|
||||||
|
|
||||||
|
|
||||||
class BrotherPrinterUptimeSensor(BrotherPrinterSensor):
|
|
||||||
"""Define an Brother Printer Uptime sensor."""
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self) -> datetime:
|
|
||||||
"""Return the state."""
|
|
||||||
return cast(
|
|
||||||
datetime, getattr(self.coordinator.data, self.entity_description.key)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class BrotherSensorEntityDescription(SensorEntityDescription):
|
|
||||||
"""A class that describes sensor entities."""
|
|
||||||
|
|
||||||
entity_class: type[BrotherPrinterSensor] = BrotherPrinterSensor
|
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[BrotherSensorEntityDescription, ...] = (
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_STATUS,
|
|
||||||
icon="mdi:printer",
|
|
||||||
name="Status",
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_PAGE_COUNTER,
|
|
||||||
icon="mdi:file-document-outline",
|
|
||||||
name="Page counter",
|
|
||||||
native_unit_of_measurement=UNIT_PAGES,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_BW_COUNTER,
|
|
||||||
icon="mdi:file-document-outline",
|
|
||||||
name="B/W counter",
|
|
||||||
native_unit_of_measurement=UNIT_PAGES,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_COLOR_COUNTER,
|
|
||||||
icon="mdi:file-document-outline",
|
|
||||||
name="Color counter",
|
|
||||||
native_unit_of_measurement=UNIT_PAGES,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_DUPLEX_COUNTER,
|
|
||||||
icon="mdi:file-document-outline",
|
|
||||||
name="Duplex unit pages counter",
|
|
||||||
native_unit_of_measurement=UNIT_PAGES,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_DRUM_REMAINING_LIFE,
|
|
||||||
icon="mdi:chart-donut",
|
|
||||||
name="Drum remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_BLACK_DRUM_REMAINING_LIFE,
|
|
||||||
icon="mdi:chart-donut",
|
|
||||||
name="Black drum remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_CYAN_DRUM_REMAINING_LIFE,
|
|
||||||
icon="mdi:chart-donut",
|
|
||||||
name="Cyan drum remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_MAGENTA_DRUM_REMAINING_LIFE,
|
|
||||||
icon="mdi:chart-donut",
|
|
||||||
name="Magenta drum remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_YELLOW_DRUM_REMAINING_LIFE,
|
|
||||||
icon="mdi:chart-donut",
|
|
||||||
name="Yellow drum remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_BELT_UNIT_REMAINING_LIFE,
|
|
||||||
icon="mdi:current-ac",
|
|
||||||
name="Belt unit remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_FUSER_REMAINING_LIFE,
|
|
||||||
icon="mdi:water-outline",
|
|
||||||
name="Fuser remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_LASER_REMAINING_LIFE,
|
|
||||||
icon="mdi:spotlight-beam",
|
|
||||||
name="Laser remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_PF_KIT_1_REMAINING_LIFE,
|
|
||||||
icon="mdi:printer-3d",
|
|
||||||
name="PF Kit 1 remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_PF_KIT_MP_REMAINING_LIFE,
|
|
||||||
icon="mdi:printer-3d",
|
|
||||||
name="PF Kit MP remaining life",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_BLACK_TONER_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Black toner remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_CYAN_TONER_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Cyan toner remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_MAGENTA_TONER_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Magenta toner remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_YELLOW_TONER_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Yellow toner remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_BLACK_INK_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Black ink remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_CYAN_INK_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Cyan ink remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_MAGENTA_INK_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Magenta ink remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_YELLOW_INK_REMAINING,
|
|
||||||
icon="mdi:printer-3d-nozzle",
|
|
||||||
name="Yellow ink remaining",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
),
|
|
||||||
BrotherSensorEntityDescription(
|
|
||||||
key=ATTR_UPTIME,
|
|
||||||
name="Uptime",
|
|
||||||
entity_registry_enabled_default=False,
|
|
||||||
device_class=SensorDeviceClass.TIMESTAMP,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
entity_class=BrotherPrinterUptimeSensor,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user