mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Move HomeWizard API initialisation to async_setup_entry (#135315)
This commit is contained in:
parent
4dbf2b0320
commit
8f71d7a6f3
@ -1,8 +1,12 @@
|
||||
"""The Homewizard integration."""
|
||||
|
||||
from homewizard_energy import HomeWizardEnergy, HomeWizardEnergyV1, HomeWizardEnergyV2
|
||||
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_TOKEN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN, PLATFORMS
|
||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||
@ -12,7 +16,22 @@ type HomeWizardConfigEntry = ConfigEntry[HWEnergyDeviceUpdateCoordinator]
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: HomeWizardConfigEntry) -> bool:
|
||||
"""Set up Homewizard from a config entry."""
|
||||
coordinator = HWEnergyDeviceUpdateCoordinator(hass)
|
||||
|
||||
api: HomeWizardEnergy
|
||||
|
||||
if token := entry.data.get(CONF_TOKEN):
|
||||
api = HomeWizardEnergyV2(
|
||||
entry.data[CONF_IP_ADDRESS],
|
||||
token=token,
|
||||
clientsession=async_get_clientsession(hass),
|
||||
)
|
||||
else:
|
||||
api = HomeWizardEnergyV1(
|
||||
entry.data[CONF_IP_ADDRESS],
|
||||
clientsession=async_get_clientsession(hass),
|
||||
)
|
||||
|
||||
coordinator = HWEnergyDeviceUpdateCoordinator(hass, api)
|
||||
try:
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
|
@ -2,14 +2,12 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homewizard_energy import HomeWizardEnergy, HomeWizardEnergyV1
|
||||
from homewizard_energy import HomeWizardEnergy
|
||||
from homewizard_energy.errors import DisabledError, RequestError
|
||||
from homewizard_energy.models import CombinedModels as DeviceResponseEntry
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_IP_ADDRESS
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN, LOGGER, UPDATE_INTERVAL
|
||||
@ -23,16 +21,10 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
|
||||
|
||||
config_entry: ConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
def __init__(self, hass: HomeAssistant, api: HomeWizardEnergy) -> None:
|
||||
"""Initialize update coordinator."""
|
||||
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
||||
self.api = HomeWizardEnergyV1(
|
||||
self.config_entry.data[CONF_IP_ADDRESS],
|
||||
clientsession=async_get_clientsession(hass),
|
||||
)
|
||||
self.api = api
|
||||
|
||||
async def _async_update_data(self) -> DeviceResponseEntry:
|
||||
"""Fetch all device and sensor data from api."""
|
||||
|
@ -26,7 +26,7 @@ def mock_homewizardenergy(
|
||||
"""Return a mock bridge."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.homewizard.coordinator.HomeWizardEnergyV1",
|
||||
"homeassistant.components.homewizard.HomeWizardEnergyV1",
|
||||
autospec=True,
|
||||
) as homewizard,
|
||||
patch(
|
||||
|
Loading…
x
Reference in New Issue
Block a user