Add Mutesync dynamic update interval and catch invalid response values (#50764)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
927b5361a3
commit
0327d0b6db
2 changed files with 19 additions and 5 deletions
|
@ -1,7 +1,6 @@
|
|||
"""The mütesync integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import async_timeout
|
||||
|
@ -11,7 +10,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import update_coordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DOMAIN, UPDATE_INTERVAL_IN_MEETING, UPDATE_INTERVAL_NOT_IN_MEETING
|
||||
|
||||
PLATFORMS = ["binary_sensor"]
|
||||
|
||||
|
@ -27,7 +26,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
async def update_data():
|
||||
"""Update the data."""
|
||||
async with async_timeout.timeout(2.5):
|
||||
return await client.get_state()
|
||||
state = await client.get_state()
|
||||
|
||||
if state["muted"] is None or state["in_meeting"] is None:
|
||||
raise update_coordinator.UpdateFailed("Got invalid response")
|
||||
|
||||
if state["in_meeting"]:
|
||||
coordinator.update_interval = UPDATE_INTERVAL_IN_MEETING
|
||||
else:
|
||||
coordinator.update_interval = UPDATE_INTERVAL_NOT_IN_MEETING
|
||||
|
||||
return state
|
||||
|
||||
coordinator = hass.data.setdefault(DOMAIN, {})[
|
||||
entry.entry_id
|
||||
|
@ -35,7 +44,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
hass,
|
||||
logging.getLogger(__name__),
|
||||
name=DOMAIN,
|
||||
update_interval=timedelta(seconds=5),
|
||||
update_interval=UPDATE_INTERVAL_NOT_IN_MEETING,
|
||||
update_method=update_data,
|
||||
)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
"""Constants for the mütesync integration."""
|
||||
from datetime import timedelta
|
||||
from typing import Final
|
||||
|
||||
DOMAIN = "mutesync"
|
||||
DOMAIN: Final = "mutesync"
|
||||
|
||||
UPDATE_INTERVAL_NOT_IN_MEETING: Final = timedelta(seconds=10)
|
||||
UPDATE_INTERVAL_IN_MEETING: Final = timedelta(seconds=5)
|
||||
|
|
Loading…
Add table
Reference in a new issue