Fulfill IQS rule runtime-data in ViCare integration (#133633)
parent
353f085474
commit
67f0de441b
|
@ -2,11 +2,9 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from contextlib import suppress
|
||||
import logging
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from PyViCare.PyViCare import PyViCare
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
|
@ -16,8 +14,6 @@ from PyViCare.PyViCareUtils import (
|
|||
)
|
||||
|
||||
from homeassistant.components.climate import DOMAIN as DOMAIN_CLIMATE
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
@ -25,31 +21,28 @@ from homeassistant.helpers.storage import STORAGE_DIR
|
|||
|
||||
from .const import (
|
||||
DEFAULT_CACHE_DURATION,
|
||||
DEVICE_LIST,
|
||||
DOMAIN,
|
||||
PLATFORMS,
|
||||
UNSUPPORTED_DEVICES,
|
||||
VICARE_TOKEN_FILENAME,
|
||||
)
|
||||
from .types import ViCareDevice
|
||||
from .utils import get_device, get_device_serial
|
||||
from .types import ViCareConfigEntry, ViCareData, ViCareDevice
|
||||
from .utils import get_device, get_device_serial, login
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_TOKEN_FILENAME = "vicare_token.save"
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ViCareConfigEntry) -> bool:
|
||||
"""Set up from config entry."""
|
||||
_LOGGER.debug("Setting up ViCare component")
|
||||
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][entry.entry_id] = {}
|
||||
|
||||
try:
|
||||
await hass.async_add_executor_job(setup_vicare_api, hass, entry)
|
||||
entry.runtime_data = await hass.async_add_executor_job(
|
||||
setup_vicare_api, hass, entry
|
||||
)
|
||||
except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError) as err:
|
||||
raise ConfigEntryAuthFailed("Authentication failed") from err
|
||||
|
||||
for device in hass.data[DOMAIN][entry.entry_id][DEVICE_LIST]:
|
||||
for device in entry.runtime_data.devices:
|
||||
# Migration can be removed in 2025.4.0
|
||||
await async_migrate_devices_and_entities(hass, entry, device)
|
||||
|
||||
|
@ -58,28 +51,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
def vicare_login(
|
||||
hass: HomeAssistant,
|
||||
entry_data: Mapping[str, Any],
|
||||
cache_duration=DEFAULT_CACHE_DURATION,
|
||||
) -> PyViCare:
|
||||
"""Login via PyVicare API."""
|
||||
vicare_api = PyViCare()
|
||||
vicare_api.setCacheDuration(cache_duration)
|
||||
vicare_api.initWithCredentials(
|
||||
entry_data[CONF_USERNAME],
|
||||
entry_data[CONF_PASSWORD],
|
||||
entry_data[CONF_CLIENT_ID],
|
||||
hass.config.path(STORAGE_DIR, _TOKEN_FILENAME),
|
||||
)
|
||||
return vicare_api
|
||||
|
||||
|
||||
def setup_vicare_api(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
def setup_vicare_api(hass: HomeAssistant, entry: ViCareConfigEntry) -> PyViCare:
|
||||
"""Set up PyVicare API."""
|
||||
vicare_api = vicare_login(hass, entry.data)
|
||||
client = login(hass, entry.data)
|
||||
|
||||
device_config_list = get_supported_devices(vicare_api.devices)
|
||||
device_config_list = get_supported_devices(client.devices)
|
||||
|
||||
# increase cache duration to fit rate limit to number of devices
|
||||
if (number_of_devices := len(device_config_list)) > 1:
|
||||
cache_duration = DEFAULT_CACHE_DURATION * number_of_devices
|
||||
_LOGGER.debug(
|
||||
|
@ -87,36 +65,35 @@ def setup_vicare_api(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|||
number_of_devices,
|
||||
cache_duration,
|
||||
)
|
||||
vicare_api = vicare_login(hass, entry.data, cache_duration)
|
||||
device_config_list = get_supported_devices(vicare_api.devices)
|
||||
client = login(hass, entry.data, cache_duration)
|
||||
device_config_list = get_supported_devices(client.devices)
|
||||
|
||||
for device in device_config_list:
|
||||
_LOGGER.debug(
|
||||
"Found device: %s (online: %s)", device.getModel(), str(device.isOnline())
|
||||
)
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id][DEVICE_LIST] = [
|
||||
devices = [
|
||||
ViCareDevice(config=device_config, api=get_device(entry, device_config))
|
||||
for device_config in device_config_list
|
||||
]
|
||||
return ViCareData(client=client, devices=devices)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ViCareConfigEntry) -> bool:
|
||||
"""Unload ViCare config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
with suppress(FileNotFoundError):
|
||||
await hass.async_add_executor_job(
|
||||
os.remove, hass.config.path(STORAGE_DIR, _TOKEN_FILENAME)
|
||||
os.remove, hass.config.path(STORAGE_DIR, VICARE_TOKEN_FILENAME)
|
||||
)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
async def async_migrate_devices_and_entities(
|
||||
hass: HomeAssistant, entry: ConfigEntry, device: ViCareDevice
|
||||
hass: HomeAssistant, entry: ViCareConfigEntry, device: ViCareDevice
|
||||
) -> None:
|
||||
"""Migrate old entry."""
|
||||
device_registry = dr.async_get(hass)
|
||||
|
|
|
@ -24,13 +24,11 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .entity import ViCareEntity
|
||||
from .types import ViCareDevice, ViCareRequiredKeysMixin
|
||||
from .types import ViCareConfigEntry, ViCareDevice, ViCareRequiredKeysMixin
|
||||
from .utils import (
|
||||
get_burners,
|
||||
get_circuits,
|
||||
|
@ -152,16 +150,14 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Create the ViCare binary sensor devices."""
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -16,14 +16,12 @@ from PyViCare.PyViCareUtils import (
|
|||
import requests
|
||||
|
||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .entity import ViCareEntity
|
||||
from .types import ViCareDevice, ViCareRequiredKeysMixinWithSet
|
||||
from .types import ViCareConfigEntry, ViCareDevice, ViCareRequiredKeysMixinWithSet
|
||||
from .utils import get_device_serial, is_supported
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -67,16 +65,14 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Create the ViCare button entities."""
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -24,7 +24,6 @@ from homeassistant.components.climate import (
|
|||
HVACAction,
|
||||
HVACMode,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_TEMPERATURE,
|
||||
PRECISION_TENTHS,
|
||||
|
@ -37,9 +36,9 @@ from homeassistant.helpers import entity_platform
|
|||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .const import DOMAIN
|
||||
from .entity import ViCareEntity
|
||||
from .types import HeatingProgram, ViCareDevice
|
||||
from .types import HeatingProgram, ViCareConfigEntry, ViCareDevice
|
||||
from .utils import get_burners, get_circuits, get_compressors, get_device_serial
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -99,25 +98,22 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the ViCare climate platform."""
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_SET_VICARE_MODE,
|
||||
{vol.Required(SERVICE_SET_VICARE_MODE_ATTR_MODE): cv.string},
|
||||
"set_vicare_mode",
|
||||
)
|
||||
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
|||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.device_registry import format_mac
|
||||
|
||||
from . import vicare_login
|
||||
from .const import (
|
||||
CONF_HEATING_TYPE,
|
||||
DEFAULT_HEATING_TYPE,
|
||||
|
@ -26,6 +25,7 @@ from .const import (
|
|||
VICARE_NAME,
|
||||
HeatingType,
|
||||
)
|
||||
from .utils import login
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -62,9 +62,7 @@ class ViCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
|
||||
if user_input is not None:
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
vicare_login, self.hass, user_input
|
||||
)
|
||||
await self.hass.async_add_executor_job(login, self.hass, user_input)
|
||||
except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError):
|
||||
errors["base"] = "invalid_auth"
|
||||
else:
|
||||
|
@ -96,7 +94,7 @@ class ViCareConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
}
|
||||
|
||||
try:
|
||||
await self.hass.async_add_executor_job(vicare_login, self.hass, data)
|
||||
await self.hass.async_add_executor_job(login, self.hass, data)
|
||||
except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError):
|
||||
errors["base"] = "invalid_auth"
|
||||
else:
|
||||
|
|
|
@ -25,8 +25,8 @@ UNSUPPORTED_DEVICES = [
|
|||
"E3_RoomControl_One_522",
|
||||
]
|
||||
|
||||
DEVICE_LIST = "device_list"
|
||||
VICARE_NAME = "ViCare"
|
||||
VICARE_TOKEN_FILENAME = "vicare_token.save"
|
||||
|
||||
CONF_CIRCUIT = "circuit"
|
||||
CONF_HEATING_TYPE = "heating_type"
|
||||
|
|
|
@ -6,25 +6,24 @@ import json
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .types import ViCareConfigEntry
|
||||
|
||||
TO_REDACT = {CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: ViCareConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
|
||||
def dump_devices() -> list[dict[str, Any]]:
|
||||
"""Dump devices."""
|
||||
return [
|
||||
json.loads(device.config.dump_secure())
|
||||
for device in hass.data[DOMAIN][entry.entry_id][DEVICE_LIST]
|
||||
json.loads(device.dump_secure())
|
||||
for device in entry.runtime_data.client.devices
|
||||
]
|
||||
|
||||
return {
|
||||
|
|
|
@ -19,7 +19,6 @@ from PyViCare.PyViCareVentilationDevice import (
|
|||
from requests.exceptions import ConnectionError as RequestConnectionError
|
||||
|
||||
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.percentage import (
|
||||
|
@ -27,9 +26,8 @@ from homeassistant.util.percentage import (
|
|||
percentage_to_ordered_list_item,
|
||||
)
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .entity import ViCareEntity
|
||||
from .types import ViCareDevice
|
||||
from .types import ViCareConfigEntry, ViCareDevice
|
||||
from .utils import get_device_serial
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -104,17 +102,14 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the ViCare fan platform."""
|
||||
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -25,14 +25,17 @@ from homeassistant.components.number import (
|
|||
NumberEntity,
|
||||
NumberEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .entity import ViCareEntity
|
||||
from .types import HeatingProgram, ViCareDevice, ViCareRequiredKeysMixin
|
||||
from .types import (
|
||||
HeatingProgram,
|
||||
ViCareConfigEntry,
|
||||
ViCareDevice,
|
||||
ViCareRequiredKeysMixin,
|
||||
)
|
||||
from .utils import get_circuits, get_device_serial, is_supported
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -370,16 +373,14 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Create the ViCare number devices."""
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -6,9 +6,7 @@ rules:
|
|||
status: todo
|
||||
comment: Uniqueness is not checked yet.
|
||||
config-flow-test-coverage: done
|
||||
runtime-data:
|
||||
status: todo
|
||||
comment: runtime_data is not used yet.
|
||||
runtime-data: done
|
||||
test-before-setup: done
|
||||
appropriate-polling: done
|
||||
entity-unique-id: done
|
||||
|
|
|
@ -25,7 +25,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
EntityCategory,
|
||||
|
@ -40,8 +39,6 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
DEVICE_LIST,
|
||||
DOMAIN,
|
||||
VICARE_CUBIC_METER,
|
||||
VICARE_KW,
|
||||
VICARE_KWH,
|
||||
|
@ -50,7 +47,7 @@ from .const import (
|
|||
VICARE_WH,
|
||||
)
|
||||
from .entity import ViCareEntity
|
||||
from .types import ViCareDevice, ViCareRequiredKeysMixin
|
||||
from .types import ViCareConfigEntry, ViCareDevice, ViCareRequiredKeysMixin
|
||||
from .utils import (
|
||||
get_burners,
|
||||
get_circuits,
|
||||
|
@ -968,16 +965,14 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Create the ViCare sensor devices."""
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
),
|
||||
# run update to have device_class set depending on unit_of_measurement
|
||||
True,
|
||||
|
|
|
@ -6,6 +6,7 @@ from dataclasses import dataclass
|
|||
import enum
|
||||
from typing import Any
|
||||
|
||||
from PyViCare.PyViCare import PyViCare
|
||||
from PyViCare.PyViCareDevice import Device as PyViCareDevice
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
|
||||
|
@ -15,6 +16,7 @@ from homeassistant.components.climate import (
|
|||
PRESET_HOME,
|
||||
PRESET_SLEEP,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
||||
|
||||
class HeatingProgram(enum.StrEnum):
|
||||
|
@ -80,6 +82,17 @@ class ViCareDevice:
|
|||
api: PyViCareDevice
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ViCareData:
|
||||
"""ViCare data class."""
|
||||
|
||||
client: PyViCare
|
||||
devices: list[ViCareDevice]
|
||||
|
||||
|
||||
type ViCareConfigEntry = ConfigEntry[ViCareData]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ViCareRequiredKeysMixin:
|
||||
"""Mixin for required keys."""
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
"""ViCare helpers functions."""
|
||||
|
||||
import logging
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from PyViCare.PyViCare import PyViCare
|
||||
from PyViCare.PyViCareDevice import Device as PyViCareDevice
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
from PyViCare.PyViCareHeatingDevice import (
|
||||
|
@ -14,16 +19,41 @@ from PyViCare.PyViCareUtils import (
|
|||
)
|
||||
import requests
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.storage import STORAGE_DIR
|
||||
|
||||
from .const import CONF_HEATING_TYPE, HEATING_TYPE_TO_CREATOR_METHOD, HeatingType
|
||||
from .types import ViCareRequiredKeysMixin
|
||||
from .const import (
|
||||
CONF_HEATING_TYPE,
|
||||
DEFAULT_CACHE_DURATION,
|
||||
HEATING_TYPE_TO_CREATOR_METHOD,
|
||||
VICARE_TOKEN_FILENAME,
|
||||
HeatingType,
|
||||
)
|
||||
from .types import ViCareConfigEntry, ViCareRequiredKeysMixin
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def login(
|
||||
hass: HomeAssistant,
|
||||
entry_data: Mapping[str, Any],
|
||||
cache_duration=DEFAULT_CACHE_DURATION,
|
||||
) -> PyViCare:
|
||||
"""Login via PyVicare API."""
|
||||
vicare_api = PyViCare()
|
||||
vicare_api.setCacheDuration(cache_duration)
|
||||
vicare_api.initWithCredentials(
|
||||
entry_data[CONF_USERNAME],
|
||||
entry_data[CONF_PASSWORD],
|
||||
entry_data[CONF_CLIENT_ID],
|
||||
hass.config.path(STORAGE_DIR, VICARE_TOKEN_FILENAME),
|
||||
)
|
||||
return vicare_api
|
||||
|
||||
|
||||
def get_device(
|
||||
entry: ConfigEntry, device_config: PyViCareDeviceConfig
|
||||
entry: ViCareConfigEntry, device_config: PyViCareDeviceConfig
|
||||
) -> PyViCareDevice:
|
||||
"""Get device for device config."""
|
||||
return getattr(
|
||||
|
|
|
@ -20,14 +20,12 @@ from homeassistant.components.water_heater import (
|
|||
WaterHeaterEntity,
|
||||
WaterHeaterEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_TENTHS, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DEVICE_LIST, DOMAIN
|
||||
from .entity import ViCareEntity
|
||||
from .types import ViCareDevice
|
||||
from .types import ViCareConfigEntry, ViCareDevice
|
||||
from .utils import get_circuits, get_device_serial
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -81,16 +79,14 @@ def _build_entities(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: ViCareConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the ViCare water heater platform."""
|
||||
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
|
||||
|
||||
async_add_entities(
|
||||
await hass.async_add_executor_job(
|
||||
_build_entities,
|
||||
device_list,
|
||||
config_entry.runtime_data.devices,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ async def mock_vicare_gas_boiler(
|
|||
"""Return a mocked ViCare API representing a single gas boiler device."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
with patch(
|
||||
f"{MODULE}.vicare_login",
|
||||
f"{MODULE}.login",
|
||||
return_value=MockPyViCare(fixtures),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
@ -102,7 +102,7 @@ async def mock_vicare_room_sensors(
|
|||
Fixture({"type:climateSensor"}, "vicare/RoomSensor2.json"),
|
||||
]
|
||||
with patch(
|
||||
f"{MODULE}.vicare_login",
|
||||
f"{MODULE}.login",
|
||||
return_value=MockPyViCare(fixtures),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -43,7 +43,7 @@ async def test_all_entities(
|
|||
"""Test all entities."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.BINARY_SENSOR]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -25,7 +25,7 @@ async def test_all_entities(
|
|||
"""Test all entities."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.BUTTON]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -25,7 +25,7 @@ async def test_all_entities(
|
|||
"""Test all entities."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.CLIMATE]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -49,7 +49,7 @@ async def test_user_create_entry(
|
|||
|
||||
# test PyViCareInvalidConfigurationError
|
||||
with patch(
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.login",
|
||||
side_effect=PyViCareInvalidConfigurationError(
|
||||
{"error": "foo", "error_description": "bar"}
|
||||
),
|
||||
|
@ -65,7 +65,7 @@ async def test_user_create_entry(
|
|||
|
||||
# test PyViCareInvalidCredentialsError
|
||||
with patch(
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.login",
|
||||
side_effect=PyViCareInvalidCredentialsError,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
|
@ -79,7 +79,7 @@ async def test_user_create_entry(
|
|||
|
||||
# test success
|
||||
with patch(
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.login",
|
||||
return_value=None,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
|
@ -110,7 +110,7 @@ async def test_step_reauth(hass: HomeAssistant, mock_setup_entry: AsyncMock) ->
|
|||
|
||||
# test PyViCareInvalidConfigurationError
|
||||
with patch(
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.login",
|
||||
side_effect=PyViCareInvalidConfigurationError(
|
||||
{"error": "foo", "error_description": "bar"}
|
||||
),
|
||||
|
@ -125,7 +125,7 @@ async def test_step_reauth(hass: HomeAssistant, mock_setup_entry: AsyncMock) ->
|
|||
|
||||
# test success
|
||||
with patch(
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.login",
|
||||
return_value=None,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
|
@ -160,7 +160,7 @@ async def test_form_dhcp(
|
|||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.login",
|
||||
return_value=None,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
|
|
|
@ -25,7 +25,7 @@ async def test_all_entities(
|
|||
"""Test all entities."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:ventilation"}, "vicare/ViAir300F.json")]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.FAN]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -26,7 +26,7 @@ async def test_device_and_entity_migration(
|
|||
Fixture({"type:boiler"}, "vicare/dummy-device-no-serial.json"),
|
||||
]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.CLIMATE]),
|
||||
):
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
|
|
@ -25,7 +25,7 @@ async def test_all_entities(
|
|||
"""Test all entities."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.NUMBER]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -27,7 +27,7 @@ async def test_all_entities(
|
|||
Fixture({"type:boiler"}, "vicare/Vitodens300W.json"),
|
||||
]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.SENSOR]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
@ -48,7 +48,7 @@ async def test_room_sensors(
|
|||
Fixture({"type:climateSensor"}, "vicare/RoomSensor2.json"),
|
||||
]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.SENSOR]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
|
@ -25,7 +25,7 @@ async def test_all_entities(
|
|||
"""Test all entities."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
with (
|
||||
patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.login", return_value=MockPyViCare(fixtures)),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.WATER_HEATER]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
|
Loading…
Reference in New Issue