Use SensorEntityDescription in Brother integration ()

This commit is contained in:
Maciej Bieniek 2021-07-27 17:26:47 +02:00 committed by GitHub
parent 0552b9c783
commit ea9d312b45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 118 deletions
homeassistant/components/brother

View file

@ -3,11 +3,12 @@ from __future__ import annotations
from typing import Final
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT
from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
SensorEntityDescription,
)
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, PERCENTAGE
from .model import BrotherSensorMetadata
ATTR_BELT_UNIT_REMAINING_LIFE: Final = "belt_unit_remaining_life"
ATTR_BLACK_DRUM_COUNTER: Final = "black_drum_counter"
ATTR_BLACK_DRUM_REMAINING_LIFE: Final = "black_drum_remaining_life"
@ -76,172 +77,170 @@ ATTRS_MAP: Final[dict[str, tuple[str, str]]] = {
),
}
SENSOR_TYPES: Final[dict[str, BrotherSensorMetadata]] = {
ATTR_STATUS: BrotherSensorMetadata(
SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
key=ATTR_STATUS,
icon="mdi:printer",
label=ATTR_STATUS.title(),
unit_of_measurement=None,
enabled=True,
name=ATTR_STATUS.title(),
),
ATTR_PAGE_COUNTER: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_PAGE_COUNTER,
icon="mdi:file-document-outline",
label=ATTR_PAGE_COUNTER.replace("_", " ").title(),
name=ATTR_PAGE_COUNTER.replace("_", " ").title(),
unit_of_measurement=UNIT_PAGES,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_BW_COUNTER: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_BW_COUNTER,
icon="mdi:file-document-outline",
label=ATTR_BW_COUNTER.replace("_", " ").title(),
name=ATTR_BW_COUNTER.replace("_", " ").title(),
unit_of_measurement=UNIT_PAGES,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_COLOR_COUNTER: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_COLOR_COUNTER,
icon="mdi:file-document-outline",
label=ATTR_COLOR_COUNTER.replace("_", " ").title(),
name=ATTR_COLOR_COUNTER.replace("_", " ").title(),
unit_of_measurement=UNIT_PAGES,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_DUPLEX_COUNTER: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_DUPLEX_COUNTER,
icon="mdi:file-document-outline",
label=ATTR_DUPLEX_COUNTER.replace("_", " ").title(),
name=ATTR_DUPLEX_COUNTER.replace("_", " ").title(),
unit_of_measurement=UNIT_PAGES,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_DRUM_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_DRUM_REMAINING_LIFE,
icon="mdi:chart-donut",
label=ATTR_DRUM_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_DRUM_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_BLACK_DRUM_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_BLACK_DRUM_REMAINING_LIFE,
icon="mdi:chart-donut",
label=ATTR_BLACK_DRUM_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_BLACK_DRUM_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_CYAN_DRUM_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_CYAN_DRUM_REMAINING_LIFE,
icon="mdi:chart-donut",
label=ATTR_CYAN_DRUM_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_CYAN_DRUM_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_MAGENTA_DRUM_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_MAGENTA_DRUM_REMAINING_LIFE,
icon="mdi:chart-donut",
label=ATTR_MAGENTA_DRUM_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_MAGENTA_DRUM_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_YELLOW_DRUM_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_YELLOW_DRUM_REMAINING_LIFE,
icon="mdi:chart-donut",
label=ATTR_YELLOW_DRUM_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_YELLOW_DRUM_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_BELT_UNIT_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_BELT_UNIT_REMAINING_LIFE,
icon="mdi:current-ac",
label=ATTR_BELT_UNIT_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_BELT_UNIT_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_FUSER_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_FUSER_REMAINING_LIFE,
icon="mdi:water-outline",
label=ATTR_FUSER_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_FUSER_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_LASER_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_LASER_REMAINING_LIFE,
icon="mdi:spotlight-beam",
label=ATTR_LASER_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_LASER_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_PF_KIT_1_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_PF_KIT_1_REMAINING_LIFE,
icon="mdi:printer-3d",
label=ATTR_PF_KIT_1_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_PF_KIT_1_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_PF_KIT_MP_REMAINING_LIFE: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_PF_KIT_MP_REMAINING_LIFE,
icon="mdi:printer-3d",
label=ATTR_PF_KIT_MP_REMAINING_LIFE.replace("_", " ").title(),
name=ATTR_PF_KIT_MP_REMAINING_LIFE.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_BLACK_TONER_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_BLACK_TONER_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_BLACK_TONER_REMAINING.replace("_", " ").title(),
name=ATTR_BLACK_TONER_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_CYAN_TONER_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_CYAN_TONER_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_CYAN_TONER_REMAINING.replace("_", " ").title(),
name=ATTR_CYAN_TONER_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_MAGENTA_TONER_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_MAGENTA_TONER_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_MAGENTA_TONER_REMAINING.replace("_", " ").title(),
name=ATTR_MAGENTA_TONER_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_YELLOW_TONER_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_YELLOW_TONER_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_YELLOW_TONER_REMAINING.replace("_", " ").title(),
name=ATTR_YELLOW_TONER_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_BLACK_INK_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_BLACK_INK_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_BLACK_INK_REMAINING.replace("_", " ").title(),
name=ATTR_BLACK_INK_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_CYAN_INK_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_CYAN_INK_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_CYAN_INK_REMAINING.replace("_", " ").title(),
name=ATTR_CYAN_INK_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_MAGENTA_INK_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_MAGENTA_INK_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_MAGENTA_INK_REMAINING.replace("_", " ").title(),
name=ATTR_MAGENTA_INK_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_YELLOW_INK_REMAINING: BrotherSensorMetadata(
SensorEntityDescription(
key=ATTR_YELLOW_INK_REMAINING,
icon="mdi:printer-3d-nozzle",
label=ATTR_YELLOW_INK_REMAINING.replace("_", " ").title(),
name=ATTR_YELLOW_INK_REMAINING.replace("_", " ").title(),
unit_of_measurement=PERCENTAGE,
enabled=True,
state_class=STATE_CLASS_MEASUREMENT,
),
ATTR_UPTIME: BrotherSensorMetadata(
icon=None,
label=ATTR_UPTIME.title(),
unit_of_measurement=None,
enabled=False,
SensorEntityDescription(
key=ATTR_UPTIME,
name=ATTR_UPTIME.title(),
entity_registry_enabled_default=False,
device_class=DEVICE_CLASS_TIMESTAMP,
),
}
)

View file

@ -1,15 +0,0 @@
"""Type definitions for Brother integration."""
from __future__ import annotations
from typing import NamedTuple
class BrotherSensorMetadata(NamedTuple):
"""Metadata for an individual Brother sensor."""
icon: str | None
label: str
unit_of_measurement: str | None
enabled: bool
state_class: str | None = None
device_class: str | None = None

View file

@ -1,13 +1,14 @@
"""Support for the Brother service."""
from __future__ import annotations
from typing import Any
from typing import Any, cast
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import BrotherDataUpdateCoordinator
@ -21,7 +22,6 @@ from .const import (
DOMAIN,
SENSOR_TYPES,
)
from .model import BrotherSensorMetadata
async def async_setup_entry(
@ -40,11 +40,9 @@ async def async_setup_entry(
"sw_version": getattr(coordinator.data, "firmware", None),
}
for sensor, metadata in SENSOR_TYPES.items():
if sensor in coordinator.data:
sensors.append(
BrotherPrinterSensor(coordinator, sensor, metadata, device_info)
)
for description in SENSOR_TYPES:
if description.key in coordinator.data:
sensors.append(BrotherPrinterSensor(coordinator, description, device_info))
async_add_entities(sensors, False)
@ -54,34 +52,35 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
def __init__(
self,
coordinator: BrotherDataUpdateCoordinator,
kind: str,
metadata: BrotherSensorMetadata,
description: SensorEntityDescription,
device_info: DeviceInfo,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attrs: dict[str, Any] = {}
self._attr_device_class = metadata.device_class
self._attr_device_info = device_info
self._attr_entity_registry_enabled_default = metadata.enabled
self._attr_icon = metadata.icon
self._attr_name = f"{coordinator.data.model} {metadata.label}"
self._attr_state_class = metadata.state_class
self._attr_unique_id = f"{coordinator.data.serial.lower()}_{kind}"
self._attr_unit_of_measurement = metadata.unit_of_measurement
self.kind = kind
self._attr_name = f"{coordinator.data.model} {description.name}"
self._attr_unique_id = f"{coordinator.data.serial.lower()}_{description.key}"
self.entity_description = description
@property
def state(self) -> Any:
def state(self) -> StateType:
"""Return the state."""
if self.kind == ATTR_UPTIME:
return getattr(self.coordinator.data, self.kind).isoformat()
return getattr(self.coordinator.data, self.kind)
if self.entity_description.key == ATTR_UPTIME:
return cast(
StateType,
getattr(self.coordinator.data, self.entity_description.key).isoformat(),
)
return cast(
StateType, getattr(self.coordinator.data, self.entity_description.key)
)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
remaining_pages, drum_counter = ATTRS_MAP.get(self.kind, (None, None))
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