Skip to content

Commit

Permalink
Merge branch 'master' into patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolbi authored Apr 4, 2024
2 parents 03bc355 + f0f187f commit 4608d33
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
39 changes: 34 additions & 5 deletions custom_components/audiconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
DOMAIN,
CONF_REGION,
CONF_MUTABLE,
CONF_SCAN_INITIAL,
CONF_SCAN_ACTIVE,
DEFAULT_UPDATE_INTERVAL,
MIN_UPDATE_INTERVAL,
RESOURCES,
Expand Down Expand Up @@ -107,6 +109,15 @@ async def async_setup_entry(hass, config_entry):
config_entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_UPDATE_INTERVAL),
)
)
_LOGGER.debug("User option for CONF_SCAN_INTERVAL is %s", scan_interval)

# Get Initial Scan Option - Default to True
_scan_initial = config_entry.options.get(CONF_SCAN_INITIAL, True)
_LOGGER.debug("User option for CONF_SCAN_INITIAL is %s.", _scan_initial)

# Get Active Scan Option - Default to True
_scan_active = config_entry.options.get(CONF_SCAN_ACTIVE, True)
_LOGGER.debug("User option for CONF_SCAN_ACTIVE is %s.", _scan_active)

account = config_entry.data.get(CONF_USERNAME)

Expand All @@ -122,16 +133,34 @@ async def async_setup_entry(hass, config_entry):
else:
data = hass.data[DOMAIN][account]

# Define a callback function for the timer to update data
async def update_data(now):
"""Update the data with the latest information."""
_LOGGER.info("Running cloud update at set interval...")
_LOGGER.info("Scheduled cloud update started...")
await data.update(utcnow())

_LOGGER.info("Scheduling update at every %s interval", scan_interval)
async_track_time_interval(hass, update_data, scan_interval)
# Schedule the update_data function if option is true
if _scan_active:
_LOGGER.info(
"Scheduling cloud update every %d minutes.", scan_interval.seconds / 60
)
async_track_time_interval(hass, update_data, scan_interval)
else:
_LOGGER.info(
"Active Polling at Scan Interval is turned off in user options. Skipping scheduling..."
)

# Initially update the data
return await data.update(utcnow())
# Initially update the data if option is true
if _scan_initial:
_LOGGER.info("Requesting initial cloud update...")
return await data.update(utcnow())
else:
_LOGGER.info(
"Cloud Update at Start is turned off in user options. Skipping initial update..."
)

_LOGGER.debug("Audi Connect Setup Complete.")
return True


async def async_unload_entry(hass, config_entry):
Expand Down
41 changes: 36 additions & 5 deletions custom_components/audiconnect/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
from homeassistant.core import callback

from .audi_connect_account import AudiConnectAccount
from .const import DOMAIN, CONF_SPIN, DEFAULT_UPDATE_INTERVAL, MIN_UPDATE_INTERVAL
from .const import (
DOMAIN,
CONF_SPIN,
DEFAULT_UPDATE_INTERVAL,
MIN_UPDATE_INTERVAL,
CONF_SCAN_INITIAL,
CONF_SCAN_ACTIVE,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -157,24 +164,48 @@ def async_get_options_flow(config_entry):
class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry):
self.config_entry: config_entries.ConfigEntry = config_entry
_LOGGER.debug("Initializing options flow for %s", config_entry.title)
_LOGGER.debug(
"Initializing options flow for audiconnect: %s", config_entry.title
)

async def async_step_init(self, user_input=None):
_LOGGER.debug("Options flow initiated")
_LOGGER.debug(
"Options flow initiated for audiconnect: %s", self.config_entry.title
)
if user_input is not None:
_LOGGER.debug("Received user input for options: %s", user_input)
_LOGGER.info("Received user input for options: %s", user_input)
return self.async_create_entry(title="", data=user_input)

current_scan_interval = self.config_entry.options.get(
CONF_SCAN_INTERVAL,
self.config_entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_UPDATE_INTERVAL),
)
_LOGGER.debug("Current scan interval: %s minutes", current_scan_interval)
_LOGGER.info(
"Retrieved current scan interval for audiconnect %s: %s minutes",
self.config_entry.title,
current_scan_interval,
)

