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-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-04-29 14:59:31 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-08-26 08:55:57 +00:00
|
|
|
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
|
2021-04-29 14:59:31 +00:00
|
|
|
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 (
|
|
|
|
ATTR_MANUFACTURER,
|
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,
|
|
|
|
)
|
|
|
|
|
2020-03-09 18:54:43 +00:00
|
|
|
ATTR_COUNTER = "counter"
|
|
|
|
ATTR_REMAINING_PAGES = "remaining_pages"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for sensor in SENSOR_TYPES:
|
2020-03-18 03:21:56 +00:00
|
|
|
if sensor in coordinator.data:
|
|
|
|
sensors.append(BrotherPrinterSensor(coordinator, sensor, device_info))
|
|
|
|
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-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._description = SENSOR_TYPES[kind]
|
|
|
|
self._name = f"{coordinator.data.model} {self._description['label']}"
|
2021-04-22 11:20:14 +00:00
|
|
|
self._unique_id = f"{coordinator.data.serial.lower()}_{kind}"
|
2020-01-06 17:06:16 +00:00
|
|
|
self._device_info = device_info
|
|
|
|
self.kind = kind
|
2021-04-29 14:59:31 +00:00
|
|
|
self._attrs: dict[str, Any] = {}
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def name(self) -> str:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the name."""
|
2020-03-18 03:21:56 +00:00
|
|
|
return self._name
|
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
|
|
|
|
2020-08-26 08:55:57 +00:00
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def device_class(self) -> str | None:
|
2020-08-26 08:55:57 +00:00
|
|
|
"""Return the class of this sensor."""
|
|
|
|
if self.kind == ATTR_UPTIME:
|
|
|
|
return DEVICE_CLASS_TIMESTAMP
|
|
|
|
return None
|
|
|
|
|
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
|
|
|
|
|
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def icon(self) -> str | None:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the icon."""
|
2021-04-29 14:59:31 +00:00
|
|
|
return self._description["icon"]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def unit_of_measurement(self) -> str | None:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the unit the value is expressed in."""
|
2021-04-29 14:59:31 +00:00
|
|
|
return self._description["unit"]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-30 21:21:39 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the device info."""
|
|
|
|
return self._device_info
|
|
|
|
|
2020-03-18 03:21:56 +00:00
|
|
|
@property
|
2021-04-29 14:59:31 +00:00
|
|
|
def entity_registry_enabled_default(self) -> bool:
|
2020-03-18 03:21:56 +00:00
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
2021-04-29 14:59:31 +00:00
|
|
|
return self._description["enabled"]
|