Use shorthand attributes in Sense (#99833)

This commit is contained in:
Joost Lekkerkerker 2023-09-07 15:56:21 +02:00 committed by GitHub
parent 526b587170
commit 1fe17b5bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,53 +74,23 @@ class SenseDevice(BinarySensorEntity):
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION
_attr_should_poll = False _attr_should_poll = False
_attr_available = False
_attr_device_class = BinarySensorDeviceClass.POWER
def __init__(self, sense_devices_data, device, sense_monitor_id): def __init__(self, sense_devices_data, device, sense_monitor_id):
"""Initialize the Sense binary sensor.""" """Initialize the Sense binary sensor."""
self._name = device["name"] self._attr_name = device["name"]
self._id = device["id"] self._id = device["id"]
self._sense_monitor_id = sense_monitor_id self._sense_monitor_id = sense_monitor_id
self._unique_id = f"{sense_monitor_id}-{self._id}" self._attr_unique_id = f"{sense_monitor_id}-{self._id}"
self._icon = sense_to_mdi(device["icon"]) self._attr_icon = sense_to_mdi(device["icon"])
self._sense_devices_data = sense_devices_data self._sense_devices_data = sense_devices_data
self._state = None
self._available = False
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
@property
def available(self):
"""Return the availability of the binary sensor."""
return self._available
@property
def name(self):
"""Return the name of the binary sensor."""
return self._name
@property
def unique_id(self):
"""Return the unique id of the binary sensor."""
return self._unique_id
@property @property
def old_unique_id(self): def old_unique_id(self):
"""Return the old not so unique id of the binary sensor.""" """Return the old not so unique id of the binary sensor."""
return self._id return self._id
@property
def icon(self):
"""Return the icon of the binary sensor."""
return self._icon
@property
def device_class(self):
"""Return the device class of the binary sensor."""
return BinarySensorDeviceClass.POWER
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
self.async_on_remove( self.async_on_remove(
@ -135,8 +105,8 @@ class SenseDevice(BinarySensorEntity):
def _async_update_from_data(self): def _async_update_from_data(self):
"""Get the latest data, update state. Must not do I/O.""" """Get the latest data, update state. Must not do I/O."""
new_state = bool(self._sense_devices_data.get_device_by_id(self._id)) new_state = bool(self._sense_devices_data.get_device_by_id(self._id))
if self._available and self._state == new_state: if self._attr_available and self._attr_is_on == new_state:
return return
self._available = True self._attr_available = True
self._state = new_state self._attr_is_on = new_state
self.async_write_ha_state() self.async_write_ha_state()