mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Improve coordinator for yale_smart_alarm (#54091)
* Commit coordinator adjustments * Review changes
This commit is contained in:
parent
9197512ed1
commit
0dece582e4
@ -3,12 +3,17 @@ from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import requests
|
||||
from yalesmartalarmclient.client import AuthenticationError, YaleSmartAlarmClient
|
||||
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
ConfigEntryAuthFailed,
|
||||
DataUpdateCoordinator,
|
||||
UpdateFailed,
|
||||
)
|
||||
|
||||
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER
|
||||
|
||||
@ -40,14 +45,28 @@ class YaleDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
if device["type"] == "device_type.door_lock":
|
||||
lock_status_str = device["minigw_lock_status"]
|
||||
lock_status = int(str(lock_status_str or 0), 16)
|
||||
jammed = (lock_status & 48) == 48
|
||||
closed = (lock_status & 16) == 16
|
||||
locked = (lock_status & 1) == 1
|
||||
if not lock_status and "device_status.lock" in state:
|
||||
device["_state"] = "locked"
|
||||
device["_state2"] = "unknown"
|
||||
locks.append(device)
|
||||
continue
|
||||
if not lock_status and "device_status.unlock" in state:
|
||||
device["_state"] = "unlocked"
|
||||
device["_state2"] = "unknown"
|
||||
locks.append(device)
|
||||
continue
|
||||
if (
|
||||
lock_status
|
||||
and (
|
||||
"device_status.lock" in state or "device_status.unlock" in state
|
||||
)
|
||||
and jammed
|
||||
):
|
||||
device["_state"] = "jammed"
|
||||
device["_state2"] = "closed"
|
||||
locks.append(device)
|
||||
continue
|
||||
if (
|
||||
@ -59,6 +78,7 @@ class YaleDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
and locked
|
||||
):
|
||||
device["_state"] = "locked"
|
||||
device["_state2"] = "closed"
|
||||
locks.append(device)
|
||||
continue
|
||||
if (
|
||||
@ -70,6 +90,7 @@ class YaleDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
and not locked
|
||||
):
|
||||
device["_state"] = "unlocked"
|
||||
device["_state2"] = "closed"
|
||||
locks.append(device)
|
||||
continue
|
||||
if (
|
||||
@ -80,6 +101,7 @@ class YaleDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
and not closed
|
||||
):
|
||||
device["_state"] = "unlocked"
|
||||
device["_state2"] = "open"
|
||||
locks.append(device)
|
||||
continue
|
||||
device["_state"] = "unavailable"
|
||||
@ -110,9 +132,16 @@ class YaleDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
"""Fetch data from Yale."""
|
||||
|
||||
if self.yale is None:
|
||||
self.yale = YaleSmartAlarmClient(
|
||||
self.entry.data[CONF_USERNAME], self.entry.data[CONF_PASSWORD]
|
||||
)
|
||||
try:
|
||||
self.yale = YaleSmartAlarmClient(
|
||||
self.entry.data[CONF_USERNAME], self.entry.data[CONF_PASSWORD]
|
||||
)
|
||||
except AuthenticationError as error:
|
||||
raise ConfigEntryAuthFailed from error
|
||||
except requests.HTTPError as error:
|
||||
if error.response.status_code == 401:
|
||||
raise ConfigEntryAuthFailed from error
|
||||
raise UpdateFailed from error
|
||||
|
||||
try:
|
||||
arm_status = self.yale.get_armed_status()
|
||||
@ -121,14 +150,12 @@ class YaleDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
online = self.yale.get_online()
|
||||
|
||||
except AuthenticationError as error:
|
||||
LOGGER.error("Authentication failed. Check credentials %s", error)
|
||||
self.hass.async_create_task(
|
||||
self.hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_REAUTH, "entry_id": self.entry.entry_id},
|
||||
data=self.entry.data,
|
||||
)
|
||||
)
|
||||
raise ConfigEntryAuthFailed from error
|
||||
except requests.HTTPError as error:
|
||||
if error.response.status_code == 401:
|
||||
raise ConfigEntryAuthFailed from error
|
||||
raise UpdateFailed from error
|
||||
except requests.RequestException as error:
|
||||
raise UpdateFailed from error
|
||||
|
||||
return {
|
||||
|
Loading…
x
Reference in New Issue
Block a user