2020-01-06 17:06:16 +00:00
|
|
|
"""Support for the Brother service."""
|
2021-04-29 14:59:31 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2021-07-22 08:36:29 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-04-29 14:59:31 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 21:21:39 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-04-29 14:59:31 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-08-30 14:02:12 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2021-04-29 14:59:31 +00:00
|
|
|
from . import BrotherDataUpdateCoordinator
|
2020-01-06 17:06:16 +00:00
|
|
|
from .const import (
|
2021-05-03 13:10:20 +00:00
|
|
|
ATTR_COUNTER,
|
2020-01-06 17:06:16 +00:00
|
|
|
ATTR_MANUFACTURER,
|
2021-05-03 13:10:20 +00:00
|
|
|
ATTR_REMAINING_PAGES,
|
2020-08-26 08:55:57 +00:00
|
|
|
ATTR_UPTIME,
|
2021-04-22 11:20:14 +00:00
|
|
|
ATTRS_MAP,
|
2021-02-12 17:11:35 +00:00
|
|
|
DATA_CONFIG_ENTRY,
|
2020-01-06 17:06:16 +00:00
|
|
|
DOMAIN,
|
|
|
|
SENSOR_TYPES,
|
|
|
|
)
|
2021-07-22 08:36:29 +00:00
|
|
|
from .model import BrotherSensorMetadata
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
|
2021-04-29 14:59:31 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Add Brother entities from a config_entry."""
|
2021-04-29 14:59:31 +00:00
|
|
|
coordinator = hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
sensors = []
|
|
|
|
|
2021-04-30 21:21:39 +00:00
|
|
|
device_info: DeviceInfo = {
|
2021-04-22 11:20:14 +00:00
|
|
|
"identifiers": {(DOMAIN, coordinator.data.serial)},
|
|
|
|
"name": coordinator.data.model,
|
2020-01-06 17:06:16 +00:00
|
|
|
"manufacturer": ATTR_MANUFACTURER,
|
2021-04-22 11:20:14 +00:00
|
|
|
"model": coordinator.data.model,
|
|
|
|
"sw_version": getattr(coordinator.data, "firmware", None),
|
2020-01-06 17:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 08:36:29 +00:00
|
|
|
for sensor, metadata in SENSOR_TYPES.items():
|
2020-03-18 03:21:56 +00:00
|
|
|
if sensor in coordinator.data:
|
2021-07-22 08:36:29 +00:00
|
|
|
sensors.append(
|
|
|
|
BrotherPrinterSensor(coordinator, sensor, metadata, device_info)
|
|
|
|
)
|
2020-03-18 03:21:56 +00:00
|
|
|
async_add_entities(sensors, False)
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Define an Brother Printer sensor."""
|
|
|
|
|
2021-04-29 14:59:31 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: BrotherDataUpdateCoordinator,
|
|
|
|
kind: str,
|
2021-07-22 08:36:29 +00:00
|
|
|
metadata: BrotherSensorMetadata,
|
2021-04-30 21:21:39 +00:00
|
|
|
device_info: DeviceInfo,
|
2021-04-29 14:59:31 +00:00
|
|
|
) -> None:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Initialize."""
|
2020-08-30 14:02:12 +00:00
|
|
|
super().__init__(coordinator)
|
2021-04-29 14:59:31 +00:00
|
|
|
self._attrs: dict[str, Any] = {}
|
2021-07-22 08:36:29 +00:00
|
|
|
self._attr_device_class = metadata.device_class
|
2021-06-11 11:36:17 +00:00
|
|
|
self._attr_device_info = device_info
|
2021-07-22 08:36:29 +00:00
|
|
|
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
|
2021-06-11 11:36:17 +00:00
|
|
|
self._attr_unique_id = f"{coordinator.data.serial.lower()}_{kind}"
|
2021-07-22 08:36:29 +00:00
|
|
|
self._attr_unit_of_measurement = metadata.unit_of_measurement
|
2021-06-11 11:36:17 +00:00
|
|
|
self.kind = kind
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def state(self) -> Any:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the state."""
|
2020-08-26 08:55:57 +00:00
|
|
|
if self.kind == ATTR_UPTIME:
|
2021-04-22 11:20:14 +00:00
|
|
|
return getattr(self.coordinator.data, self.kind).isoformat()
|
|
|
|
return getattr(self.coordinator.data, self.kind)
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the state attributes."""
|
2021-04-22 11:20:14 +00:00
|
|
|
remaining_pages, drum_counter = ATTRS_MAP.get(self.kind, (None, None))
|
2020-03-09 18:54:43 +00:00
|
|
|
if remaining_pages and drum_counter:
|
2021-04-22 11:20:14 +00:00
|
|
|
self._attrs[ATTR_REMAINING_PAGES] = getattr(
|
|
|
|
self.coordinator.data, remaining_pages
|
2020-03-18 03:21:56 +00:00
|
|
|
)
|
2021-04-22 11:20:14 +00:00
|
|
|
self._attrs[ATTR_COUNTER] = getattr(self.coordinator.data, drum_counter)
|
2020-01-06 17:06:16 +00:00
|
|
|
return self._attrs
|