Move the coordinator logic to the BroadlinkEntity class ()

This commit is contained in:
Felipe Martins Diel 2021-07-27 15:53:29 -03:00 committed by GitHub
parent 37c841956f
commit 43d3b6c2a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 56 deletions
homeassistant/components/broadlink

View file

@ -12,8 +12,38 @@ class BroadlinkEntity(Entity):
_attr_should_poll = False
def __init__(self, device):
"""Initialize the device."""
"""Initialize the entity."""
self._device = device
self._coordinator = device.update_manager.coordinator
async def async_added_to_hass(self):
"""Call when the entity is added to hass."""
self.async_on_remove(self._coordinator.async_add_listener(self._recv_data))
async def async_update(self):
"""Update the state of the entity."""
await self._coordinator.async_request_refresh()
def _recv_data(self):
"""Receive data from the update coordinator.
This event listener should be called by the coordinator whenever
there is an update available.
It works as a template for the _update_state() method, which should
be overridden by child classes in order to update the state of the
entities, when applicable.
"""
if self._coordinator.last_update_success:
self._update_state(self._coordinator.data)
self.async_write_ha_state()
def _update_state(self, data):
"""Update the state of the entity.
This method should be overridden by child classes in order to
internalize state and attributes received from the coordinator.
"""
@property
def available(self):

View file

@ -119,7 +119,6 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
def __init__(self, device, codes, flags):
"""Initialize the remote."""
super().__init__(device)
self._coordinator = device.update_manager.coordinator
self._code_storage = codes
self._flag_storage = flags
self._storage_loaded = False
@ -189,14 +188,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
"""Call when the remote is added to hass."""
state = await self.async_get_last_state()
self._attr_is_on = state is None or state.state != STATE_OFF
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_update(self):
"""Update the remote."""
await self._coordinator.async_request_refresh()
await super().async_added_to_hass()
async def async_turn_on(self, **kwargs):
"""Turn on the remote."""

View file

@ -13,7 +13,6 @@ from homeassistant.components.sensor import (
SensorEntity,
)
from homeassistant.const import CONF_HOST, PERCENTAGE, POWER_WATT, TEMP_CELSIUS
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from .const import DOMAIN
@ -81,7 +80,6 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity):
def __init__(self, device, monitored_condition):
"""Initialize the sensor."""
super().__init__(device)
self._coordinator = device.update_manager.coordinator
self._monitored_condition = monitored_condition
self._attr_device_class = SENSOR_TYPES[monitored_condition][2]
@ -91,17 +89,6 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity):
self._attr_unique_id = f"{device.unique_id}-{monitored_condition}"
self._attr_unit_of_measurement = SENSOR_TYPES[monitored_condition][1]
@callback
def update_data(self):
"""Update data."""
if self._coordinator.last_update_success:
self._attr_state = self._coordinator.data[self._monitored_condition]
self.async_write_ha_state()
async def async_added_to_hass(self):
"""Call when the sensor is added to hass."""
self.async_on_remove(self._coordinator.async_add_listener(self.update_data))
async def async_update(self):
"""Update the sensor."""
await self._coordinator.async_request_refresh()
def _update_state(self, data):
"""Update the state of the entity."""
self._attr_state = data[self._monitored_condition]

View file

@ -24,7 +24,6 @@ from homeassistant.const import (
CONF_TYPE,
STATE_ON,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
@ -143,27 +142,17 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
super().__init__(device)
self._command_on = command_on
self._command_off = command_off
self._coordinator = device.update_manager.coordinator
self._attr_assumed_state = True
self._attr_device_class = DEVICE_CLASS_SWITCH
self._attr_name = f"{device.name} Switch"
self._attr_unique_id = device.unique_id
@callback
def update_data(self):
"""Update data."""
self.async_write_ha_state()
async def async_added_to_hass(self):
"""Call when the switch is added to hass."""
state = await self.async_get_last_state()
self._attr_is_on = state is not None and state.state == STATE_ON
self.async_on_remove(self._coordinator.async_add_listener(self.update_data))
async def async_update(self):
"""Update the switch."""
await self._coordinator.async_request_refresh()
await super().async_added_to_hass()
async def async_turn_on(self, **kwargs):
"""Turn on the switch."""
@ -233,12 +222,9 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
super().__init__(device, *args, **kwargs)
self._attr_is_on = self._coordinator.data["pwr"]
@callback
def update_data(self):
"""Update data."""
if self._coordinator.last_update_success:
self._attr_is_on = self._coordinator.data["pwr"]
self.async_write_ha_state()
def _update_state(self, data):
"""Update the state of the entity."""
self._attr_is_on = data["pwr"]
class BroadlinkMP1Slot(BroadlinkSwitch):
@ -254,12 +240,9 @@ class BroadlinkMP1Slot(BroadlinkSwitch):
self._attr_name = f"{device.name} S{slot}"
self._attr_unique_id = f"{device.unique_id}-s{slot}"
@callback
def update_data(self):
"""Update data."""
if self._coordinator.last_update_success:
self._attr_is_on = self._coordinator.data[f"s{self._slot}"]
self.async_write_ha_state()
def _update_state(self, data):
"""Update the state of the entity."""
self._attr_is_on = data[f"s{self._slot}"]
async def _async_send_packet(self, packet):
"""Send a packet to the device."""
@ -288,12 +271,9 @@ class BroadlinkBG1Slot(BroadlinkSwitch):
self._attr_device_class = DEVICE_CLASS_OUTLET
self._attr_unique_id = f"{device.unique_id}-s{slot}"
@callback
def update_data(self):
"""Update data."""
if self._coordinator.last_update_success:
self._attr_is_on = self._coordinator.data[f"pwr{self._slot}"]
self.async_write_ha_state()
def _update_state(self, data):
"""Update the state of the entity."""
self._attr_is_on = data[f"pwr{self._slot}"]
async def _async_send_packet(self, packet):
"""Send a packet to the device."""