Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the coordinator logic to the BroadlinkEntity class #53571

Merged
merged 2 commits into from Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 31 additions & 1 deletion homeassistant/components/broadlink/entity.py
Expand Up @@ -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):
bdraco marked this conversation as resolved.
Show resolved Hide resolved
"""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):
Expand Down
10 changes: 1 addition & 9 deletions homeassistant/components/broadlink/remote.py
Expand Up @@ -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
Expand Down Expand Up @@ -189,14 +188,7 @@ async def async_added_to_hass(self):
"""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."""
Expand Down
19 changes: 3 additions & 16 deletions homeassistant/components/broadlink/sensor.py
Expand Up @@ -13,7 +13,6 @@
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
Expand Down Expand Up @@ -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]
Expand All @@ -91,17 +89,6 @@ def __init__(self, device, monitored_condition):
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]
40 changes: 10 additions & 30 deletions homeassistant/components/broadlink/switch.py
Expand Up @@ -24,7 +24,6 @@
CONF_TYPE,
STATE_ON,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity

Expand Down Expand Up @@ -143,27 +142,17 @@ def __init__(self, device, command_on, command_off):
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."""
Expand Down Expand Up @@ -233,12 +222,9 @@ def __init__(self, device, *args, **kwargs):
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):
Expand All @@ -254,12 +240,9 @@ def __init__(self, device, slot):
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."""
Expand Down Expand Up @@ -288,12 +271,9 @@ def __init__(self, device, slot):
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."""
Expand Down