diff --git a/homeassistant/components/brother/__init__.py b/homeassistant/components/brother/__init__.py index cd5a8b444b3..f3c7678f3e3 100644 --- a/homeassistant/components/brother/__init__.py +++ b/homeassistant/components/brother/__init__.py @@ -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 diff --git a/homeassistant/components/brother/const.py b/homeassistant/components/brother/const.py index 07843b0f3d0..2df14031f94 100644 --- a/homeassistant/components/brother/const.py +++ b/homeassistant/components/brother/const.py @@ -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", diff --git a/homeassistant/components/brother/manifest.json b/homeassistant/components/brother/manifest.json index dd33046a065..e2c1d4e9aff 100644 --- a/homeassistant/components/brother/manifest.json +++ b/homeassistant/components/brother/manifest.json @@ -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.", diff --git a/homeassistant/components/brother/sensor.py b/homeassistant/components/brother/sensor.py index 0b614ffa582..ca76932cd95 100644 --- a/homeassistant/components/brother/sensor.py +++ b/homeassistant/components/brother/sensor.py @@ -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 diff --git a/requirements_all.txt b/requirements_all.txt index f7ae5b4a141..c78bd272292 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2bdab44dc49..a7260aadb25 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -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 diff --git a/tests/components/brother/test_sensor.py b/tests/components/brother/test_sensor.py index ab48721dec5..49f7340a37a 100644 --- a/tests/components/brother/test_sensor.py +++ b/tests/components/brother/test_sensor.py @@ -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",