Use entity class vars in Broadlink (#52177)
This commit is contained in:
parent
42c4317628
commit
4abdeec36d
3 changed files with 32 additions and 119 deletions
|
@ -125,28 +125,12 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
self._storage_loaded = False
|
self._storage_loaded = False
|
||||||
self._codes = {}
|
self._codes = {}
|
||||||
self._flags = defaultdict(int)
|
self._flags = defaultdict(int)
|
||||||
self._state = True
|
|
||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
@property
|
self._attr_name = f"{self._device.name} Remote"
|
||||||
def name(self):
|
self._attr_is_on = True
|
||||||
"""Return the name of the remote."""
|
self._attr_supported_features = SUPPORT_LEARN_COMMAND | SUPPORT_DELETE_COMMAND
|
||||||
return f"{self._device.name} Remote"
|
self._attr_unique_id = self._device.unique_id
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
def _extract_codes(self, commands, device=None):
|
def _extract_codes(self, commands, device=None):
|
||||||
"""Extract a list of codes.
|
"""Extract a list of codes.
|
||||||
|
@ -204,7 +188,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Call when the remote is added to hass."""
|
"""Call when the remote is added to hass."""
|
||||||
state = await self.async_get_last_state()
|
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.async_on_remove(
|
||||||
self._coordinator.async_add_listener(self.async_write_ha_state)
|
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):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn on the remote."""
|
"""Turn on the remote."""
|
||||||
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 remote."""
|
"""Turn off the remote."""
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def _async_load_storage(self):
|
async def _async_load_storage(self):
|
||||||
|
@ -242,7 +226,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
delay = kwargs[ATTR_DELAY_SECS]
|
delay = kwargs[ATTR_DELAY_SECS]
|
||||||
service = f"{RM_DOMAIN}.{SERVICE_SEND_COMMAND}"
|
service = f"{RM_DOMAIN}.{SERVICE_SEND_COMMAND}"
|
||||||
|
|
||||||
if not self._state:
|
if not self._attr_is_on:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"%s canceled: %s entity is turned off", service, self.entity_id
|
"%s canceled: %s entity is turned off", service, self.entity_id
|
||||||
)
|
)
|
||||||
|
@ -297,7 +281,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
toggle = kwargs[ATTR_ALTERNATIVE]
|
toggle = kwargs[ATTR_ALTERNATIVE]
|
||||||
service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}"
|
service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}"
|
||||||
|
|
||||||
if not self._state:
|
if not self._attr_is_on:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"%s canceled: %s entity is turned off", service, self.entity_id
|
"%s canceled: %s entity is turned off", service, self.entity_id
|
||||||
)
|
)
|
||||||
|
@ -455,7 +439,7 @@ class BroadlinkRemote(BroadlinkEntity, RemoteEntity, RestoreEntity):
|
||||||
device = kwargs[ATTR_DEVICE]
|
device = kwargs[ATTR_DEVICE]
|
||||||
service = f"{RM_DOMAIN}.{SERVICE_DELETE_COMMAND}"
|
service = f"{RM_DOMAIN}.{SERVICE_DELETE_COMMAND}"
|
||||||
|
|
||||||
if not self._state:
|
if not self._attr_is_on:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"%s canceled: %s entity is turned off",
|
"%s canceled: %s entity is turned off",
|
||||||
service,
|
service,
|
||||||
|
|
|
@ -76,43 +76,21 @@ class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
||||||
super().__init__(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]
|
|
||||||
|
|
||||||
@property
|
self._attr_device_class = SENSOR_TYPES[self._monitored_condition][2]
|
||||||
def unique_id(self):
|
self._attr_name = (
|
||||||
"""Return the unique id of the sensor."""
|
f"{self._device.name} {SENSOR_TYPES[self._monitored_condition][0]}"
|
||||||
return f"{self._device.unique_id}-{self._monitored_condition}"
|
)
|
||||||
|
self._attr_state_class = SENSOR_TYPES[self._monitored_condition][3]
|
||||||
@property
|
self._attr_state = self._coordinator.data[monitored_condition]
|
||||||
def name(self):
|
self._attr_unique_id = f"{self._device.unique_id}-{self._monitored_condition}"
|
||||||
"""Return the name of the sensor."""
|
self._attr_unit_of_measurement = SENSOR_TYPES[self._monitored_condition][1]
|
||||||
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]
|
|
||||||
|
|
||||||
@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[self._monitored_condition]
|
self._attr_state = self._coordinator.data[self._monitored_condition]
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
|
|
|
@ -143,26 +143,16 @@ class BroadlinkSwitch(BroadlinkEntity, SwitchEntity, RestoreEntity, ABC):
|
||||||
self._coordinator = device.update_manager.coordinator
|
self._coordinator = device.update_manager.coordinator
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
self._attr_assumed_state = True
|
||||||
def name(self):
|
self._attr_device_class = DEVICE_CLASS_SWITCH
|
||||||
"""Return the name of the switch."""
|
self._attr_name = f"{self._device.name} Switch"
|
||||||
return f"{self._device.name} Switch"
|
self._attr_unique_id = self._device.unique_id
|
||||||
|
|
||||||
@property
|
|
||||||
def assumed_state(self):
|
|
||||||
"""Return True if unable to access real state of the switch."""
|
|
||||||
return True
|
|
||||||
|
|
||||||
@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 device_class(self):
|
|
||||||
"""Return device class."""
|
|
||||||
return DEVICE_CLASS_SWITCH
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
"""Update data."""
|
"""Update data."""
|
||||||
|
@ -204,12 +194,7 @@ class BroadlinkRMSwitch(BroadlinkSwitch):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
device, config.get(CONF_COMMAND_ON), config.get(CONF_COMMAND_OFF)
|
device, config.get(CONF_COMMAND_ON), config.get(CONF_COMMAND_OFF)
|
||||||
)
|
)
|
||||||
self._name = config[CONF_NAME]
|
self._attr_name = config[CONF_NAME]
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the switch."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
async def _async_send_packet(self, packet):
|
async def _async_send_packet(self, packet):
|
||||||
"""Send a packet to the device."""
|
"""Send a packet to the device."""
|
||||||
|
@ -231,11 +216,6 @@ class BroadlinkSP1Switch(BroadlinkSwitch):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(device, 1, 0)
|
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):
|
async def _async_send_packet(self, packet):
|
||||||
"""Send a packet to the device."""
|
"""Send a packet to the device."""
|
||||||
try:
|
try:
|
||||||
|
@ -255,10 +235,7 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
|
||||||
self._state = self._coordinator.data["pwr"]
|
self._state = self._coordinator.data["pwr"]
|
||||||
self._load_power = self._coordinator.data.get("power")
|
self._load_power = self._coordinator.data.get("power")
|
||||||
|
|
||||||
@property
|
self._attr_assumed_state = False
|
||||||
def assumed_state(self):
|
|
||||||
"""Return True if unable to access real state of the switch."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_power_w(self):
|
def current_power_w(self):
|
||||||
|
@ -283,20 +260,9 @@ class BroadlinkMP1Slot(BroadlinkSwitch):
|
||||||
self._slot = slot
|
self._slot = slot
|
||||||
self._state = self._coordinator.data[f"s{slot}"]
|
self._state = self._coordinator.data[f"s{slot}"]
|
||||||
|
|
||||||
@property
|
self._attr_name = f"{self._device.name} S{self._slot}"
|
||||||
def unique_id(self):
|
self._attr_unique_id = f"{self._device.unique_id}-s{self._slot}"
|
||||||
"""Return the unique id of the slot."""
|
self._attr_assumed_state = False
|
||||||
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
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
|
@ -326,25 +292,10 @@ class BroadlinkBG1Slot(BroadlinkSwitch):
|
||||||
self._slot = slot
|
self._slot = slot
|
||||||
self._state = self._coordinator.data[f"pwr{slot}"]
|
self._state = self._coordinator.data[f"pwr{slot}"]
|
||||||
|
|
||||||
@property
|
self._attr_name = f"{self._device.name} S{self._slot}"
|
||||||
def unique_id(self):
|
self._attr_device_class = DEVICE_CLASS_OUTLET
|
||||||
"""Return the unique id of the slot."""
|
self._attr_unique_id = f"{self._device.unique_id}-s{self._slot}"
|
||||||
return f"{self._device.unique_id}-s{self._slot}"
|
self._attr_assumed_state = False
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self):
|
def update_data(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue