2020-01-06 17:06:16 +00:00
|
|
|
"""Support for the Brother service."""
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-08-26 08:55:57 +00:00
|
|
|
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
|
2020-08-30 14:02:12 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
from .const import (
|
2020-03-09 18:54:43 +00:00
|
|
|
ATTR_BLACK_DRUM_COUNTER,
|
|
|
|
ATTR_BLACK_DRUM_REMAINING_LIFE,
|
|
|
|
ATTR_BLACK_DRUM_REMAINING_PAGES,
|
|
|
|
ATTR_CYAN_DRUM_COUNTER,
|
|
|
|
ATTR_CYAN_DRUM_REMAINING_LIFE,
|
|
|
|
ATTR_CYAN_DRUM_REMAINING_PAGES,
|
2020-01-06 17:06:16 +00:00
|
|
|
ATTR_DRUM_COUNTER,
|
|
|
|
ATTR_DRUM_REMAINING_LIFE,
|
|
|
|
ATTR_DRUM_REMAINING_PAGES,
|
2020-11-22 21:42:16 +00:00
|
|
|
ATTR_ENABLED,
|
2020-01-06 17:06:16 +00:00
|
|
|
ATTR_ICON,
|
|
|
|
ATTR_LABEL,
|
2020-03-09 18:54:43 +00:00
|
|
|
ATTR_MAGENTA_DRUM_COUNTER,
|
|
|
|
ATTR_MAGENTA_DRUM_REMAINING_LIFE,
|
|
|
|
ATTR_MAGENTA_DRUM_REMAINING_PAGES,
|
2020-01-06 17:06:16 +00:00
|
|
|
ATTR_MANUFACTURER,
|
|
|
|
ATTR_UNIT,
|
2020-08-26 08:55:57 +00:00
|
|
|
ATTR_UPTIME,
|
2020-03-09 18:54:43 +00:00
|
|
|
ATTR_YELLOW_DRUM_COUNTER,
|
|
|
|
ATTR_YELLOW_DRUM_REMAINING_LIFE,
|
|
|
|
ATTR_YELLOW_DRUM_REMAINING_PAGES,
|
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"
|
2020-03-18 03:21:56 +00:00
|
|
|
ATTR_FIRMWARE = "firmware"
|
|
|
|
ATTR_MODEL = "model"
|
2020-03-09 18:54:43 +00:00
|
|
|
ATTR_REMAINING_PAGES = "remaining_pages"
|
2020-03-18 03:21:56 +00:00
|
|
|
ATTR_SERIAL = "serial"
|
2020-03-09 18:54:43 +00:00
|
|
|
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Add Brother entities from a config_entry."""
|
2021-02-12 17:11:35 +00:00
|
|
|
coordinator = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
sensors = []
|
|
|
|
|
|
|
|
device_info = {
|
2020-03-18 03:21:56 +00:00
|
|
|
"identifiers": {(DOMAIN, coordinator.data[ATTR_SERIAL])},
|
|
|
|
"name": coordinator.data[ATTR_MODEL],
|
2020-01-06 17:06:16 +00:00
|
|
|
"manufacturer": ATTR_MANUFACTURER,
|
2020-03-18 03:21:56 +00:00
|
|
|
"model": coordinator.data[ATTR_MODEL],
|
|
|
|
"sw_version": coordinator.data.get(ATTR_FIRMWARE),
|
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 11:37:16 +00:00
|
|
|
class BrotherPrinterSensor(SensorEntity, CoordinatorEntity):
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Define an Brother Printer sensor."""
|
|
|
|
|
2020-03-18 03:21:56 +00:00
|
|
|
def __init__(self, coordinator, kind, device_info):
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Initialize."""
|
2020-08-30 14:02:12 +00:00
|
|
|
super().__init__(coordinator)
|
2020-03-18 03:21:56 +00:00
|
|
|
self._name = f"{coordinator.data[ATTR_MODEL]} {SENSOR_TYPES[kind][ATTR_LABEL]}"
|
|
|
|
self._unique_id = f"{coordinator.data[ATTR_SERIAL].lower()}_{kind}"
|
2020-01-06 17:06:16 +00:00
|
|
|
self._device_info = device_info
|
|
|
|
self.kind = kind
|
|
|
|
self._attrs = {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
2020-03-18 03:21:56 +00:00
|
|
|
return self._name
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
2020-08-26 08:55:57 +00:00
|
|
|
if self.kind == ATTR_UPTIME:
|
2020-11-28 20:17:16 +00:00
|
|
|
return self.coordinator.data.get(self.kind).isoformat()
|
2020-03-18 03:21:56 +00:00
|
|
|
return self.coordinator.data.get(self.kind)
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2020-08-26 08:55:57 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""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-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Return the state attributes."""
|
2020-03-09 18:54:43 +00:00
|
|
|
remaining_pages = None
|
|
|
|
drum_counter = None
|
2020-01-06 17:06:16 +00:00
|
|
|
if self.kind == ATTR_DRUM_REMAINING_LIFE:
|
2020-03-09 18:54:43 +00:00
|
|
|
remaining_pages = ATTR_DRUM_REMAINING_PAGES
|
|
|
|
drum_counter = ATTR_DRUM_COUNTER
|
|
|
|
elif self.kind == ATTR_BLACK_DRUM_REMAINING_LIFE:
|
|
|
|
remaining_pages = ATTR_BLACK_DRUM_REMAINING_PAGES
|
|
|
|
drum_counter = ATTR_BLACK_DRUM_COUNTER
|
|
|
|
elif self.kind == ATTR_CYAN_DRUM_REMAINING_LIFE:
|
|
|
|
remaining_pages = ATTR_CYAN_DRUM_REMAINING_PAGES
|
|
|
|
drum_counter = ATTR_CYAN_DRUM_COUNTER
|
|
|
|
elif self.kind == ATTR_MAGENTA_DRUM_REMAINING_LIFE:
|
|
|
|
remaining_pages = ATTR_MAGENTA_DRUM_REMAINING_PAGES
|
|
|
|
drum_counter = ATTR_MAGENTA_DRUM_COUNTER
|
|
|
|
elif self.kind == ATTR_YELLOW_DRUM_REMAINING_LIFE:
|
|
|
|
remaining_pages = ATTR_YELLOW_DRUM_REMAINING_PAGES
|
|
|
|
drum_counter = ATTR_YELLOW_DRUM_COUNTER
|
|
|
|
if remaining_pages and drum_counter:
|
2020-03-18 03:21:56 +00:00
|
|
|
self._attrs[ATTR_REMAINING_PAGES] = self.coordinator.data.get(
|
|
|
|
remaining_pages
|
|
|
|
)
|
|
|
|
self._attrs[ATTR_COUNTER] = self.coordinator.data.get(drum_counter)
|
2020-01-06 17:06:16 +00:00
|
|
|
return self._attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return SENSOR_TYPES[self.kind][ATTR_ICON]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
return SENSOR_TYPES[self.kind][ATTR_UNIT]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device info."""
|
|
|
|
return self._device_info
|
|
|
|
|
2020-03-18 03:21:56 +00:00
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self):
|
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
2020-11-22 21:42:16 +00:00
|
|
|
return SENSOR_TYPES[self.kind][ATTR_ENABLED]
|