Update brother to use CoordinatorEntity (#39418)

This commit is contained in:
springstan 2020-08-30 16:02:12 +02:00 committed by GitHub
parent 64513c8c9a
commit 29c1f873eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,7 +3,7 @@ from datetime import timedelta
import logging import logging
from homeassistant.const import DEVICE_CLASS_TIMESTAMP from homeassistant.const import DEVICE_CLASS_TIMESTAMP
from homeassistant.helpers.entity import Entity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from .const import ( from .const import (
@ -60,15 +60,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(sensors, False) async_add_entities(sensors, False)
class BrotherPrinterSensor(Entity): class BrotherPrinterSensor(CoordinatorEntity):
"""Define an Brother Printer sensor.""" """Define an Brother Printer sensor."""
def __init__(self, coordinator, kind, device_info): def __init__(self, coordinator, kind, device_info):
"""Initialize.""" """Initialize."""
super().__init__(coordinator)
self._name = f"{coordinator.data[ATTR_MODEL]} {SENSOR_TYPES[kind][ATTR_LABEL]}" self._name = f"{coordinator.data[ATTR_MODEL]} {SENSOR_TYPES[kind][ATTR_LABEL]}"
self._unique_id = f"{coordinator.data[ATTR_SERIAL].lower()}_{kind}" self._unique_id = f"{coordinator.data[ATTR_SERIAL].lower()}_{kind}"
self._device_info = device_info self._device_info = device_info
self.coordinator = coordinator
self.kind = kind self.kind = kind
self._attrs = {} self._attrs = {}
@ -134,16 +134,6 @@ class BrotherPrinterSensor(Entity):
"""Return the unit the value is expressed in.""" """Return the unit the value is expressed in."""
return SENSOR_TYPES[self.kind][ATTR_UNIT] return SENSOR_TYPES[self.kind][ATTR_UNIT]
@property
def available(self):
"""Return True if entity is available."""
return self.coordinator.last_update_success
@property
def should_poll(self):
"""Return the polling requirement of the entity."""
return False
@property @property
def device_info(self): def device_info(self):
"""Return the device info.""" """Return the device info."""
@ -153,13 +143,3 @@ class BrotherPrinterSensor(Entity):
def entity_registry_enabled_default(self): def entity_registry_enabled_default(self):
"""Return if the entity should be enabled when first added to the entity registry.""" """Return if the entity should be enabled when first added to the entity registry."""
return True return True
async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications."""
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_update(self):
"""Update Brother entity."""
await self.coordinator.async_request_refresh()