From 22c8afe637a98a82c79f2b636c81473663e7bbc1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 24 Jun 2021 19:39:21 -1000 Subject: [PATCH] Create a base class for broadlink entities (#52132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Create a base class for broadlink entities * Update homeassistant/components/broadlink/entity.py Co-authored-by: Daniel Hjelseth Høyer * Update homeassistant/components/broadlink/entity.py * Update homeassistant/components/broadlink/entity.py Co-authored-by: Daniel Hjelseth Høyer * black, remove unused Co-authored-by: Daniel Hjelseth Høyer --- homeassistant/components/broadlink/device.py | 5 +++ homeassistant/components/broadlink/entity.py | 32 ++++++++++++++++++++ homeassistant/components/broadlink/remote.py | 26 ++-------------- homeassistant/components/broadlink/sensor.py | 26 ++-------------- homeassistant/components/broadlink/switch.py | 26 ++-------------- 5 files changed, 46 insertions(+), 69 deletions(-) create mode 100644 homeassistant/components/broadlink/entity.py diff --git a/homeassistant/components/broadlink/device.py b/homeassistant/components/broadlink/device.py index b18d64c327f..2686b3dd9ed 100644 --- a/homeassistant/components/broadlink/device.py +++ b/homeassistant/components/broadlink/device.py @@ -51,6 +51,11 @@ class BroadlinkDevice: """Return the unique id of the device.""" return self.config.unique_id + @property + def mac_address(self): + """Return the mac address of the device.""" + return self.config.data[CONF_MAC] + @staticmethod async def async_update(hass, entry): """Update the device and related entities. diff --git a/homeassistant/components/broadlink/entity.py b/homeassistant/components/broadlink/entity.py new file mode 100644 index 00000000000..850611b391f --- /dev/null +++ b/homeassistant/components/broadlink/entity.py @@ -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, + } diff --git a/homeassistant/components/broadlink/remote.py b/homeassistant/components/broadlink/remote.py index 291bf6a3d8b..3e0c37d3f55 100644 --- a/homeassistant/components/broadlink/remote.py +++ b/homeassistant/components/broadlink/remote.py @@ -40,6 +40,7 @@ from homeassistant.helpers.storage import Store from homeassistant.util.dt import utcnow from .const import DOMAIN +from .entity import BroadlinkEntity from .helpers import data_packet, import_device _LOGGER = logging.getLogger(__name__) @@ -112,12 +113,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities([remote], False) -class BroadlinkRemote(RemoteEntity, RestoreEntity): +class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): """Representation of a Broadlink remote.""" def __init__(self, device, codes, flags): """Initialize the remote.""" - self._device = device + super().__init__(device) self._coordinator = device.update_manager.coordinator self._code_storage = codes self._flag_storage = flags @@ -142,32 +143,11 @@ class BroadlinkRemote(RemoteEntity, RestoreEntity): """Return True if the remote is on.""" 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 def supported_features(self): """Flag supported features.""" 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): """Extract a list of codes. diff --git a/homeassistant/components/broadlink/sensor.py b/homeassistant/components/broadlink/sensor.py index 92708583c43..15445699a33 100644 --- a/homeassistant/components/broadlink/sensor.py +++ b/homeassistant/components/broadlink/sensor.py @@ -16,6 +16,7 @@ from homeassistant.core import callback from homeassistant.helpers import config_validation as cv from .const import DOMAIN +from .entity import BroadlinkEntity from .helpers import import_device _LOGGER = logging.getLogger(__name__) @@ -67,12 +68,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(sensors) -class BroadlinkSensor(SensorEntity): +class BroadlinkSensor(BroadlinkEntity, SensorEntity): """Representation of a Broadlink sensor.""" def __init__(self, device, monitored_condition): """Initialize the sensor.""" - self._device = device + super().__init__(device) self._coordinator = device.update_manager.coordinator self._monitored_condition = monitored_condition self._state = self._coordinator.data[monitored_condition] @@ -92,21 +93,11 @@ class BroadlinkSensor(SensorEntity): """Return the state of the sensor.""" return self._state - @property - def available(self): - """Return True if the sensor is available.""" - return self._device.update_manager.available - @property def unit_of_measurement(self): """Return the unit of measurement of the sensor.""" 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 def device_class(self): """Return device class.""" @@ -117,17 +108,6 @@ class BroadlinkSensor(SensorEntity): """Return state class.""" 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 def update_data(self): """Update data.""" diff --git a/homeassistant/components/broadlink/switch.py b/homeassistant/components/broadlink/switch.py index 0a98530c806..4aed6e2288a 100644 --- a/homeassistant/components/broadlink/switch.py +++ b/homeassistant/components/broadlink/switch.py @@ -29,6 +29,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.restore_state import RestoreEntity from .const import DOMAIN, SWITCH_DOMAIN +from .entity import BroadlinkEntity from .helpers import data_packet, import_device, mac_address _LOGGER = logging.getLogger(__name__) @@ -131,12 +132,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(switches) -class BroadlinkSwitch(SwitchEntity, RestoreEntity, ABC): +class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC): """Representation of a Broadlink switch.""" def __init__(self, device, command_on, command_off): """Initialize the switch.""" - self._device = device + super().__init__(device) self._command_on = command_on self._command_off = command_off 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 - @property - def available(self): - """Return True if the switch is available.""" - return self._device.update_manager.available - @property def is_on(self): """Return True if the switch is on.""" return self._state - @property - def should_poll(self): - """Return True if the switch has to be polled for state.""" - return False - @property def device_class(self): """Return device class.""" 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 def update_data(self): """Update data."""