Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions homeassistant/components/vesync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pyvesync import VeSync
from pyvesync.utils.errors import VeSyncLoginError

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
Expand All @@ -14,8 +13,8 @@
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.typing import ConfigType

from .const import DOMAIN, VS_COORDINATOR, VS_MANAGER
from .coordinator import VeSyncDataCoordinator
from .const import DOMAIN
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
from .services import async_setup_services

CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
Expand Down Expand Up @@ -43,7 +42,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: VesyncConfigEntry
) -> bool:
"""Set up Vesync as config entry."""
username = config_entry.data[CONF_USERNAME]
password = config_entry.data[CONF_PASSWORD]
Expand All @@ -61,31 +62,24 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
except VeSyncLoginError as err:
raise ConfigEntryAuthFailed from err

hass.data[DOMAIN] = {}
hass.data[DOMAIN][VS_MANAGER] = manager

coordinator = VeSyncDataCoordinator(hass, config_entry, manager)

# Store coordinator at domain level since only single integration instance is permitted.
hass.data[DOMAIN][VS_COORDINATOR] = coordinator
await manager.update()
await manager.check_firmware()

config_entry.runtime_data = VeSyncDataCoordinator(hass, config_entry, manager)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there a reason we are not building the manager inside the coordinator now and to https://github.com/cdnninja/core/blob/5614518710c77b1e190f313e944dcf03c72b5fb9/homeassistant/components/vesync/__init__.py#L60-L65 within _async_setup of the coordinator

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could move it in. Only thought is that #156663 (comment) comment is referencing adding a second coordinator. Both will need access to the manager. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Then it probably doesn’t make sense


await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: VesyncConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data.pop(DOMAIN)

return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_migrate_entry(
hass: HomeAssistant, config_entry: VesyncConfigEntry
) -> bool:
"""Migrate old entry."""
_LOGGER.debug(
"Migrating VeSync config entry: %s minor version: %s",
Expand Down Expand Up @@ -120,10 +114,10 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->


async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
hass: HomeAssistant, config_entry: VesyncConfigEntry, device_entry: DeviceEntry
) -> bool:
"""Remove a config entry from a device."""
manager = hass.data[DOMAIN][VS_MANAGER]
manager = config_entry.runtime_data.manager
await manager.get_devices()
for dev in manager.devices:
if isinstance(dev.sub_device_no, int):
Expand Down
14 changes: 7 additions & 7 deletions homeassistant/components/vesync/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import logging

from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
from pyvesync.device_container import DeviceContainer

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .common import rgetattr
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
from .coordinator import VeSyncDataCoordinator
from .const import VS_DEVICES, VS_DISCOVERY
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
from .entity import VeSyncBaseEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -56,12 +56,12 @@ class VeSyncBinarySensorEntityDescription(BinarySensorEntityDescription):

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: VesyncConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up binary_sensor platform."""

coordinator = hass.data[DOMAIN][VS_COORDINATOR]
coordinator = config_entry.runtime_data

@callback
def discover(devices: list[VeSyncBaseDevice]) -> None:
Expand All @@ -73,13 +73,13 @@ def discover(devices: list[VeSyncBaseDevice]) -> None:
)

_setup_entities(
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
)


@callback
def _setup_entities(
devices: list[VeSyncBaseDevice],
devices: DeviceContainer | list[VeSyncBaseDevice],
async_add_entities: AddConfigEntryEntitiesCallback,
coordinator: VeSyncDataCoordinator,
) -> None:
Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/vesync/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
in this case every 6 hours.
"""
VS_DEVICES = "devices"
VS_COORDINATOR = "coordinator"
VS_MANAGER = "manager"
VS_LISTENERS = "listeners"
VS_NUMBERS = "numbers"

Expand Down
12 changes: 7 additions & 5 deletions homeassistant/components/vesync/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@

_LOGGER = logging.getLogger(__name__)

type VesyncConfigEntry = ConfigEntry[VeSyncDataCoordinator]


class VeSyncDataCoordinator(DataUpdateCoordinator[None]):
"""Class representing data coordinator for VeSync devices."""

config_entry: ConfigEntry
config_entry: VesyncConfigEntry
update_time: datetime | None = None

def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, manager: VeSync
self, hass: HomeAssistant, config_entry: VesyncConfigEntry, manager: VeSync
) -> None:
"""Initialize."""
self._manager = manager
self.manager = manager

