Move the coordinator logic to the BroadlinkEntity class (#53571)
This commit is contained in:
parent
37c841956f
commit
43d3b6c2a2
4 changed files with 45 additions and 56 deletions
|
@ -12,8 +12,38 @@ class BroadlinkEntity(Entity):
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, device):
|
def __init__(self, device):
|
||||||
"""Initialize the device."""
|
"""Initialize the entity."""
|
||||||
self._device = device
|
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
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
|
|
|
@ -119,7 +119,6 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
def __init__(self, device, codes, flags):
|
def __init__(self, device, codes, flags):
|
||||||
"""Initialize the remote."""
|
"""Initialize the remote."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._coordinator = device.update_manager.coordinator
|
|
||||||
self._code_storage = codes
|
self._code_storage = codes
|
||||||
self._flag_storage = flags
|
self._flag_storage = flags
|
||||||
self._storage_loaded = False
|
self._storage_loaded = False
|
||||||
|
@ -189,14 +188,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
"""Call when the remote is added to hass."""
|
"""Call when the remote is added to hass."""
|
||||||
state = await self.async_get_last_state()
|
state = await self.async_get_last_state()
|
||||||
self._attr_is_on = state is None or state.state != STATE_OFF
|
self._attr_is_on = state is None or state.state != STATE_OFF
|
||||||
|
await super().async_added_to_hass()
|
||||||
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()
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn on the remote."""
|
"""Turn on the remote."""
|
||||||
|
|
|
@ -13,7 +13,6 @@ from homeassistant.components.sensor import (
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_HOST, PERCENTAGE, POWER_WATT, TEMP_CELSIUS
|
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 homeassistant.helpers import config_validation as cv
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
@ -81,7 +80,6 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
||||||
def __init__(self, device, monitored_condition):
|
def __init__(self, device, monitored_condition):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._coordinator = device.update_manager.coordinator
|
|
||||||
self._monitored_condition = monitored_condition
|
self._monitored_condition = monitored_condition
|
||||||
|
|
||||||
self._attr_device_class = SENSOR_TYPES[monitored_condition][2]
|
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_unique_id = f"{device.unique_id}-{monitored_condition}"
|
||||||
self._attr_unit_of_measurement = SENSOR_TYPES[monitored_condition][1]
|
self._attr_unit_of_measurement = SENSOR_TYPES[monitored_condition][1]
|
||||||
|
|
||||||
@callback
|
def _update_state(self, data):
|
||||||
def update_data(self):
|
"""Update the state of the entity."""
|
||||||
"""Update data."""
|
self._attr_state = data[self._monitored_condition]
|
||||||
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()
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ from homeassistant.const import (
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
|
@ -143,27 +142,17 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._command_on = command_on
|
self._command_on = command_on
|
||||||
self._command_off = command_off
|
self._command_off = command_off
|
||||||
self._coordinator = device.update_manager.coordinator
|
|
||||||
|
|
||||||
self._attr_assumed_state = True
|
self._attr_assumed_state = True
|
||||||
self._attr_device_class = DEVICE_CLASS_SWITCH
|
self._attr_device_class = DEVICE_CLASS_SWITCH
|
||||||
self._attr_name = f"{device.name} Switch"
|
self._attr_name = f"{device.name} Switch"
|
||||||
self._attr_unique_id = device.unique_id
|
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):
|
async def async_added_to_hass(self):
|
||||||
"""Call when the switch is added to hass."""
|
"""Call when the switch is added to hass."""
|
||||||
state = await self.async_get_last_state()
|
state = await self.async_get_last_state()
|
||||||
self._attr_is_on = state is not None and state.state == STATE_ON
|
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))
|
await super().async_added_to_hass()
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update the switch."""
|
|
||||||
await self._coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
|
@ -233,12 +222,9 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
|
||||||
super().__init__(device, *args, **kwargs)
|
super().__init__(device, *args, **kwargs)
|
||||||
self._attr_is_on = self._coordinator.data["pwr"]
|
self._attr_is_on = self._coordinator.data["pwr"]
|
||||||
|
|
||||||
@callback
|
def _update_state(self, data):
|
||||||
def update_data(self):
|
"""Update the state of the entity."""
|
||||||
"""Update data."""
|
self._attr_is_on = data["pwr"]
|
||||||
if self._coordinator.last_update_success:
|
|
||||||
self._attr_is_on = self._coordinator.data["pwr"]
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkMP1Slot(BroadlinkSwitch):
|
class BroadlinkMP1Slot(BroadlinkSwitch):
|
||||||
|
@ -254,12 +240,9 @@ class BroadlinkMP1Slot(BroadlinkSwitch):
|
||||||
self._attr_name = f"{device.name} S{slot}"
|
self._attr_name = f"{device.name} S{slot}"
|
||||||
self._attr_unique_id = f"{device.unique_id}-s{slot}"
|
self._attr_unique_id = f"{device.unique_id}-s{slot}"
|
||||||
|
|
||||||
@callback
|
def _update_state(self, data):
|
||||||
def update_data(self):
|
"""Update the state of the entity."""
|
||||||
"""Update data."""
|
self._attr_is_on = data[f"s{self._slot}"]
|
||||||
if self._coordinator.last_update_success:
|
|
||||||
self._attr_is_on = self._coordinator.data[f"s{self._slot}"]
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
async def _async_send_packet(self, packet):
|
async def _async_send_packet(self, packet):
|
||||||
"""Send a packet to the device."""
|
"""Send a packet to the device."""
|
||||||
|
@ -288,12 +271,9 @@ class BroadlinkBG1Slot(BroadlinkSwitch):
|
||||||
self._attr_device_class = DEVICE_CLASS_OUTLET
|
self._attr_device_class = DEVICE_CLASS_OUTLET
|
||||||
self._attr_unique_id = f"{device.unique_id}-s{slot}"
|
self._attr_unique_id = f"{device.unique_id}-s{slot}"
|
||||||
|
|
||||||
@callback
|
def _update_state(self, data):
|
||||||
def update_data(self):
|
"""Update the state of the entity."""
|
||||||
"""Update data."""
|
self._attr_is_on = data[f"pwr{self._slot}"]
|
||||||
if self._coordinator.last_update_success:
|
|
||||||
self._attr_is_on = self._coordinator.data[f"pwr{self._slot}"]
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
async def _async_send_packet(self, packet):
|
async def _async_send_packet(self, packet):
|
||||||
"""Send a packet to the device."""
|
"""Send a packet to the device."""
|
||||||
|
|
Loading…
Add table
Reference in a new issue