From 4abdeec36ddcf7d69c855df50bec375b4efc3bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Fri, 25 Jun 2021 23:31:17 +0200 Subject: [PATCH] Use entity class vars in Broadlink (#52177) --- homeassistant/components/broadlink/remote.py | 36 +++------- homeassistant/components/broadlink/sensor.py | 40 +++-------- homeassistant/components/broadlink/switch.py | 75 ++++---------------- 3 files changed, 32 insertions(+), 119 deletions(-) diff --git a/homeassistant/components/broadlink/remote.py b/homeassistant/components/broadlink/remote.py index 3e0c37d3f55..b9dd34d22d8 100644 --- a/homeassistant/components/broadlink/remote.py +++ b/homeassistant/components/broadlink/remote.py @@ -125,28 +125,12 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): self._storage_loaded = False self._codes = {} self._flags = defaultdict(int) - self._state = True self._lock = asyncio.Lock() - @property - def name(self): - """Return the name of the remote.""" - return f"{self._device.name} Remote" - - @property - def unique_id(self): - """Return the unique id of the remote.""" - return self._device.unique_id - - @property - def is_on(self): - """Return True if the remote is on.""" - return self._state - - @property - def supported_features(self): - """Flag supported features.""" - return SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND + self._attr_name = f"{self._device.name} Remote" + self._attr_is_on = True + self._attr_supported_features = SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND + self._attr_unique_id = self._device.unique_id def _extract_codes(self, commands, device=None): """Extract a list of codes. @@ -204,7 +188,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): async def async_added_to_hass(self): """Call when the remote is added to hass.""" state = await self.async_get_last_state() - self._state = state is None or state.state != STATE_OFF + 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) @@ -216,12 +200,12 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): async def async_turn_on(self, **kwargs): """Turn on the remote.""" - self._state = True + self._attr_is_on = True self.async_write_ha_state() async def async_turn_off(self, **kwargs): """Turn off the remote.""" - self._state = False + self._attr_is_on = False self.async_write_ha_state() async def _async_load_storage(self): @@ -242,7 +226,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): delay = kwargs[ATTR_DELAY_SECS] service = f"{RM_DOMAIN}.{SERVICE_SEND_COMMAND}" - if not self._state: + if not self._attr_is_on: _LOGGER.warning( "%s canceled: %s entity is turned off", service, self.entity_id ) @@ -297,7 +281,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): toggle = kwargs[ATTR_ALTERNATIVE] service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}" - if not self._state: + if not self._attr_is_on: _LOGGER.warning( "%s canceled: %s entity is turned off", service, self.entity_id ) @@ -455,7 +439,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): device = kwargs[ATTR_DEVICE] service = f"{RM_DOMAIN}.{SERVICE_DELETE_COMMAND}" - if not self._state: + if not self._attr_is_on: _LOGGER.warning( "%s canceled: %s entity is turned off", service, diff --git a/homeassistant/components/broadlink/sensor.py b/homeassistant/components/broadlink/sensor.py index 15445699a33..851668fdeff 100644 --- a/homeassistant/components/broadlink/sensor.py +++ b/homeassistant/components/broadlink/sensor.py @@ -76,43 +76,21 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity): super().__init__(device) self._coordinator = device.update_manager.coordinator self._monitored_condition = monitored_condition - self._state = self._coordinator.data[monitored_condition] - @property - def unique_id(self): - """Return the unique id of the sensor.""" - return f"{self._device.unique_id}-{self._monitored_condition}" - - @property - def name(self): - """Return the name of the sensor.""" - return f"{self._device.name} {SENSOR_TYPES[self._monitored_condition][0]}" - - @property - def state(self): - """Return the state of the sensor.""" - return self._state - - @property - def unit_of_measurement(self): - """Return the unit of measurement of the sensor.""" - return SENSOR_TYPES[self._monitored_condition][1] - - @property - def device_class(self): - """Return device class.""" - return SENSOR_TYPES[self._monitored_condition][2] - - @property - def state_class(self): - """Return state class.""" - return SENSOR_TYPES[self._monitored_condition][3] + self._attr_device_class = SENSOR_TYPES[self._monitored_condition][2] + self._attr_name = ( + f"{self._device.name} {SENSOR_TYPES[self._monitored_condition][0]}" + ) + self._attr_state_class = SENSOR_TYPES[self._monitored_condition][3] + self._attr_state = self._coordinator.data[monitored_condition] + self._attr_unique_id = f"{self._device.unique_id}-{self._monitored_condition}" + self._attr_unit_of_measurement = SENSOR_TYPES[self._monitored_condition][1] @callback def update_data(self): """Update data.""" if self._coordinator.last_update_success: - self._state = self._coordinator.data[self._monitored_condition] + self._attr_state = self._coordinator.data[self._monitored_condition] self.async_write_ha_state() async def async_added_to_hass(self): diff --git a/homeassistant/components/broadlink/switch.py b/homeassistant/components/broadlink/switch.py index 4aed6e2288a..1f599d6d108 100644 --- a/homeassistant/components/broadlink/switch.py +++ b/homeassistant/components/broadlink/switch.py @@ -143,26 +143,16 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC): self._coordinator = device.update_manager.coordinator self._state = None - @property - def name(self): - """Return the name of the switch.""" - return f"{self._device.name} Switch" - - @property - def assumed_state(self): - """Return True if unable to access real state of the switch.""" - return True + self._attr_assumed_state = True + self._attr_device_class = DEVICE_CLASS_SWITCH + self._attr_name = f"{self._device.name} Switch" + self._attr_unique_id = self._device.unique_id @property def is_on(self): """Return True if the switch is on.""" return self._state - @property - def device_class(self): - """Return device class.""" - return DEVICE_CLASS_SWITCH - @callback def update_data(self): """Update data.""" @@ -204,12 +194,7 @@ class BroadlinkRMSwitch(BroadlinkSwitch): super().__init__( device, config.get(CONF_COMMAND_ON), config.get(CONF_COMMAND_OFF) ) - self._name = config[CONF_NAME] - - @property - def name(self): - """Return the name of the switch.""" - return self._name + self._attr_name = config[CONF_NAME] async def _async_send_packet(self, packet): """Send a packet to the device.""" @@ -231,11 +216,6 @@ class BroadlinkSP1Switch(BroadlinkSwitch): """Initialize the switch.""" super().__init__(device, 1, 0) - @property - def unique_id(self): - """Return the unique id of the switch.""" - return self._device.unique_id - async def _async_send_packet(self, packet): """Send a packet to the device.""" try: @@ -255,10 +235,7 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch): self._state = self._coordinator.data["pwr"] self._load_power = self._coordinator.data.get("power") - @property - def assumed_state(self): - """Return True if unable to access real state of the switch.""" - return False + self._attr_assumed_state = False @property def current_power_w(self): @@ -283,20 +260,9 @@ class BroadlinkMP1Slot(BroadlinkSwitch): self._slot = slot self._state = self._coordinator.data[f"s{slot}"] - @property - def unique_id(self): - """Return the unique id of the slot.""" - return f"{self._device.unique_id}-s{self._slot}" - - @property - def name(self): - """Return the name of the switch.""" - return f"{self._device.name} S{self._slot}" - - @property - def assumed_state(self): - """Return True if unable to access real state of the switch.""" - return False + self._attr_name = f"{self._device.name} S{self._slot}" + self._attr_unique_id = f"{self._device.unique_id}-s{self._slot}" + self._attr_assumed_state = False @callback def update_data(self): @@ -326,25 +292,10 @@ class BroadlinkBG1Slot(BroadlinkSwitch): self._slot = slot self._state = self._coordinator.data[f"pwr{slot}"] - @property - def unique_id(self): - """Return the unique id of the slot.""" - return f"{self._device.unique_id}-s{self._slot}" - - @property - def name(self): - """Return the name of the switch.""" - return f"{self._device.name} S{self._slot}" - - @property - def assumed_state(self): - """Return True if unable to access real state of the switch.""" - return False - - @property - def device_class(self): - """Return device class.""" - return DEVICE_CLASS_OUTLET + self._attr_name = f"{self._device.name} S{self._slot}" + self._attr_device_class = DEVICE_CLASS_OUTLET + self._attr_unique_id = f"{self._device.unique_id}-s{self._slot}" + self._attr_assumed_state = False @callback def update_data(self):