super().__init__(
hass,
Expand All @@ -48,9 +50,9 @@ def should_update_energy(self) -> bool:
async def _async_update_data(self) -> None:
"""Fetch data from API endpoint."""

await self._manager.update_all_devices()
await self.manager.update_all_devices()

if self.should_update_energy():
self.update_time = datetime.now()
for outlet in self._manager.devices.outlets:
for outlet in self.manager.devices.outlets:
await outlet.update_energy()
12 changes: 6 additions & 6 deletions homeassistant/components/vesync/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
from pyvesync import VeSync

from homeassistant.components.diagnostics import REDACTED
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry

from .const import DOMAIN, VS_MANAGER
from .const import DOMAIN
from .coordinator import VesyncConfigEntry

KEYS_TO_REDACT = {"manager", "uuid", "mac_id"}


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, config_entry: VesyncConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
manager: VeSync = hass.data[DOMAIN][VS_MANAGER]
manager: VeSync = config_entry.runtime_data.manager

return {
DOMAIN: {
Expand All @@ -39,10 +39,10 @@ async def async_get_config_entry_diagnostics(


async def async_get_device_diagnostics(
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
hass: HomeAssistant, config_entry: VesyncConfigEntry, device: DeviceEntry
) -> dict[str, Any]:
"""Return diagnostics for a device entry."""
manager: VeSync = hass.data[DOMAIN][VS_MANAGER]
manager: VeSync = config_entry.runtime_data.manager
vesync_device_id = next(iden[1] for iden in device.identifiers if iden[0] == DOMAIN)

def get_vesync_unique_id(dev: Any) -> str:
Expand Down
15 changes: 6 additions & 9 deletions homeassistant/components/vesync/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from typing import Any

from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
from pyvesync.device_container import DeviceContainer

from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand All @@ -20,8 +20,6 @@

from .common import is_fan, is_purifier, rgetattr
from .const import (
DOMAIN,
VS_COORDINATOR,
VS_DEVICES,
VS_DISCOVERY,
VS_FAN_MODE_ADVANCED_SLEEP,
Expand All @@ -32,22 +30,21 @@
VS_FAN_MODE_PRESET_LIST_HA,
VS_FAN_MODE_SLEEP,
VS_FAN_MODE_TURBO,
VS_MANAGER,
)
from .coordinator import VeSyncDataCoordinator
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
from .entity import VeSyncBaseEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: VesyncConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the VeSync fan platform."""

coordinator = hass.data[DOMAIN][VS_COORDINATOR]
coordinator = config_entry.runtime_data

@callback
def discover(devices: list[VeSyncBaseDevice]) -> None:
Expand All @@ -59,13 +56,13 @@ def discover(devices: list[VeSyncBaseDevice]) -> None:
)

_setup_entities(
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
)


@callback
def _setup_entities(
devices: list[VeSyncBaseDevice],
devices: DeviceContainer | list[VeSyncBaseDevice],
async_add_entities: AddConfigEntryEntitiesCallback,
coordinator: VeSyncDataCoordinator,
) -> None:
Expand Down
12 changes: 4 additions & 8 deletions homeassistant/components/vesync/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,20 @@
HumidifierEntity,
HumidifierEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .const import (
DOMAIN,
VS_COORDINATOR,
VS_DEVICES,
VS_DISCOVERY,
VS_HUMIDIFIER_MODE_AUTO,
VS_HUMIDIFIER_MODE_HUMIDITY,
VS_HUMIDIFIER_MODE_MANUAL,
VS_HUMIDIFIER_MODE_SLEEP,
VS_MANAGER,
)
from .coordinator import VeSyncDataCoordinator
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
from .entity import VeSyncBaseEntity

_LOGGER = logging.getLogger(__name__)
Expand All @@ -45,12 +41,12 @@

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: VesyncConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the VeSync humidifier platform."""

coordinator = hass.data[DOMAIN][VS_COORDINATOR]
coordinator = config_entry.runtime_data

@callback
def discover(devices: list[VeSyncBaseDevice]) -> None:
Expand All @@ -62,7 +58,7 @@ def discover(devices: list[VeSyncBaseDevice]) -> None:
)

_setup_entities(
hass.data[DOMAIN][VS_MANAGER].devices.humidifiers,
config_entry.runtime_data.manager.devices.humidifiers,
async_add_entities,
coordinator,
)
Expand Down
14 changes: 7 additions & 7 deletions homeassistant/components/vesync/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
from pyvesync.base_devices.bulb_base import VeSyncBulb
from pyvesync.base_devices.switch_base import VeSyncSwitch
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
from pyvesync.device_container import DeviceContainer

from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP_KELVIN,
ColorMode,
LightEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import color as color_util

from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
from .coordinator import VeSyncDataCoordinator
from .const import VS_DEVICES, VS_DISCOVERY
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
from .entity import VeSyncBaseEntity

_LOGGER = logging.getLogger(__name__)
Expand All @@ -30,12 +30,12 @@

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: VesyncConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up lights."""

coordinator = hass.data[DOMAIN][VS_COORDINATOR]
coordinator = config_entry.runtime_data

@callback
def discover(devices: list[VeSyncBaseDevice]) -> None:
Expand All @@ -47,13 +47,13 @@ def discover(devices: list[VeSyncBaseDevice]) -> None:
)

_setup_entities(
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
)


@callback
def _setup_entities(
devices: list[VeSyncBaseDevice],
devices: DeviceContainer | list[VeSyncBaseDevice],
async_add_entities: AddConfigEntryEntitiesCallback,
coordinator: VeSyncDataCoordinator,
) -> None:
Expand Down
Loading