Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -8,17 +8,28 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from .common import async_process_devices
|
||||
from .config_flow import configured_instances
|
||||
from .const import (DOMAIN, VS_DISPATCHERS, VS_DISCOVERY, VS_SWITCHES,
|
||||
SERVICE_UPDATE_DEVS, VS_MANAGER)
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
VS_DISPATCHERS,
|
||||
VS_DISCOVERY,
|
||||
VS_SWITCHES,
|
||||
SERVICE_UPDATE_DEVS,
|
||||
VS_MANAGER,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Required(CONF_USERNAME): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_USERNAME): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
}
|
||||
)
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
@@ -32,11 +43,13 @@ async def async_setup(hass, config):
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={'source': SOURCE_IMPORT},
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_USERNAME: conf[CONF_USERNAME],
|
||||
CONF_PASSWORD: conf[CONF_PASSWORD]
|
||||
}))
|
||||
CONF_PASSWORD: conf[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -69,7 +82,7 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
if device_dict[VS_SWITCHES]:
|
||||
switches.extend(device_dict[VS_SWITCHES])
|
||||
hass.async_create_task(forward_setup(config_entry, 'switch'))
|
||||
hass.async_create_task(forward_setup(config_entry, "switch"))
|
||||
|
||||
async def async_new_device_discovery(service):
|
||||
"""Discover if new devices should be added."""
|
||||
@@ -83,18 +96,15 @@ async def async_setup_entry(hass, config_entry):
|
||||
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)
|
||||
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_setup(config_entry, 'switch'))
|
||||
hass.async_create_task(forward_setup(config_entry, "switch"))
|
||||
|
||||
hass.services.async_register(DOMAIN,
|
||||
SERVICE_UPDATE_DEVS,
|
||||
async_new_device_discovery
|
||||
)
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -104,7 +114,7 @@ async def async_unload_entry(hass, entry):
|
||||
forward_unload = hass.config_entries.async_forward_entry_unload
|
||||
remove_switches = False
|
||||
if hass.data[DOMAIN][VS_SWITCHES]:
|
||||
remove_switches = await forward_unload(entry, 'switch')
|
||||
remove_switches = await forward_unload(entry, "switch")
|
||||
|
||||
if remove_switches:
|
||||
hass.services.async_remove(DOMAIN, SERVICE_UPDATE_DEVS)
|
||||
|
||||
@@ -21,8 +21,7 @@ async def async_process_devices(hass, manager):
|
||||
for switch in manager.switches:
|
||||
if not switch.is_dimmable():
|
||||
devices[VS_SWITCHES].append(switch)
|
||||
_LOGGER.info(
|
||||
"%d VeSync standard switches found", len(manager.switches))
|
||||
_LOGGER.info("%d VeSync standard switches found", len(manager.switches))
|
||||
|
||||
return devices
|
||||
|
||||
@@ -38,8 +37,7 @@ class VeSyncDevice(ToggleEntity):
|
||||
def unique_id(self):
|
||||
"""Return the ID of this device."""
|
||||
if isinstance(self.device.sub_device_no, int):
|
||||
return ('{}{}'.format(
|
||||
self.device.cid, str(self.device.sub_device_no)))
|
||||
return "{}{}".format(self.device.cid, str(self.device.sub_device_no))
|
||||
return self.device.cid
|
||||
|
||||
@property
|
||||
|
||||
@@ -36,7 +36,7 @@ class VeSyncFlowHandler(config_entries.ConfigFlow):
|
||||
def _show_form(self, errors=None):
|
||||
"""Show form to the user."""
|
||||
return self.async_show_form(
|
||||
step_id='user',
|
||||
step_id="user",
|
||||
data_schema=vol.Schema(self.data_schema),
|
||||
errors=errors if errors else {},
|
||||
)
|
||||
@@ -48,7 +48,7 @@ class VeSyncFlowHandler(config_entries.ConfigFlow):
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle a flow start."""
|
||||
if configured_instances(self.hass):
|
||||
return self.async_abort(reason='already_setup')
|
||||
return self.async_abort(reason="already_setup")
|
||||
|
||||
if not user_input:
|
||||
return self._show_form()
|
||||
@@ -59,12 +59,9 @@ class VeSyncFlowHandler(config_entries.ConfigFlow):
|
||||
manager = VeSync(self._username, self._password)
|
||||
login = await self.hass.async_add_executor_job(manager.login)
|
||||
if not login:
|
||||
return self._show_form(errors={'base': 'invalid_login'})
|
||||
return self._show_form(errors={"base": "invalid_login"})
|
||||
|
||||
return self.async_create_entry(
|
||||
title=self._username,
|
||||
data={
|
||||
CONF_USERNAME: self._username,
|
||||
CONF_PASSWORD: self._password,
|
||||
},
|
||||
data={CONF_USERNAME: self._username, CONF_PASSWORD: self._password},
|
||||
)
|
||||
@@ -1,9 +1,9 @@
|
||||
"""Constants for VeSync Component."""
|
||||
|
||||
DOMAIN = 'vesync'
|
||||
VS_DISPATCHERS = 'vesync_dispatchers'
|
||||
VS_DISCOVERY = 'vesync_discovery_{}'
|
||||
SERVICE_UPDATE_DEVS = 'update_devices'
|
||||
DOMAIN = "vesync"
|
||||
VS_DISPATCHERS = "vesync_dispatchers"
|
||||
VS_DISCOVERY = "vesync_discovery_{}"
|
||||
SERVICE_UPDATE_DEVS = "update_devices"
|
||||
|
||||
VS_SWITCHES = 'switches'
|
||||
VS_MANAGER = 'manager'
|
||||
VS_SWITCHES = "switches"
|
||||
VS_MANAGER = "manager"
|
||||
@@ -9,30 +9,29 @@ from .common import VeSyncDevice
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEV_TYPE_TO_HA = {
|
||||
'wifi-switch-1.3': 'outlet',
|
||||
'ESW03-USA': 'outlet',
|
||||
'ESW01-EU': 'outlet',
|
||||
'ESW15-USA': 'outlet',
|
||||
'ESWL01': 'switch',
|
||||
'ESWL03': 'switch',
|
||||
'ESO15-TB': 'outlet'
|
||||
"wifi-switch-1.3": "outlet",
|
||||
"ESW03-USA": "outlet",
|
||||
"ESW01-EU": "outlet",
|
||||
"ESW15-USA": "outlet",
|
||||
"ESWL01": "switch",
|
||||
"ESWL03": "switch",
|
||||
"ESO15-TB": "outlet",
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up switches."""
|
||||
|
||||
async def async_discover(devices):
|
||||
"""Add new devices to platform."""
|
||||
_async_setup_entities(devices, async_add_entities)
|
||||
|
||||
disp = async_dispatcher_connect(
|
||||
hass, VS_DISCOVERY.format(VS_SWITCHES), async_discover)
|
||||
hass, VS_DISCOVERY.format(VS_SWITCHES), async_discover
|
||||
)
|
||||
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
|
||||
|
||||
_async_setup_entities(
|
||||
hass.data[DOMAIN][VS_SWITCHES],
|
||||
async_add_entities
|
||||
)
|
||||
_async_setup_entities(hass.data[DOMAIN][VS_SWITCHES], async_add_entities)
|
||||
return True
|
||||
|
||||
|
||||
@@ -41,19 +40,17 @@ def _async_setup_entities(devices, async_add_entities):
|
||||
"""Check if device is online and add entity."""
|
||||
dev_list = []
|
||||
for dev in devices:
|
||||
if DEV_TYPE_TO_HA.get(dev.device_type) == 'outlet':
|
||||
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
|
||||
dev_list.append(VeSyncSwitchHA(dev))
|
||||
elif DEV_TYPE_TO_HA.get(dev.device_type) == 'switch':
|
||||
elif DEV_TYPE_TO_HA.get(dev.device_type) == "switch":
|
||||
dev_list.append(VeSyncLightSwitch(dev))
|
||||
else:
|
||||
_LOGGER.warning("%s - Unkown device type - %s",
|
||||
dev.device_name, dev.device_type)
|
||||
_LOGGER.warning(
|
||||
"%s - Unkown device type - %s", dev.device_name, dev.device_type
|
||||
)
|
||||
continue
|
||||
|
||||
async_add_entities(
|
||||
dev_list,
|
||||
update_before_add=True
|
||||
)
|
||||
async_add_entities(dev_list, update_before_add=True)
|
||||
|
||||
|
||||
class VeSyncSwitchHA(VeSyncDevice, SwitchDevice):
|
||||
@@ -68,11 +65,11 @@ class VeSyncSwitchHA(VeSyncDevice, SwitchDevice):
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes of the device."""
|
||||
attr = {}
|
||||
if hasattr(self.smartplug, 'weekly_energy_total'):
|
||||
attr['voltage'] = self.smartplug.voltage
|
||||
attr['weekly_energy_total'] = self.smartplug.weekly_energy_total
|
||||
attr['monthly_energy_total'] = self.smartplug.monthly_energy_total
|
||||
attr['yearly_energy_total'] = self.smartplug.yearly_energy_total
|
||||
if hasattr(self.smartplug, "weekly_energy_total"):
|
||||
attr["voltage"] = self.smartplug.voltage
|
||||
attr["weekly_energy_total"] = self.smartplug.weekly_energy_total
|
||||
attr["monthly_energy_total"] = self.smartplug.monthly_energy_total
|
||||
attr["yearly_energy_total"] = self.smartplug.yearly_energy_total
|
||||
return attr
|
||||
|
||||
@property
|
||||
|
||||
Reference in new issue
Block a user