Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #150 from oischinger/fixscaninterval
Browse files Browse the repository at this point in the history
Fixscaninterval
  • Loading branch information
oischinger authored Aug 12, 2023
2 parents a051d9a + af705c0 commit 99a88c8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
39 changes: 29 additions & 10 deletions custom_components/vicare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@

from PyViCare.PyViCare import PyViCare
from PyViCare.PyViCareDevice import Device
from PyViCare.PyViCareUtils import (
PyViCareInternalServerError,
PyViCareInvalidCredentialsError,
PyViCareRateLimitError,
)
import requests

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.storage import STORAGE_DIR

Expand Down Expand Up @@ -105,19 +112,27 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN] = {}
hass.data[DOMAIN][entry.entry_id] = {}

await hass.async_add_executor_job(setup_vicare_api, hass, entry)
try:
await hass.async_add_executor_job(setup_vicare_api, hass, entry)

await _async_migrate_entries(hass, entry)
await _async_migrate_entries(hass, entry)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


def vicare_login(hass, entry_data):
return True
except PyViCareInvalidCredentialsError as err:
raise ConfigEntryAuthFailed from err
except PyViCareRateLimitError as err:
raise ConfigEntryNotReady from err
except PyViCareInternalServerError as err:
raise ConfigEntryNotReady from err
except requests.exceptions.ConnectionError as err:
raise ConfigEntryNotReady from err

def vicare_login(hass, entry_data, scan_interval = DEFAULT_SCAN_INTERVAL):
"""Login via PyVicare API."""
vicare_api = PyViCare()
vicare_api.setCacheDuration(DEFAULT_SCAN_INTERVAL)
vicare_api.setCacheDuration(scan_interval)
vicare_api.initWithCredentials(
entry_data[CONF_USERNAME],
entry_data[CONF_PASSWORD],
Expand All @@ -130,9 +145,13 @@ def vicare_login(hass, entry_data):
def setup_vicare_api(hass, entry):
"""Set up PyVicare API."""
vicare_api = vicare_login(hass, entry.data)
scan_interval = max(DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL * len(vicare_api.devices))

# Readjust scan interval: each device has its own API endpoint
vicare_api.setCacheDuration(DEFAULT_SCAN_INTERVAL * len(vicare_api.devices))
_LOGGER.info(
"Setting up API with scan interval %i seconds.", scan_interval
)

vicare_api = vicare_login(hass, entry.data, scan_interval)

for device in vicare_api.devices:
_LOGGER.info(
Expand Down
18 changes: 17 additions & 1 deletion custom_components/vicare/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import logging
from typing import Any

from PyViCare.PyViCareUtils import PyViCareInvalidCredentialsError
from PyViCare.PyViCareUtils import (
PyViCareInternalServerError,
PyViCareInvalidCredentialsError,
PyViCareRateLimitError,
)
import requests
import voluptuous as vol

from homeassistant import config_entries
Expand Down Expand Up @@ -38,6 +43,7 @@ async def async_step_user(
vol.Required(CONF_CLIENT_ID): cv.string,
}
errors: dict[str, str] = {}
description_placeholders: dict[str, str] = {}

if user_input is not None:
try:
Expand All @@ -46,13 +52,23 @@ async def async_step_user(
)
except PyViCareInvalidCredentialsError:
errors["base"] = "invalid_auth"
except PyViCareInternalServerError as err:
errors["base"] = "server_error"
description_placeholders = {"error": str(err)}
except requests.exceptions.ConnectionError as err:
errors["base"] = "cannot_connect"
description_placeholders = {"error": str(err)}
except PyViCareRateLimitError as err:
errors["base"] = "too_many_attempts"
description_placeholders = {"error": str(err)}
else:
return self.async_create_entry(title=VICARE_NAME, data=user_input)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(data_schema),
errors=errors,
description_placeholders = description_placeholders
)

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
Expand Down
5 changes: 4 additions & 1 deletion custom_components/vicare/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
}
},
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
"cannot_connect": "Failed to connect: {error}",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"too_many_attempts": "Too many attempts, temporarily banned: {error}",
"server_error": "ViCare server error: {error}"
},
"abort": {
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]",
Expand Down
43 changes: 23 additions & 20 deletions custom_components/vicare/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
{
"config": {
"abort": {
"single_instance_allowed": "Already configured. Only a single configuration possible.",
"unknown": "Unexpected error"
"config": {
"abort": {
"single_instance_allowed": "Already configured. Only a single configuration possible.",
"unknown": "Unexpected error"
},
"error": {
"cannot_connect": "Failed to connect: {error}",
"invalid_auth": "Invalid authentication",
"too_many_attempts": "Too many attempts, temporarily banned: {error}",
"server_error": "ViCare server error: {error}"
},
"flow_title": "{name} ({host})",
"step": {
"user": {
"data": {
"client_id": "API Key",
"heating_type": "Heating type",
"password": "Password",
"username": "Email"
},
"error": {
"invalid_auth": "Invalid authentication"
},
"flow_title": "{name} ({host})",
"step": {
"user": {
"data": {
"client_id": "API Key",
"heating_type": "Heating type",
"password": "Password",
"username": "Email"
},
"description": "Set up ViCare integration. To generate API key go to https://developer.viessmann.com"
}
}
"description": "Set up ViCare integration. To generate API key go to https://developer.viessmann.com"
}
}
}
}
}

1 comment on commit 99a88c8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.