Skip to content

Commit

Permalink
Support refreshing entities after Vantage system is programmed
Browse files Browse the repository at this point in the history
  • Loading branch information
loopj committed Sep 20, 2023
1 parent 5322006 commit dd27617
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ repos:
hooks:
- id: mypy
additional_dependencies:
- aiovantage==0.12.8
- aiovantage==0.13.0b2
- voluptuous-stubs==0.1.1
- homeassistant-stubs==2023.9.0
- homeassistant-stubs==2023.9.2
args: []

- repo: https://github.com/thlorenz/doctoc
Expand Down
28 changes: 27 additions & 1 deletion custom_components/vantage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""The Vantage InFusion Controller integration."""

from aiovantage import Vantage
import asyncio
from typing import Any

from aiovantage import Vantage, VantageEvent
from aiovantage.errors import (
ClientConnectionError,
LoginFailedError,
LoginRequiredError,
)
from aiovantage.models import Master

from homeassistant.config_entries import (
ConfigEntry,
Expand Down Expand Up @@ -38,6 +42,9 @@
Platform.TEXT,
]

# How long to wait after receiving a system programming event before refreshing
SYSTEM_PROGRAMMING_DELAY = 30


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Vantage integration from a config entry."""
Expand Down Expand Up @@ -71,6 +78,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Clean up any orphaned entities
async_cleanup_entities(hass, entry)

# Subscribe to system programming events
async def handle_system_program_event(
event: VantageEvent, obj: Master, data: dict[str, Any]
) -> None:
# Return early if the last_updated attribute did not change
if "last_updated" not in data.get("attrs_changed", []):
return

# The last_updated attribute changes at the start of system programming.
# Unfortunately, the Vantage controller does not send an event when
# programming ends, so we must wait for a short time before refreshing
# controllers to avoid fetching incomplete data.
await asyncio.sleep(SYSTEM_PROGRAMMING_DELAY)
await vantage.initialize()

vantage.masters.subscribe(
handle_system_program_event, event_filter=VantageEvent.OBJECT_UPDATED
)

except (LoginFailedError, LoginRequiredError) as err:
# Handle expired or invalid credentials. This will prompt the user to
# reconfigure the integration.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/vantage/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}

VANTAGE_FAN_MODE_MAP = {
Thermostat.FanMode.OFF: FAN_AUTO,
Thermostat.FanMode.AUTO: FAN_AUTO,
Thermostat.FanMode.ON: FAN_ON,
}

Expand Down
2 changes: 1 addition & 1 deletion custom_components/vantage/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"custom_components.vantage"
],
"requirements": [
"aiovantage==0.12.8"
"aiovantage==0.13.0b2"
],
"version": "0.10.1",
"zeroconf": [
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Home assistant
homeassistant==2023.9.0
homeassistant==2023.9.2
colorlog==6.7.0

# Use a specific version of aiovantage
aiovantage==0.12.8
aiovantage==0.13.0b2

# Use a local version of aiovantage
# -e ../aiovantage

# Type stubs
voluptuous-stubs==0.1.1
homeassistant-stubs==2023.9.1
homeassistant-stubs==2023.9.2

# Linting, etc
black==23.9.1
Expand Down

0 comments on commit dd27617

Please sign in to comment.