mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +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."""
|
"""The Homewizard integration."""
|
||||||
|
|
||||||
|
from homewizard_energy import HomeWizardEnergy, HomeWizardEnergyV1, HomeWizardEnergyV2
|
||||||
|
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
||||||
|
from homeassistant.const import CONF_IP_ADDRESS, CONF_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DOMAIN, PLATFORMS
|
from .const import DOMAIN, PLATFORMS
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
@ -12,7 +16,22 @@ type HomeWizardConfigEntry = ConfigEntry[HWEnergyDeviceUpdateCoordinator]
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: HomeWizardConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: HomeWizardConfigEntry) -> bool:
|
||||||
"""Set up Homewizard from a config entry."""
|
"""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:
|
try:
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
|
@ -2,14 +2,12 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
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.errors import DisabledError, RequestError
|
||||||
from homewizard_energy.models import CombinedModels as DeviceResponseEntry
|
from homewizard_energy.models import CombinedModels as DeviceResponseEntry
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_IP_ADDRESS
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import DOMAIN, LOGGER, UPDATE_INTERVAL
|
from .const import DOMAIN, LOGGER, UPDATE_INTERVAL
|
||||||
@ -23,16 +21,10 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
|
|||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: ConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, hass: HomeAssistant, api: HomeWizardEnergy) -> None:
|
||||||
self,
|
|
||||||
hass: HomeAssistant,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize update coordinator."""
|
"""Initialize update coordinator."""
|
||||||
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
||||||
self.api = HomeWizardEnergyV1(
|
self.api = api
|
||||||
self.config_entry.data[CONF_IP_ADDRESS],
|
|
||||||
clientsession=async_get_clientsession(hass),
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _async_update_data(self) -> DeviceResponseEntry:
|
async def _async_update_data(self) -> DeviceResponseEntry:
|
||||||
"""Fetch all device and sensor data from api."""
|
"""Fetch all device and sensor data from api."""
|
||||||
|
@ -26,7 +26,7 @@ def mock_homewizardenergy(
|
|||||||
"""Return a mock bridge."""
|
"""Return a mock bridge."""
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.homewizard.coordinator.HomeWizardEnergyV1",
|
"homeassistant.components.homewizard.HomeWizardEnergyV1",
|
||||||
autospec=True,
|
autospec=True,
|
||||||
) as homewizard,
|
) as homewizard,
|
||||||
patch(
|
patch(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user