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