Add option to enable/disable active mode and make update_interval configurable (closes #11)

This commit is contained in:
freybene 2024-06-19 12:15:05 +02:00
parent 6f17edae14
commit 065163fdcc
7 changed files with 242 additions and 59 deletions

View file

@ -9,7 +9,16 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.config_entries import ConfigEntry
from .const import DOMAIN, CONF_JSESSIONID
from .const import (
DOMAIN,
CONF_JSESSIONID,
CONF_ACTIVE_MODE_OTHERS,
CONF_ACTIVE_MODE_OTHERS_DEFAULT,
CONF_ACTIVE_MODE_SMARTTAGS,
CONF_ACTIVE_MODE_SMARTTAGS_DEFAULT,
CONF_UPDATE_INTERVAL,
CONF_UPDATE_INTERVAL_DEFAULT
)
from .utils import fetch_csrf, get_devices, get_device_location
_LOGGER = logging.getLogger(__name__)
@ -25,12 +34,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up SmartThings Find from a config entry."""
hass.data[DOMAIN][entry.entry_id] = {}
# Load the jsessionid from the config and create a session from it
jsessionid = entry.data[CONF_JSESSIONID]
session = async_get_clientsession(hass)
session.cookie_jar.update_cookies({"JSESSIONID": jsessionid})
active_smarttags = entry.options.get(CONF_ACTIVE_MODE_SMARTTAGS, CONF_ACTIVE_MODE_SMARTTAGS_DEFAULT)
active_others = entry.options.get(CONF_ACTIVE_MODE_OTHERS, CONF_ACTIVE_MODE_OTHERS_DEFAULT)
hass.data[DOMAIN][entry.entry_id].update({
CONF_ACTIVE_MODE_SMARTTAGS: active_smarttags,
CONF_ACTIVE_MODE_OTHERS: active_others,
})
# This raises ConfigEntryAuthFailed-exception if failed. So if we
# can continue after fetch_csrf, we know that authentication was ok
await fetch_csrf(hass, session, entry.entry_id)
@ -41,7 +59,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Create an update coordinator. This is responsible to regularly
# fetch data from STF and update the device_tracker and sensor
# entities
coordinator = SmartThingsFindCoordinator(hass, session, devices)
update_interval = entry.options.get(CONF_UPDATE_INTERVAL, CONF_UPDATE_INTERVAL_DEFAULT)
coordinator = SmartThingsFindCoordinator(hass, session, devices, update_interval)
# This is what makes the whole integration slow to load (around 10-15
# seconds for my 15 devices) but it is the right way to do it. Only if
@ -73,7 +92,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class SmartThingsFindCoordinator(DataUpdateCoordinator):
"""Class to manage fetching SmartThings Find data."""
def __init__(self, hass: HomeAssistant, session: aiohttp.ClientSession, devices):
def __init__(self, hass: HomeAssistant, session: aiohttp.ClientSession, devices, update_interval : int):
"""Initialize the coordinator."""
self.session = session
self.devices = devices
@ -82,7 +101,7 @@ class SmartThingsFindCoordinator(DataUpdateCoordinator):
hass,
_LOGGER,
name=DOMAIN,
update_interval=timedelta(minutes=2) # Update interval for all entities
update_interval=timedelta(seconds=update_interval) # Update interval for all entities
)
async def _async_update_data(self):