mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Create a base class for broadlink entities (#52132)
* Create a base class for broadlink entities * Update homeassistant/components/broadlink/entity.py Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net> * Update homeassistant/components/broadlink/entity.py * Update homeassistant/components/broadlink/entity.py Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net> * black, remove unused Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net>
This commit is contained in:
parent
d009f06a55
commit
22c8afe637
@ -51,6 +51,11 @@ class BroadlinkDevice:
|
|||||||
"""Return the unique id of the device."""
|
"""Return the unique id of the device."""
|
||||||
return self.config.unique_id
|
return self.config.unique_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mac_address(self):
|
||||||
|
"""Return the mac address of the device."""
|
||||||
|
return self.config.data[CONF_MAC]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def async_update(hass, entry):
|
async def async_update(hass, entry):
|
||||||
"""Update the device and related entities.
|
"""Update the device and related entities.
|
||||||
|
32
homeassistant/components/broadlink/entity.py
Normal file
32
homeassistant/components/broadlink/entity.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
"""Broadlink entities."""
|
||||||
|
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
class BroadlinkEntity:
|
||||||
|
"""Representation of a Broadlink entity."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(self, device):
|
||||||
|
"""Initialize the device."""
|
||||||
|
self._device = device
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return True if the remote is available."""
|
||||||
|
return self._device.update_manager.available
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self):
|
||||||
|
"""Return device info."""
|
||||||
|
return {
|
||||||
|
"identifiers": {(DOMAIN, self._device.unique_id)},
|
||||||
|
"connections": {(dr.CONNECTION_NETWORK_MAC, self._device.mac_address)},
|
||||||
|
"manufacturer": self._device.api.manufacturer,
|
||||||
|
"model": self._device.api.model,
|
||||||
|
"name": self._device.name,
|
||||||
|
"sw_version": self._device.fw_version,
|
||||||
|
}
|
@ -40,6 +40,7 @@ from homeassistant.helpers.storage import Store
|
|||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import BroadlinkEntity
|
||||||
from .helpers import data_packet, import_device
|
from .helpers import data_packet, import_device
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -112,12 +113,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async_add_entities([remote], False)
|
async_add_entities([remote], False)
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkRemote(RemoteEntity, RestoreEntity):
|
class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
"""Representation of a Broadlink remote."""
|
"""Representation of a Broadlink remote."""
|
||||||
|
|
||||||
def __init__(self, device, codes, flags):
|
def __init__(self, device, codes, flags):
|
||||||
"""Initialize the remote."""
|
"""Initialize the remote."""
|
||||||
self._device = device
|
super().__init__(device)
|
||||||
self._coordinator = device.update_manager.coordinator
|
self._coordinator = device.update_manager.coordinator
|
||||||
self._code_storage = codes
|
self._code_storage = codes
|
||||||
self._flag_storage = flags
|
self._flag_storage = flags
|
||||||
@ -142,32 +143,11 @@ class BroadlinkRemote(RemoteEntity, RestoreEntity):
|
|||||||
"""Return True if the remote is on."""
|
"""Return True if the remote is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return True if the remote is available."""
|
|
||||||
return self._device.update_manager.available
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return True if the remote has to be polled for state."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
return SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND
|
return SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self):
|
|
||||||
"""Return device info."""
|
|
||||||
return {
|
|
||||||
"identifiers": {(DOMAIN, self._device.unique_id)},
|
|
||||||
"manufacturer": self._device.api.manufacturer,
|
|
||||||
"model": self._device.api.model,
|
|
||||||
"name": self._device.name,
|
|
||||||
"sw_version": self._device.fw_version,
|
|
||||||
}
|
|
||||||
|
|
||||||
def _extract_codes(self, commands, device=None):
|
def _extract_codes(self, commands, device=None):
|
||||||
"""Extract a list of codes.
|
"""Extract a list of codes.
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ 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
|
||||||
|
from .entity import BroadlinkEntity
|
||||||
from .helpers import import_device
|
from .helpers import import_device
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -67,12 +68,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkSensor(SensorEntity):
|
class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
||||||
"""Representation of a Broadlink sensor."""
|
"""Representation of a Broadlink sensor."""
|
||||||
|
|
||||||
def __init__(self, device, monitored_condition):
|
def __init__(self, device, monitored_condition):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._device = device
|
super().__init__(device)
|
||||||
self._coordinator = device.update_manager.coordinator
|
self._coordinator = device.update_manager.coordinator
|
||||||
self._monitored_condition = monitored_condition
|
self._monitored_condition = monitored_condition
|
||||||
self._state = self._coordinator.data[monitored_condition]
|
self._state = self._coordinator.data[monitored_condition]
|
||||||
@ -92,21 +93,11 @@ class BroadlinkSensor(SensorEntity):
|
|||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return True if the sensor is available."""
|
|
||||||
return self._device.update_manager.available
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit of measurement of the sensor."""
|
"""Return the unit of measurement of the sensor."""
|
||||||
return SENSOR_TYPES[self._monitored_condition][1]
|
return SENSOR_TYPES[self._monitored_condition][1]
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return True if the sensor has to be polled for state."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
"""Return device class."""
|
"""Return device class."""
|
||||||
@ -117,17 +108,6 @@ class BroadlinkSensor(SensorEntity):
|
|||||||
"""Return state class."""
|
"""Return state class."""
|
||||||
return SENSOR_TYPES[self._monitored_condition][3]
|
return SENSOR_TYPES[self._monitored_condition][3]
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self):
|
|
||||||
"""Return device info."""
|
|
||||||
return {
|
|
||||||
"identifiers": {(DOMAIN, self._device.unique_id)},
|
|
||||||
"manufacturer": self._device.api.manufacturer,
|
|
||||||
"model": self._device.api.model,
|
|
||||||
"name": self._device.name,
|
|
||||||
"sw_version": self._device.fw_version,
|
|
||||||
}
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
"""Update data."""
|
"""Update data."""
|
||||||
|
@ -29,6 +29,7 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from .const import DOMAIN, SWITCH_DOMAIN
|
from .const import DOMAIN, SWITCH_DOMAIN
|
||||||
|
from .entity import BroadlinkEntity
|
||||||
from .helpers import data_packet, import_device, mac_address
|
from .helpers import data_packet, import_device, mac_address
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -131,12 +132,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async_add_entities(switches)
|
async_add_entities(switches)
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkSwitch(SwitchEntity, RestoreEntity, ABC):
|
class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
||||||
"""Representation of a Broadlink switch."""
|
"""Representation of a Broadlink switch."""
|
||||||
|
|
||||||
def __init__(self, device, command_on, command_off):
|
def __init__(self, device, command_on, command_off):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
self._device = 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
|
||||||
@ -152,37 +153,16 @@ class BroadlinkSwitch(SwitchEntity, RestoreEntity, ABC):
|
|||||||
"""Return True if unable to access real state of the switch."""
|
"""Return True if unable to access real state of the switch."""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return True if the switch is available."""
|
|
||||||
return self._device.update_manager.available
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return True if the switch is on."""
|
"""Return True if the switch is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return True if the switch has to be polled for state."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
"""Return device class."""
|
"""Return device class."""
|
||||||
return DEVICE_CLASS_SWITCH
|
return DEVICE_CLASS_SWITCH
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self):
|
|
||||||
"""Return device info."""
|
|
||||||
return {
|
|
||||||
"identifiers": {(DOMAIN, self._device.unique_id)},
|
|
||||||
"manufacturer": self._device.api.manufacturer,
|
|
||||||
"model": self._device.api.model,
|
|
||||||
"name": self._device.name,
|
|
||||||
"sw_version": self._device.fw_version,
|
|
||||||
}
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
"""Update data."""
|
"""Update data."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user