Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """VeSync integration."""
2 
3 import logging
4 
5 from pyvesync import VeSync
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
9 from homeassistant.core import HomeAssistant, ServiceCall
10 from homeassistant.helpers.dispatcher import async_dispatcher_send
11 
12 from .common import async_process_devices
13 from .const import (
14  DOMAIN,
15  SERVICE_UPDATE_DEVS,
16  VS_DISCOVERY,
17  VS_FANS,
18  VS_LIGHTS,
19  VS_MANAGER,
20  VS_SENSORS,
21  VS_SWITCHES,
22 )
23 
24 PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
29 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
30  """Set up Vesync as config entry."""
31  username = config_entry.data[CONF_USERNAME]
32  password = config_entry.data[CONF_PASSWORD]
33 
34  time_zone = str(hass.config.time_zone)
35 
36  manager = VeSync(username, password, time_zone)
37 
38  login = await hass.async_add_executor_job(manager.login)
39 
40  if not login:
41  _LOGGER.error("Unable to login to the VeSync server")
42  return False
43 
44  device_dict = await async_process_devices(hass, manager)
45 
46  forward_setups = hass.config_entries.async_forward_entry_setups
47 
48  hass.data[DOMAIN] = {}
49  hass.data[DOMAIN][VS_MANAGER] = manager
50 
51  switches = hass.data[DOMAIN][VS_SWITCHES] = []
52  fans = hass.data[DOMAIN][VS_FANS] = []
53  lights = hass.data[DOMAIN][VS_LIGHTS] = []
54  sensors = hass.data[DOMAIN][VS_SENSORS] = []
55  platforms = []
56 
57  if device_dict[VS_SWITCHES]:
58  switches.extend(device_dict[VS_SWITCHES])
59  platforms.append(Platform.SWITCH)
60 
61  if device_dict[VS_FANS]:
62  fans.extend(device_dict[VS_FANS])
63  platforms.append(Platform.FAN)
64 
65  if device_dict[VS_LIGHTS]:
66  lights.extend(device_dict[VS_LIGHTS])
67  platforms.append(Platform.LIGHT)
68 
69  if device_dict[VS_SENSORS]:
70  sensors.extend(device_dict[VS_SENSORS])
71  platforms.append(Platform.SENSOR)
72 
73  await hass.config_entries.async_forward_entry_setups(config_entry, platforms)
74 
75  async def async_new_device_discovery(service: ServiceCall) -> None:
76  """Discover if new devices should be added."""
77  manager = hass.data[DOMAIN][VS_MANAGER]
78  switches = hass.data[DOMAIN][VS_SWITCHES]
79  fans = hass.data[DOMAIN][VS_FANS]
80  lights = hass.data[DOMAIN][VS_LIGHTS]
81  sensors = hass.data[DOMAIN][VS_SENSORS]
82 
83  dev_dict = await async_process_devices(hass, manager)
84  switch_devs = dev_dict.get(VS_SWITCHES, [])
85  fan_devs = dev_dict.get(VS_FANS, [])
86  light_devs = dev_dict.get(VS_LIGHTS, [])
87  sensor_devs = dev_dict.get(VS_SENSORS, [])
88 
89  switch_set = set(switch_devs)
90  new_switches = list(switch_set.difference(switches))
91  if new_switches and switches:
92  switches.extend(new_switches)
93  async_dispatcher_send(hass, VS_DISCOVERY.format(VS_SWITCHES), new_switches)
94  return
95  if new_switches and not switches:
96  switches.extend(new_switches)
97  hass.async_create_task(forward_setups(config_entry, [Platform.SWITCH]))
98 
99  fan_set = set(fan_devs)
100  new_fans = list(fan_set.difference(fans))
101  if new_fans and fans:
102  fans.extend(new_fans)
103  async_dispatcher_send(hass, VS_DISCOVERY.format(VS_FANS), new_fans)
104  return
105  if new_fans and not fans:
106  fans.extend(new_fans)
107  hass.async_create_task(forward_setups(config_entry, [Platform.FAN]))
108 
109  light_set = set(light_devs)
110  new_lights = list(light_set.difference(lights))
111  if new_lights and lights:
112  lights.extend(new_lights)
113  async_dispatcher_send(hass, VS_DISCOVERY.format(VS_LIGHTS), new_lights)
114  return
115  if new_lights and not lights:
116  lights.extend(new_lights)
117  hass.async_create_task(forward_setups(config_entry, [Platform.LIGHT]))
118 
119  sensor_set = set(sensor_devs)
120  new_sensors = list(sensor_set.difference(sensors))
121  if new_sensors and sensors:
122  sensors.extend(new_sensors)
123  async_dispatcher_send(hass, VS_DISCOVERY.format(VS_SENSORS), new_sensors)
124  return
125  if new_sensors and not sensors:
126  sensors.extend(new_sensors)
127  hass.async_create_task(forward_setups(config_entry, [Platform.SENSOR]))
128 
129  hass.services.async_register(
130  DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
131  )
132 
133  return True
134 
135 
136 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
137  """Unload a config entry."""
138  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
139  if unload_ok:
140  hass.data.pop(DOMAIN)
141 
142  return unload_ok
def async_process_devices(hass, manager)
Definition: common.py:10
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:136
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:29
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193