Bump `brother` library to version 1.0.0 (#49547)

* Bump brother library

* Improve attributes generation
pull/49553/head
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):
"""Update data via library."""
try:
await self.brother.async_update()
data = await self.brother.async_update()
except (ConnectionError, SnmpError, UnsupportedModel) as error:
raise UpdateFailed(error) from error
return self.brother.data
return data

View File

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

View File

@ -3,7 +3,7 @@
"name": "Brother Printer",
"documentation": "https://www.home-assistant.io/integrations/brother",
"codeowners": ["@bieniu"],
"requirements": ["brother==0.2.2"],
"requirements": ["brother==1.0.0"],
"zeroconf": [
{
"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 .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_ICON,
ATTR_LABEL,
ATTR_MAGENTA_DRUM_COUNTER,
ATTR_MAGENTA_DRUM_REMAINING_LIFE,
ATTR_MAGENTA_DRUM_REMAINING_PAGES,
ATTR_MANUFACTURER,
ATTR_UNIT,
ATTR_UPTIME,
ATTR_YELLOW_DRUM_COUNTER,
ATTR_YELLOW_DRUM_REMAINING_LIFE,
ATTR_YELLOW_DRUM_REMAINING_PAGES,
ATTRS_MAP,
DATA_CONFIG_ENTRY,
DOMAIN,
SENSOR_TYPES,
)
ATTR_COUNTER = "counter"
ATTR_FIRMWARE = "firmware"
ATTR_MODEL = "model"
ATTR_REMAINING_PAGES = "remaining_pages"
ATTR_SERIAL = "serial"
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 = []
device_info = {
"identifiers": {(DOMAIN, coordinator.data[ATTR_SERIAL])},
"name": coordinator.data[ATTR_MODEL],
"identifiers": {(DOMAIN, coordinator.data.serial)},
"name": coordinator.data.model,
"manufacturer": ATTR_MANUFACTURER,
"model": coordinator.data[ATTR_MODEL],
"sw_version": coordinator.data.get(ATTR_FIRMWARE),
"model": coordinator.data.model,
"sw_version": getattr(coordinator.data, "firmware", None),
}
for sensor in SENSOR_TYPES:
@ -63,8 +46,8 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator, kind, device_info):
"""Initialize."""
super().__init__(coordinator)
self._name = f"{coordinator.data[ATTR_MODEL]} {SENSOR_TYPES[kind][ATTR_LABEL]}"
self._unique_id = f"{coordinator.data[ATTR_SERIAL].lower()}_{kind}"
self._name = f"{coordinator.data.model} {SENSOR_TYPES[kind][ATTR_LABEL]}"
self._unique_id = f"{coordinator.data.serial.lower()}_{kind}"
self._device_info = device_info
self.kind = kind
self._attrs = {}
@ -78,8 +61,8 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
def state(self):
"""Return the state."""
if self.kind == ATTR_UPTIME:
return self.coordinator.data.get(self.kind).isoformat()
return self.coordinator.data.get(self.kind)
return getattr(self.coordinator.data, self.kind).isoformat()
return getattr(self.coordinator.data, self.kind)
@property
def device_class(self):
@ -91,28 +74,12 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
@property
def extra_state_attributes(self):
"""Return the state attributes."""
remaining_pages = 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
remaining_pages, drum_counter = ATTRS_MAP.get(self.kind, (None, None))
if remaining_pages and drum_counter:
self._attrs[ATTR_REMAINING_PAGES] = self.coordinator.data.get(
remaining_pages
self._attrs[ATTR_REMAINING_PAGES] = getattr(
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
@property

View File

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

View File

@ -223,7 +223,7 @@ bravia-tv==1.0.8
broadlink==0.17.0
# homeassistant.components.brother
brother==0.2.2
brother==1.0.0
# homeassistant.components.bsblan
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."""
await init_integration(hass)
data = json.loads(load_fixture("brother_printer_data.json"))
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(
"homeassistant",
"update_entity",