Skip to content

Commit

Permalink
Simplify vesync init loading (home-assistant#135052)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdnninja authored Jan 10, 2025
1 parent 475a2fb commit bce7e9b
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 163 deletions.
109 changes: 17 additions & 92 deletions homeassistant/components/vesync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.dispatcher import async_dispatcher_send

from .common import async_process_devices
from .common import async_generate_device_list
from .const import (
DOMAIN,
SERVICE_UPDATE_DEVS,
VS_COORDINATOR,
VS_DEVICES,
VS_DISCOVERY,
VS_FANS,
VS_LIGHTS,
VS_MANAGER,
VS_SENSORS,
VS_SWITCHES,
)
from .coordinator import VeSyncDataCoordinator

Expand All @@ -43,10 +40,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
_LOGGER.error("Unable to login to the VeSync server")
return False

device_dict = await async_process_devices(hass, manager)

forward_setups = hass.config_entries.async_forward_entry_setups

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

Expand All @@ -55,83 +48,25 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
# Store coordinator at domain level since only single integration instance is permitted.
hass.data[DOMAIN][VS_COORDINATOR] = coordinator

switches = hass.data[DOMAIN][VS_SWITCHES] = []
fans = hass.data[DOMAIN][VS_FANS] = []
lights = hass.data[DOMAIN][VS_LIGHTS] = []
sensors = hass.data[DOMAIN][VS_SENSORS] = []
platforms = []

if device_dict[VS_SWITCHES]:
switches.extend(device_dict[VS_SWITCHES])
platforms.append(Platform.SWITCH)

if device_dict[VS_FANS]:
fans.extend(device_dict[VS_FANS])
platforms.append(Platform.FAN)
hass.data[DOMAIN][VS_DEVICES] = await async_generate_device_list(hass, manager)

if device_dict[VS_LIGHTS]:
lights.extend(device_dict[VS_LIGHTS])
platforms.append(Platform.LIGHT)

if device_dict[VS_SENSORS]:
sensors.extend(device_dict[VS_SENSORS])
platforms.append(Platform.SENSOR)

await hass.config_entries.async_forward_entry_setups(config_entry, platforms)
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

async def async_new_device_discovery(service: ServiceCall) -> None:
"""Discover if new devices should be added."""
manager = hass.data[DOMAIN][VS_MANAGER]
switches = hass.data[DOMAIN][VS_SWITCHES]
fans = hass.data[DOMAIN][VS_FANS]
lights = hass.data[DOMAIN][VS_LIGHTS]
sensors = hass.data[DOMAIN][VS_SENSORS]

dev_dict = await async_process_devices(hass, manager)
switch_devs = dev_dict.get(VS_SWITCHES, [])
fan_devs = dev_dict.get(VS_FANS, [])
light_devs = dev_dict.get(VS_LIGHTS, [])
sensor_devs = dev_dict.get(VS_SENSORS, [])

switch_set = set(switch_devs)
new_switches = list(switch_set.difference(switches))
if new_switches and switches:
switches.extend(new_switches)
async_dispatcher_send(hass, VS_DISCOVERY.format(VS_SWITCHES), new_switches)
return
if new_switches and not switches:
switches.extend(new_switches)
hass.async_create_task(forward_setups(config_entry, [Platform.SWITCH]))

fan_set = set(fan_devs)
new_fans = list(fan_set.difference(fans))
if new_fans and fans:
fans.extend(new_fans)
async_dispatcher_send(hass, VS_DISCOVERY.format(VS_FANS), new_fans)
return
if new_fans and not fans:
fans.extend(new_fans)
hass.async_create_task(forward_setups(config_entry, [Platform.FAN]))

light_set = set(light_devs)
new_lights = list(light_set.difference(lights))
if new_lights and lights:
lights.extend(new_lights)
async_dispatcher_send(hass, VS_DISCOVERY.format(VS_LIGHTS), new_lights)
return
if new_lights and not lights:
lights.extend(new_lights)
hass.async_create_task(forward_setups(config_entry, [Platform.LIGHT]))

sensor_set = set(sensor_devs)
new_sensors = list(sensor_set.difference(sensors))
if new_sensors and sensors:
sensors.extend(new_sensors)
async_dispatcher_send(hass, VS_DISCOVERY.format(VS_SENSORS), new_sensors)
devices = hass.data[DOMAIN][VS_DEVICES]

new_devices = await async_generate_device_list(hass, manager)

device_set = set(new_devices)
new_devices = list(device_set.difference(devices))
if new_devices and devices:
devices.extend(new_devices)
async_dispatcher_send(hass, VS_DISCOVERY.format(VS_DEVICES), new_devices)
return
if new_sensors and not sensors:
sensors.extend(new_sensors)
hass.async_create_task(forward_setups(config_entry, [Platform.SENSOR]))
if new_devices and not devices:
devices.extend(new_devices)

hass.services.async_register(
DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
Expand All @@ -142,18 +77,8 @@ async def async_new_device_discovery(service: ServiceCall) -> None:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
in_use_platforms = []
if hass.data[DOMAIN][VS_SWITCHES]:
in_use_platforms.append(Platform.SWITCH)
if hass.data[DOMAIN][VS_FANS]:
in_use_platforms.append(Platform.FAN)
if hass.data[DOMAIN][VS_LIGHTS]:
in_use_platforms.append(Platform.LIGHT)
if hass.data[DOMAIN][VS_SENSORS]:
in_use_platforms.append(Platform.SENSOR)
unload_ok = await hass.config_entries.async_unload_platforms(
entry, in_use_platforms
)

unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data.pop(DOMAIN)

Expand Down
39 changes: 7 additions & 32 deletions homeassistant/components/vesync/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,20 @@

from homeassistant.core import HomeAssistant

from .const import VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES

_LOGGER = logging.getLogger(__name__)


async def async_process_devices(
async def async_generate_device_list(
hass: HomeAssistant, manager: VeSync
) -> dict[str, list[VeSyncBaseDevice]]:
) -> list[VeSyncBaseDevice]:
"""Assign devices to proper component."""
devices: dict[str, list[VeSyncBaseDevice]] = {}
devices[VS_SWITCHES] = []
devices[VS_FANS] = []
devices[VS_LIGHTS] = []
devices[VS_SENSORS] = []
devices: list[VeSyncBaseDevice] = []

await hass.async_add_executor_job(manager.update)

if manager.fans:
devices[VS_FANS].extend(manager.fans)
# Expose fan sensors separately
devices[VS_SENSORS].extend(manager.fans)
_LOGGER.debug("%d VeSync fans found", len(manager.fans))

if manager.bulbs:
devices[VS_LIGHTS].extend(manager.bulbs)
_LOGGER.debug("%d VeSync lights found", len(manager.bulbs))

if manager.outlets:
devices[VS_SWITCHES].extend(manager.outlets)
# Expose outlets' voltage, power & energy usage as separate sensors
devices[VS_SENSORS].extend(manager.outlets)
_LOGGER.debug("%d VeSync outlets found", len(manager.outlets))

if manager.switches:
for switch in manager.switches:
if not switch.is_dimmable():
devices[VS_SWITCHES].append(switch)
else:
devices[VS_LIGHTS].append(switch)
_LOGGER.debug("%d VeSync switches found", len(manager.switches))
devices.extend(manager.fans)
devices.extend(manager.bulbs)
devices.extend(manager.outlets)
devices.extend(manager.switches)

return devices
6 changes: 1 addition & 5 deletions homeassistant/components/vesync/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
Using 30 seconds interval gives 8640 for 3 devices which
exceeds the quota of 7700.
"""

VS_SWITCHES = "switches"
VS_FANS = "fans"
VS_LIGHTS = "lights"
VS_SENSORS = "sensors"
VS_DEVICES = "devices"
VS_COORDINATOR = "coordinator"
VS_MANAGER = "manager"

Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/vesync/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
DOMAIN,
SKU_TO_BASE_DEVICE,
VS_COORDINATOR,
VS_DEVICES,
VS_DISCOVERY,
VS_FANS,
)
from .coordinator import VeSyncDataCoordinator
from .entity import VeSyncBaseEntity
Expand Down Expand Up @@ -74,10 +74,10 @@ def discover(devices):
_setup_entities(devices, async_add_entities, coordinator)

config_entry.async_on_unload(
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_FANS), discover)
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_DEVICES), discover)
)

_setup_entities(hass.data[DOMAIN][VS_FANS], async_add_entities, coordinator)
_setup_entities(hass.data[DOMAIN][VS_DEVICES], async_add_entities, coordinator)


@callback
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/vesync/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import color as color_util

from .const import DEV_TYPE_TO_HA, DOMAIN, VS_COORDINATOR, VS_DISCOVERY, VS_LIGHTS
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY
from .coordinator import VeSyncDataCoordinator
from .entity import VeSyncBaseEntity

Expand All @@ -41,10 +41,10 @@ def discover(devices):
_setup_entities(devices, async_add_entities, coordinator)

config_entry.async_on_unload(
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_LIGHTS), discover)
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_DEVICES), discover)
)

_setup_entities(hass.data[DOMAIN][VS_LIGHTS], async_add_entities, coordinator)
_setup_entities(hass.data[DOMAIN][VS_DEVICES], async_add_entities, coordinator)


@callback
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/vesync/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
DOMAIN,
SKU_TO_BASE_DEVICE,
VS_COORDINATOR,
VS_DEVICES,
VS_DISCOVERY,
VS_SENSORS,
)
from .coordinator import VeSyncDataCoordinator
from .entity import VeSyncBaseEntity
Expand Down Expand Up @@ -204,10 +204,10 @@ def discover(devices):
_setup_entities(devices, async_add_entities, coordinator)

config_entry.async_on_unload(
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SENSORS), discover)
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_DEVICES), discover)
)

_setup_entities(hass.data[DOMAIN][VS_SENSORS], async_add_entities, coordinator)
_setup_entities(hass.data[DOMAIN][VS_DEVICES], async_add_entities, coordinator)


@callback
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/vesync/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DEV_TYPE_TO_HA, DOMAIN, VS_COORDINATOR, VS_DISCOVERY, VS_SWITCHES
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY
from .coordinator import VeSyncDataCoordinator
from .entity import VeSyncBaseEntity

Expand All @@ -33,10 +33,10 @@ def discover(devices):
_setup_entities(devices, async_add_entities, coordinator)

config_entry.async_on_unload(
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SWITCHES), discover)
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_DEVICES), discover)
)

_setup_entities(hass.data[DOMAIN][VS_SWITCHES], async_add_entities, coordinator)
_setup_entities(hass.data[DOMAIN][VS_DEVICES], async_add_entities, coordinator)


@callback
Expand Down
Loading

0 comments on commit bce7e9b

Please sign in to comment.