Clean up alarmdecoder (#52517)
* Clean up alarmdecoder * fix * try again * tweak
This commit is contained in:
parent
07bda0973e
commit
6e779855f7
5 changed files with 65 additions and 153 deletions
|
@ -60,6 +60,8 @@ async def async_setup_entry(
|
|||
class AlarmDecoderBinarySensor(BinarySensorEntity):
|
||||
"""Representation of an AlarmDecoder binary sensor."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
zone_number,
|
||||
|
@ -73,13 +75,12 @@ class AlarmDecoderBinarySensor(BinarySensorEntity):
|
|||
"""Initialize the binary_sensor."""
|
||||
self._zone_number = int(zone_number)
|
||||
self._zone_type = zone_type
|
||||
self._state = None
|
||||
self._name = zone_name
|
||||
self._attr_name = zone_name
|
||||
self._rfid = zone_rfid
|
||||
self._loop = zone_loop
|
||||
self._rfstate = None
|
||||
self._relay_addr = relay_addr
|
||||
self._relay_chan = relay_chan
|
||||
self._attr_device_class = zone_type
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register callbacks."""
|
||||
|
@ -107,59 +108,35 @@ class AlarmDecoderBinarySensor(BinarySensorEntity):
|
|||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
attr = {CONF_ZONE_NUMBER: self._zone_number}
|
||||
if self._rfid and self._rfstate is not None:
|
||||
attr[ATTR_RF_BIT0] = bool(self._rfstate & 0x01)
|
||||
attr[ATTR_RF_LOW_BAT] = bool(self._rfstate & 0x02)
|
||||
attr[ATTR_RF_SUPERVISED] = bool(self._rfstate & 0x04)
|
||||
attr[ATTR_RF_BIT3] = bool(self._rfstate & 0x08)
|
||||
attr[ATTR_RF_LOOP3] = bool(self._rfstate & 0x10)
|
||||
attr[ATTR_RF_LOOP2] = bool(self._rfstate & 0x20)
|
||||
attr[ATTR_RF_LOOP4] = bool(self._rfstate & 0x40)
|
||||
attr[ATTR_RF_LOOP1] = bool(self._rfstate & 0x80)
|
||||
return attr
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if sensor is on."""
|
||||
return self._state == 1
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of this sensor, from DEVICE_CLASSES."""
|
||||
return self._zone_type
|
||||
|
||||
def _fault_callback(self, zone):
|
||||
"""Update the zone's state, if needed."""
|
||||
if zone is None or int(zone) == self._zone_number:
|
||||
self._state = 1
|
||||
self._attr_state = 1
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
def _restore_callback(self, zone):
|
||||
"""Update the zone's state, if needed."""
|
||||
if zone is None or (int(zone) == self._zone_number and not self._loop):
|
||||
self._state = 0
|
||||
self._attr_state = 0
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
def _rfx_message_callback(self, message):
|
||||
"""Update RF state."""
|
||||
if self._rfid and message and message.serial_number == self._rfid:
|
||||
self._rfstate = message.value
|
||||
rfstate = message.value
|
||||
if self._loop:
|
||||
self._state = 1 if message.loop[self._loop - 1] else 0
|
||||
self._attr_state = 1 if message.loop[self._loop - 1] else 0
|
||||
attr = {CONF_ZONE_NUMBER: self._zone_number}
|
||||
if self._rfid and rfstate is not None:
|
||||
attr[ATTR_RF_BIT0] = bool(rfstate & 0x01)
|
||||
attr[ATTR_RF_LOW_BAT] = bool(rfstate & 0x02)
|
||||
attr[ATTR_RF_SUPERVISED] = bool(rfstate & 0x04)
|
||||
attr[ATTR_RF_BIT3] = bool(rfstate & 0x08)
|
||||
attr[ATTR_RF_LOOP3] = bool(rfstate & 0x10)
|
||||
attr[ATTR_RF_LOOP2] = bool(rfstate & 0x20)
|
||||
attr[ATTR_RF_LOOP4] = bool(rfstate & 0x40)
|
||||
attr[ATTR_RF_LOOP1] = bool(rfstate & 0x80)
|
||||
self._attr_extra_state_attributes = attr
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
def _rel_message_callback(self, message):
|
||||
|
@ -173,5 +150,5 @@ class AlarmDecoderBinarySensor(BinarySensorEntity):
|
|||
message.channel,
|
||||
message.value,
|
||||
)
|
||||
self._state = message.value
|
||||
self._attr_state = message.value
|
||||
self.schedule_update_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue