1 """VeSync integration."""
5 from pyvesync
import VeSync
12 from .common
import async_process_devices
24 PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
26 _LOGGER = logging.getLogger(__name__)
30 """Set up Vesync as config entry."""
31 username = config_entry.data[CONF_USERNAME]
32 password = config_entry.data[CONF_PASSWORD]
34 time_zone =
str(hass.config.time_zone)
36 manager = VeSync(username, password, time_zone)
38 login = await hass.async_add_executor_job(manager.login)
41 _LOGGER.error(
"Unable to login to the VeSync server")
46 forward_setups = hass.config_entries.async_forward_entry_setups
48 hass.data[DOMAIN] = {}
49 hass.data[DOMAIN][VS_MANAGER] = manager
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] = []
57 if device_dict[VS_SWITCHES]:
58 switches.extend(device_dict[VS_SWITCHES])
59 platforms.append(Platform.SWITCH)
61 if device_dict[VS_FANS]:
62 fans.extend(device_dict[VS_FANS])
63 platforms.append(Platform.FAN)
65 if device_dict[VS_LIGHTS]:
66 lights.extend(device_dict[VS_LIGHTS])
67 platforms.append(Platform.LIGHT)
69 if device_dict[VS_SENSORS]:
70 sensors.extend(device_dict[VS_SENSORS])
71 platforms.append(Platform.SENSOR)
73 await hass.config_entries.async_forward_entry_setups(config_entry, platforms)
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]
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, [])
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)
95 if new_switches
and not switches:
96 switches.extend(new_switches)
97 hass.async_create_task(forward_setups(config_entry, [Platform.SWITCH]))
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)
105 if new_fans
and not fans:
106 fans.extend(new_fans)
107 hass.async_create_task(forward_setups(config_entry, [Platform.FAN]))
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)
115 if new_lights
and not lights:
116 lights.extend(new_lights)
117 hass.async_create_task(forward_setups(config_entry, [Platform.LIGHT]))
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)
125 if new_sensors
and not sensors:
126 sensors.extend(new_sensors)
127 hass.async_create_task(forward_setups(config_entry, [Platform.SENSOR]))
129 hass.services.async_register(
130 DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
137 """Unload a config entry."""
138 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
140 hass.data.pop(DOMAIN)
def async_process_devices(hass, manager)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)