Add Reolink update entity (#87865)
This commit is contained in:
parent
2314d15761
commit
f97b50f762
8 changed files with 162 additions and 34 deletions
|
@ -23,8 +23,9 @@ from .host import ReolinkHost
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CAMERA, Platform.NUMBER]
|
||||
DEVICE_UPDATE_INTERVAL = 60
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CAMERA, Platform.NUMBER, Platform.UPDATE]
|
||||
DEVICE_UPDATE_INTERVAL = timedelta(seconds=60)
|
||||
FIRMWARE_UPDATE_INTERVAL = timedelta(hours=12)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -33,6 +34,7 @@ class ReolinkData:
|
|||
|
||||
host: ReolinkHost
|
||||
device_coordinator: DataUpdateCoordinator
|
||||
firmware_coordinator: DataUpdateCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
|
@ -75,23 +77,44 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
async with async_timeout.timeout(host.api.timeout):
|
||||
await host.renew()
|
||||
|
||||
coordinator_device_config_update = DataUpdateCoordinator(
|
||||
async def async_check_firmware_update():
|
||||
"""Check for firmware updates."""
|
||||
async with async_timeout.timeout(host.api.timeout):
|
||||
try:
|
||||
return await host.api.check_new_firmware()
|
||||
except ReolinkError as err:
|
||||
raise UpdateFailed(
|
||||
f"Error checking Reolink firmware update {host.api.nvr_name}"
|
||||
) from err
|
||||
|
||||
device_coordinator = DataUpdateCoordinator(
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=f"reolink.{host.api.nvr_name}",
|
||||
update_method=async_device_config_update,
|
||||
update_interval=timedelta(seconds=DEVICE_UPDATE_INTERVAL),
|
||||
update_interval=DEVICE_UPDATE_INTERVAL,
|
||||
)
|
||||
firmware_coordinator = DataUpdateCoordinator(
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=f"reolink.{host.api.nvr_name}.firmware",
|
||||
update_method=async_check_firmware_update,
|
||||
update_interval=FIRMWARE_UPDATE_INTERVAL,
|
||||
)
|
||||
# Fetch initial data so we have data when entities subscribe
|
||||
try:
|
||||
await coordinator_device_config_update.async_config_entry_first_refresh()
|
||||
await asyncio.gather(
|
||||
device_coordinator.async_config_entry_first_refresh(),
|
||||
firmware_coordinator.async_config_entry_first_refresh(),
|
||||
)
|
||||
except ConfigEntryNotReady:
|
||||
await host.stop()
|
||||
raise
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = ReolinkData(
|
||||
host=host,
|
||||
device_coordinator=coordinator_device_config_update,
|
||||
device_coordinator=device_coordinator,
|
||||
firmware_coordinator=firmware_coordinator,
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue