Bump brother library to version 1.0.0 (#49547)

* Bump brother library

* Improve attributes generation
This commit is contained in:
Maciej Bieniek 2021-04-22 13:20:14 +02:00 committed by GitHub
parent f67c0ce8bb
commit e75233b279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 52 deletions

View File

@ -81,7 +81,7 @@ class BrotherDataUpdateCoordinator(DataUpdateCoordinator):
async def _async_update_data(self): async def _async_update_data(self):
"""Update data via library.""" """Update data via library."""
try: try:
await self.brother.async_update() data = await self.brother.async_update()
except (ConnectionError, SnmpError, UnsupportedModel) as error: except (ConnectionError, SnmpError, UnsupportedModel) as error:
raise UpdateFailed(error) from error raise UpdateFailed(error) from error
return self.brother.data return data

View File

@ -50,6 +50,26 @@ PRINTER_TYPES = ["laser", "ink"]
SNMP = "snmp" SNMP = "snmp"
ATTRS_MAP = {
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,
),
}
SENSOR_TYPES = { SENSOR_TYPES = {
ATTR_STATUS: { ATTR_STATUS: {
ATTR_ICON: "mdi:printer", ATTR_ICON: "mdi:printer",

View File

@ -3,7 +3,7 @@
"name": "Brother Printer", "name": "Brother Printer",
"documentation": "https://www.home-assistant.io/integrations/brother", "documentation": "https://www.home-assistant.io/integrations/brother",
"codeowners": ["@bieniu"], "codeowners": ["@bieniu"],
"requirements": ["brother==0.2.2"], "requirements": ["brother==1.0.0"],
"zeroconf": [ "zeroconf": [
{ {
"type": "_printer._tcp.local.", "type": "_printer._tcp.local.",

View File

@ -4,37 +4,20 @@ from homeassistant.const import DEVICE_CLASS_TIMESTAMP
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ( from .const import (
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,
ATTR_DRUM_COUNTER,
ATTR_DRUM_REMAINING_LIFE,
ATTR_DRUM_REMAINING_PAGES,
ATTR_ENABLED, ATTR_ENABLED,
ATTR_ICON, ATTR_ICON,
ATTR_LABEL, ATTR_LABEL,
ATTR_MAGENTA_DRUM_COUNTER,
ATTR_MAGENTA_DRUM_REMAINING_LIFE,
ATTR_MAGENTA_DRUM_REMAINING_PAGES,
ATTR_MANUFACTURER, ATTR_MANUFACTURER,
ATTR_UNIT, ATTR_UNIT,
ATTR_UPTIME, ATTR_UPTIME,
ATTR_YELLOW_DRUM_COUNTER, ATTRS_MAP,
ATTR_YELLOW_DRUM_REMAINING_LIFE,
ATTR_YELLOW_DRUM_REMAINING_PAGES,
DATA_CONFIG_ENTRY, DATA_CONFIG_ENTRY,
DOMAIN, DOMAIN,
SENSOR_TYPES, SENSOR_TYPES,
) )
ATTR_COUNTER = "counter" ATTR_COUNTER = "counter"
ATTR_FIRMWARE = "firmware"
ATTR_MODEL = "model"
ATTR_REMAINING_PAGES = "remaining_pages" ATTR_REMAINING_PAGES = "remaining_pages"
ATTR_SERIAL = "serial"
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
@ -44,11 +27,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
sensors = [] sensors = []
device_info = { device_info = {
"identifiers": {(DOMAIN, coordinator.data[ATTR_SERIAL])}, "identifiers": {(DOMAIN, coordinator.data.serial)},
"name": coordinator.data[ATTR_MODEL], "name": coordinator.data.model,
"manufacturer": ATTR_MANUFACTURER, "manufacturer": ATTR_MANUFACTURER,
"model": coordinator.data[ATTR_MODEL], "model": coordinator.data.model,
"sw_version": coordinator.data.get(ATTR_FIRMWARE), "sw_version": getattr(coordinator.data, "firmware", None),
} }
for sensor in SENSOR_TYPES: for sensor in SENSOR_TYPES:
@ -63,8 +46,8 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator, kind, device_info): def __init__(self, coordinator, kind, device_info):
"""Initialize.""" """Initialize."""
super().__init__(coordinator) super().__init__(coordinator)
self._name = f"{coordinator.data[ATTR_MODEL]} {SENSOR_TYPES[kind][ATTR_LABEL]}" self._name = f"{coordinator.data.model} {SENSOR_TYPES[kind][ATTR_LABEL]}"
self._unique_id = f"{coordinator.data[ATTR_SERIAL].lower()}_{kind}" self._unique_id = f"{coordinator.data.serial.lower()}_{kind}"
self._device_info = device_info self._device_info = device_info
self.kind = kind self.kind = kind
self._attrs = {} self._attrs = {}
@ -78,8 +61,8 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
def state(self): def state(self):
"""Return the state.""" """Return the state."""
if self.kind == ATTR_UPTIME: if self.kind == ATTR_UPTIME:
return self.coordinator.data.get(self.kind).isoformat() return getattr(self.coordinator.data, self.kind).isoformat()
return self.coordinator.data.get(self.kind) return getattr(self.coordinator.data, self.kind)
@property @property
def device_class(self): def device_class(self):
@ -91,28 +74,12 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
remaining_pages = None remaining_pages, drum_counter = ATTRS_MAP.get(self.kind, (None, None))
drum_counter = None
if self.kind == ATTR_DRUM_REMAINING_LIFE:
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: if remaining_pages and drum_counter:
self._attrs[ATTR_REMAINING_PAGES] = self.coordinator.data.get( self._attrs[ATTR_REMAINING_PAGES] = getattr(
remaining_pages self.coordinator.data, remaining_pages
) )
self._attrs[ATTR_COUNTER] = self.coordinator.data.get(drum_counter) self._attrs[ATTR_COUNTER] = getattr(self.coordinator.data, drum_counter)
return self._attrs return self._attrs
@property @property

View File

@ -390,7 +390,7 @@ bravia-tv==1.0.8
broadlink==0.17.0 broadlink==0.17.0
# homeassistant.components.brother # homeassistant.components.brother
brother==0.2.2 brother==1.0.0
# homeassistant.components.brottsplatskartan # homeassistant.components.brottsplatskartan
brottsplatskartan==0.0.1 brottsplatskartan==0.0.1

View File

@ -223,7 +223,7 @@ bravia-tv==1.0.8
broadlink==0.17.0 broadlink==0.17.0
# homeassistant.components.brother # homeassistant.components.brother
brother==0.2.2 brother==1.0.0
# homeassistant.components.bsblan # homeassistant.components.bsblan
bsblan==0.4.0 bsblan==0.4.0

View File

@ -289,8 +289,12 @@ async def test_manual_update_entity(hass):
"""Test manual update entity via service homeasasistant/update_entity.""" """Test manual update entity via service homeasasistant/update_entity."""
await init_integration(hass) await init_integration(hass)
data = json.loads(load_fixture("brother_printer_data.json"))
await async_setup_component(hass, "homeassistant", {}) await async_setup_component(hass, "homeassistant", {})
with patch("homeassistant.components.brother.Brother.async_update") as mock_update: with patch(
"homeassistant.components.brother.Brother.async_update", return_value=data
) as mock_update:
await hass.services.async_call( await hass.services.async_call(
"homeassistant", "homeassistant",
"update_entity", "update_entity",