diff --git a/homeassistant/components/broadlink/entity.py b/homeassistant/components/broadlink/entity.py index 850611b391f..fc5d22302a6 100644 --- a/homeassistant/components/broadlink/entity.py +++ b/homeassistant/components/broadlink/entity.py @@ -1,11 +1,12 @@ """Broadlink entities.""" from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.entity import Entity from .const import DOMAIN -class BroadlinkEntity: +class BroadlinkEntity(Entity): """Representation of a Broadlink entity.""" _attr_should_poll = False diff --git a/homeassistant/components/broadlink/remote.py b/homeassistant/components/broadlink/remote.py index b9dd34d22d8..3bb85ab9d85 100644 --- a/homeassistant/components/broadlink/remote.py +++ b/homeassistant/components/broadlink/remote.py @@ -127,10 +127,10 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity): self._flags = defaultdict(int) 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_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): """Extract a list of codes. diff --git a/homeassistant/components/broadlink/sensor.py b/homeassistant/components/broadlink/sensor.py index 851668fdeff..044486a4a67 100644 --- a/homeassistant/components/broadlink/sensor.py +++ b/homeassistant/components/broadlink/sensor.py @@ -77,14 +77,12 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity): self._coordinator = device.update_manager.coordinator self._monitored_condition = monitored_condition - 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_device_class = SENSOR_TYPES[monitored_condition][2] + self._attr_name = f"{device.name} {SENSOR_TYPES[monitored_condition][0]}" + self._attr_state_class = SENSOR_TYPES[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] + 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): diff --git a/homeassistant/components/broadlink/switch.py b/homeassistant/components/broadlink/switch.py index 1576c8b8418..9f434102b17 100644 --- a/homeassistant/components/broadlink/switch.py +++ b/homeassistant/components/broadlink/switch.py @@ -135,22 +135,20 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC): """Representation of a Broadlink switch.""" + _attr_assumed_state = True + _attr_device_class = DEVICE_CLASS_SWITCH + def __init__(self, device, command_on, command_off): """Initialize the switch.""" super().__init__(device) self._command_on = command_on self._command_off = command_off self._coordinator = device.update_manager.coordinator - self._state = None self._attr_assumed_state = True self._attr_device_class = DEVICE_CLASS_SWITCH - self._attr_name = f"{self._device.name} Switch" - - @property - def is_on(self): - """Return True if the switch is on.""" - return self._state + self._attr_name = f"{device.name} Switch" + self._attr_unique_id = device.unique_id @callback def update_data(self): @@ -159,9 +157,8 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC): async def async_added_to_hass(self): """Call when the switch is added to hass.""" - if self._state is None: - state = await self.async_get_last_state() - self._state = state is not None and state.state == STATE_ON + 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): @@ -171,13 +168,13 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC): async def async_turn_on(self, **kwargs): """Turn on the switch.""" if await self._async_send_packet(self._command_on): - self._state = True + self._attr_is_on = True self.async_write_ha_state() async def async_turn_off(self, **kwargs): """Turn off the switch.""" if await self._async_send_packet(self._command_off): - self._state = False + self._attr_is_on = False self.async_write_ha_state() @abstractmethod @@ -229,46 +226,41 @@ class BroadlinkSP1Switch(BroadlinkSwitch): class BroadlinkSP2Switch(BroadlinkSP1Switch): """Representation of a Broadlink SP2 switch.""" + _attr_assumed_state = False + def __init__(self, device, *args, **kwargs): """Initialize the switch.""" super().__init__(device, *args, **kwargs) - self._state = self._coordinator.data["pwr"] - self._load_power = 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 + self._attr_is_on = self._coordinator.data["pwr"] + self._attr_current_power_w = self._coordinator.data.get("power") @callback def update_data(self): """Update data.""" if self._coordinator.last_update_success: - self._state = self._coordinator.data["pwr"] - self._load_power = self._coordinator.data.get("power") + self._attr_is_on = self._coordinator.data["pwr"] + self._attr_current_power_w = self._coordinator.data.get("power") self.async_write_ha_state() class BroadlinkMP1Slot(BroadlinkSwitch): """Representation of a Broadlink MP1 slot.""" + _attr_assumed_state = False + def __init__(self, device, slot): """Initialize the switch.""" super().__init__(device, 1, 0) self._slot = slot - self._state = self._coordinator.data[f"s{slot}"] - - 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 + self._attr_is_on = self._coordinator.data[f"s{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._state = self._coordinator.data[f"s{self._slot}"] + self._attr_is_on = self._coordinator.data[f"s{self._slot}"] self.async_write_ha_state() async def _async_send_packet(self, packet): @@ -286,22 +278,23 @@ class BroadlinkMP1Slot(BroadlinkSwitch): class BroadlinkBG1Slot(BroadlinkSwitch): """Representation of a Broadlink BG1 slot.""" + _attr_assumed_state = False + def __init__(self, device, slot): """Initialize the switch.""" super().__init__(device, 1, 0) 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_unique_id = f"{self._device.unique_id}-s{self._slot}" - self._attr_assumed_state = False + self._attr_unique_id = f"{device.unique_id}-s{slot}" @callback def update_data(self): """Update data.""" 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() async def _async_send_packet(self, packet):