_LOGGER.debug(
"Preparing options form for %s with default scan interval: %s minutes, initial scan: %s, active scan: %s",
self.config_entry.title,
current_scan_interval,
self.config_entry.options.get(CONF_SCAN_INITIAL, True),
self.config_entry.options.get(CONF_SCAN_ACTIVE, True),
)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
CONF_SCAN_INITIAL,
default=self.config_entry.options.get(CONF_SCAN_INITIAL, True),
): bool,
vol.Required(
CONF_SCAN_ACTIVE,
default=self.config_entry.options.get(CONF_SCAN_ACTIVE, True),
): bool,
vol.Optional(
CONF_SCAN_INTERVAL, default=current_scan_interval
): vol.All(vol.Coerce(int), vol.Clamp(min=MIN_UPDATE_INTERVAL)),
Expand Down
2 changes: 2 additions & 0 deletions custom_components/audiconnect/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
CONF_CLIMATE_SEAT_FR = "seat_fr"
CONF_CLIMATE_SEAT_RL = "seat_rl"
CONF_CLIMATE_SEAT_RR = "seat_rr"
CONF_SCAN_INITIAL = "scan_initial"
CONF_SCAN_ACTIVE = "scan_active"

MIN_UPDATE_INTERVAL = 15
DEFAULT_UPDATE_INTERVAL = 15
Expand Down
6 changes: 5 additions & 1 deletion custom_components/audiconnect/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
"step": {
"init": {
"data": {
"scan_initial": "Cloud Update at Startup",
"scan_active": "Active Polling at Scan Interval",
"scan_interval": "Scan Interval"
},
"title": "Audi Connect Options",
"data_description": {
"scan_interval": "(Minutes) Restart required for new scan interval to take effect."
"scan_initial": "Perform a cloud update immediately upon startup.",
"scan_active": "Perform a cloud update at the set scan interval.",
"scan_interval": "Minutes between active polling. If 'Active Polling at Scan Interval' is off, this value will have no impact."
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion custom_components/audiconnect/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
"step": {
"init": {
"data": {
"scan_initial": "Cloud-Update beim Start",
"scan_active": "Aktive Abfrage im Scanintervall",
"scan_interval": "Abfrageintervall"
},
"title": "Audi Connect-Optionen",
"data_description": {
"scan_interval": "(Minuten) Damit das neue Scanintervall wirksam wird, ist ein Neustart erforderlich."
"scan_initial": "Führen Sie sofort nach dem Start ein Cloud-Update durch.",
"scan_active": "Führen Sie im festgelegten Scanintervall ein Cloud-Update durch.",
"scan_interval": "Minuten zwischen aktiven Abfragen. Wenn „Aktive Abfrage im Scan-Intervall“ deaktiviert ist, hat dieser Wert keine Auswirkung."
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion custom_components/audiconnect/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
"step": {
"init": {
"data": {
"scan_initial": "Cloud Update at Startup",
"scan_active": "Active Polling at Scan Interval",
"scan_interval": "Scan Interval"
},
"title": "Audi Connect Options",
"data_description": {
"scan_interval": "(Minutes) Restart required for new scan interval to take effect."
"scan_initial": "Perform a cloud update immediately upon startup.",
"scan_active": "Perform a cloud update at the set scan interval.",
"scan_interval": "Minutes between active polling. If 'Active Polling at Scan Interval' is off, this value will have no impact."
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ To add the integration, go to **Settings ➤ Devices & Services ➤ Integrations

- (number)(Optional) The frequency in minutes for how often to fetch status data from Audi Connect. (Optional. Default is 15 minutes, can be no more frequent than 15 min.)

## Options

Find configuration options under **Settings ➤ Devices & Services ➤ Integrations ➤ Audi Connect ➤ Configure**:

- **Cloud Update at Startup (`bool`)**: Toggle cloud updates at integration startup. Ideal for development or frequent HA restarts.
- **Active Polling at Scan Interval (`bool`)**: Enable or disable active polling.
- **Scan Interval (`int`)**: Defines polling frequency in minutes (minimum 15). Effective only if "Active Polling at Scan Interval" is enabled.

_Note: A Home Assistant restart is required for changes to take effect._

## Services

### Audi Connect: Refresh Vehicle Data
Expand Down

0 comments on commit 4608d33

Please sign in to comment.