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>
pull/80691/head
Sean Vig 2022-10-17 04:13:11 -04:00 committed by Paulus Schoutsen
parent a0eade7632
commit df75346dca
1 changed files with 6 additions and 4 deletions

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