Fix updating Amcrest binary sensors (#80365)

* Fix updating Amcrest binary sensors

As detailed in https://bugs.python.org/issue32113, a generator
expression cannot be used with asynchronous components, even that the
resulting elements of the generator are normal objects.  Manually
iterate over the event codes and check if the events have happened.
Escape early on the first event that is triggered such that this is
functionally equivalent to using `any`.

* Update homeassistant/components/amcrest/binary_sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Sean Vig 2022-10-17 04:13:11 -04:00 committed by GitHub
parent dfd3476cff
commit d49a223a02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -215,10 +215,12 @@ class AmcrestBinarySensor(BinarySensorEntity):
raise ValueError(f"Binary sensor {self.name} event codes not set")
try:
self._attr_is_on = any( # type: ignore[arg-type]
len(await self._api.async_event_channels_happened(event_code)) > 0
for event_code in event_codes
)
for event_code in event_codes:
if await self._api.async_event_channels_happened(event_code):
self._attr_is_on = True
break
else:
self._attr_is_on = False
except AmcrestError as error:
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
return