Convert most esphome state updates to callbacks (#43246)

This commit is contained in:
J. Nick Koston 2020-11-15 11:18:23 -10:00 committed by GitHub
parent 149ba088d4
commit 9d0cd9c1b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 39 deletions

View file

@ -7,9 +7,10 @@ from aioesphomeapi import CameraInfo, CameraState
from homeassistant.components import camera
from homeassistant.components.camera import Camera
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType
from . import EsphomeEntity, platform_async_setup_entry
from . import EsphomeBaseEntity, platform_async_setup_entry
async def async_setup_entry(
@ -27,13 +28,13 @@ async def async_setup_entry(
)
class EsphomeCamera(Camera, EsphomeEntity):
class EsphomeCamera(Camera, EsphomeBaseEntity):
"""A camera implementation for ESPHome."""
def __init__(self, entry_id: str, component_key: str, key: int):
"""Initialize."""
Camera.__init__(self)
EsphomeEntity.__init__(self, entry_id, component_key, key)
EsphomeBaseEntity.__init__(self, entry_id, component_key, key)
self._image_cond = asyncio.Condition()
@property
@ -44,9 +45,25 @@ class EsphomeCamera(Camera, EsphomeEntity):
def _state(self) -> Optional[CameraState]:
return super()._state
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
await super().async_added_to_hass()
self.async_on_remove(
async_dispatcher_connect(
self.hass,
(
f"esphome_{self._entry_id}"
f"_update_{self._component_key}_{self._key}"
),
self._on_state_update,
)
)
async def _on_state_update(self) -> None:
"""Notify listeners of new image when update arrives."""
await super()._on_state_update()
self.async_write_ha_state()
async with self._image_cond:
self._image_cond.notify_all()