mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use entity class attributes for Broadlink (#53058)
* Clanup broadlink * rework * tweak * fix using wrong attribute * tweak * revert device info
This commit is contained in:
parent
0803b2aecd
commit
800f7fe3a5
@ -1,11 +1,12 @@
|
|||||||
"""Broadlink entities."""
|
"""Broadlink entities."""
|
||||||
|
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkEntity:
|
class BroadlinkEntity(Entity):
|
||||||
"""Representation of a Broadlink entity."""
|
"""Representation of a Broadlink entity."""
|
||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
@ -127,10 +127,10 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
|||||||
self._flags = defaultdict(int)
|
self._flags = defaultdict(int)
|
||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
self._attr_name = f"{self._device.name} Remote"
|
self._attr_name = f"{device.name} Remote"
|
||||||
self._attr_is_on = True
|
self._attr_is_on = True
|
||||||
self._attr_supported_features = SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND
|
self._attr_supported_features = SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND
|
||||||
self._attr_unique_id = self._device.unique_id
|
self._attr_unique_id = device.unique_id
|
||||||
|
|
||||||
def _extract_codes(self, commands, device=None):
|
def _extract_codes(self, commands, device=None):
|
||||||
"""Extract a list of codes.
|
"""Extract a list of codes.
|
||||||
|
@ -77,14 +77,12 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
|||||||
self._coordinator = device.update_manager.coordinator
|
self._coordinator = device.update_manager.coordinator
|
||||||
self._monitored_condition = monitored_condition
|
self._monitored_condition = monitored_condition
|
||||||
|
|
||||||
self._attr_device_class = SENSOR_TYPES[self._monitored_condition][2]
|
self._attr_device_class = SENSOR_TYPES[monitored_condition][2]
|
||||||
self._attr_name = (
|
self._attr_name = f"{device.name} {SENSOR_TYPES[monitored_condition][0]}"
|
||||||
f"{self._device.name} {SENSOR_TYPES[self._monitored_condition][0]}"
|
self._attr_state_class = SENSOR_TYPES[monitored_condition][3]
|
||||||
)
|
|
||||||
self._attr_state_class = SENSOR_TYPES[self._monitored_condition][3]
|
|
||||||
self._attr_state = self._coordinator.data[monitored_condition]
|
self._attr_state = self._coordinator.data[monitored_condition]
|
||||||
self._attr_unique_id = f"{self._device.unique_id}-{self._monitored_condition}"
|
self._attr_unique_id = f"{device.unique_id}-{monitored_condition}"
|
||||||
self._attr_unit_of_measurement = SENSOR_TYPES[self._monitored_condition][1]
|
self._attr_unit_of_measurement = SENSOR_TYPES[monitored_condition][1]
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
|
@ -135,22 +135,20 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
||||||
"""Representation of a Broadlink switch."""
|
"""Representation of a Broadlink switch."""
|
||||||
|
|
||||||
|
_attr_assumed_state = True
|
||||||
|
_attr_device_class = DEVICE_CLASS_SWITCH
|
||||||
|
|
||||||
def __init__(self, device, command_on, command_off):
|
def __init__(self, device, command_on, command_off):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
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._coordinator = device.update_manager.coordinator
|
||||||
self._state = None
|
|
||||||
|
|
||||||
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"{self._device.name} Switch"
|
self._attr_name = f"{device.name} Switch"
|
||||||
|
self._attr_unique_id = device.unique_id
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Return True if the switch is on."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
@ -159,9 +157,8 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
|||||||
|
|
||||||
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."""
|
||||||
if self._state is None:
|
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._state = state is not None and state.state == STATE_ON
|
|
||||||
self.async_on_remove(self._coordinator.async_add_listener(self.update_data))
|
self.async_on_remove(self._coordinator.async_add_listener(self.update_data))
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
@ -171,13 +168,13 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
|||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
if await self._async_send_packet(self._command_on):
|
if await self._async_send_packet(self._command_on):
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Turn off the switch."""
|
"""Turn off the switch."""
|
||||||
if await self._async_send_packet(self._command_off):
|
if await self._async_send_packet(self._command_off):
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@ -229,46 +226,41 @@ class BroadlinkSP1Switch(BroadlinkSwitch):
|
|||||||
class BroadlinkSP2Switch(BroadlinkSP1Switch):
|
class BroadlinkSP2Switch(BroadlinkSP1Switch):
|
||||||
"""Representation of a Broadlink SP2 switch."""
|
"""Representation of a Broadlink SP2 switch."""
|
||||||
|
|
||||||
|
_attr_assumed_state = False
|
||||||
|
|
||||||
def __init__(self, device, *args, **kwargs):
|
def __init__(self, device, *args, **kwargs):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(device, *args, **kwargs)
|
super().__init__(device, *args, **kwargs)
|
||||||
self._state = self._coordinator.data["pwr"]
|
self._attr_is_on = self._coordinator.data["pwr"]
|
||||||
self._load_power = self._coordinator.data.get("power")
|
self._attr_current_power_w = self._coordinator.data.get("power")
|
||||||
|
|
||||||
self._attr_assumed_state = False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def current_power_w(self):
|
|
||||||
"""Return the current power usage in Watt."""
|
|
||||||
return self._load_power
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
"""Update data."""
|
"""Update data."""
|
||||||
if self._coordinator.last_update_success:
|
if self._coordinator.last_update_success:
|
||||||
self._state = self._coordinator.data["pwr"]
|
self._attr_is_on = self._coordinator.data["pwr"]
|
||||||
self._load_power = self._coordinator.data.get("power")
|
self._attr_current_power_w = self._coordinator.data.get("power")
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkMP1Slot(BroadlinkSwitch):
|
class BroadlinkMP1Slot(BroadlinkSwitch):
|
||||||
"""Representation of a Broadlink MP1 slot."""
|
"""Representation of a Broadlink MP1 slot."""
|
||||||
|
|
||||||
|
_attr_assumed_state = False
|
||||||
|
|
||||||
def __init__(self, device, slot):
|
def __init__(self, device, slot):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(device, 1, 0)
|
super().__init__(device, 1, 0)
|
||||||
self._slot = slot
|
self._slot = slot
|
||||||
self._state = self._coordinator.data[f"s{slot}"]
|
self._attr_is_on = self._coordinator.data[f"s{slot}"]
|
||||||
|
self._attr_name = f"{device.name} S{slot}"
|
||||||
self._attr_name = f"{self._device.name} S{self._slot}"
|
self._attr_unique_id = f"{device.unique_id}-s{slot}"
|
||||||
self._attr_unique_id = f"{self._device.unique_id}-s{self._slot}"
|
|
||||||
self._attr_assumed_state = False
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
"""Update data."""
|
"""Update data."""
|
||||||
if self._coordinator.last_update_success:
|
if self._coordinator.last_update_success:
|
||||||
self._state = self._coordinator.data[f"s{self._slot}"]
|
self._attr_is_on = self._coordinator.data[f"s{self._slot}"]
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def _async_send_packet(self, packet):
|
async def _async_send_packet(self, packet):
|
||||||
@ -286,22 +278,23 @@ class BroadlinkMP1Slot(BroadlinkSwitch):
|
|||||||
class BroadlinkBG1Slot(BroadlinkSwitch):
|
class BroadlinkBG1Slot(BroadlinkSwitch):
|
||||||
"""Representation of a Broadlink BG1 slot."""
|
"""Representation of a Broadlink BG1 slot."""
|
||||||
|
|
||||||
|
_attr_assumed_state = False
|
||||||
|
|
||||||
def __init__(self, device, slot):
|
def __init__(self, device, slot):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(device, 1, 0)
|
super().__init__(device, 1, 0)
|
||||||
self._slot = slot
|
self._slot = slot
|
||||||
self._state = self._coordinator.data[f"pwr{slot}"]
|
self._attr_is_on = self._coordinator.data[f"pwr{slot}"]
|
||||||
|
|
||||||
self._attr_name = f"{self._device.name} S{self._slot}"
|
self._attr_name = f"{device.name} S{slot}"
|
||||||
self._attr_device_class = DEVICE_CLASS_OUTLET
|
self._attr_device_class = DEVICE_CLASS_OUTLET
|
||||||
self._attr_unique_id = f"{self._device.unique_id}-s{self._slot}"
|
self._attr_unique_id = f"{device.unique_id}-s{slot}"
|
||||||
self._attr_assumed_state = False
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
"""Update data."""
|
"""Update data."""
|
||||||
if self._coordinator.last_update_success:
|
if self._coordinator.last_update_success:
|
||||||
self._state = self._coordinator.data[f"pwr{self._slot}"]
|
self._attr_is_on = self._coordinator.data[f"pwr{self._slot}"]
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def _async_send_packet(self, packet):
|
async def _async_send_packet(self, packet):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user