Add Support for VeSync Devices - Outlets and Switches (#24953)
* Change dependency to pyvesync-v2 for vesync switch * async vesync component * FInish data_entry_flow * Update config flow * strings.json * Minor fix * Syntax fix * Minor Fixs * UI Fix * Minor Correct * Debug lines * fix device dictionaries * Light switch fix * Cleanup * pylint fixes * Hassfest and setup scripts * Flake8 fixes * Add vesync light platform * Fix typo * Update Devices Service * Fix update devices service * Add initial test * Add Config Flow Tests * Remove Extra Platforms * Fix requirements * Update pypi package * Add login to config_flow Avoid setting up component if login credentials are invalid * Fix variable import * Update config_flow.py * Update config_flow.py * Put VS object into hass.data instead of config entry * Update __init__.py * Handle Login Error * Fix invalid login error * Fix typo * Remove line * PEP fixes * Fix change requests * Fix typo * Update __init__.py * Update switch.py * Flake8 fix * Update test requirements * Fix permission * Address change requests * Address change requests * Fix device discovery indent, add MockConfigEntry * Fix vesynclightswitch classs * Remove active time attribute * Remove time_zone, grammar check
This commit is contained in:
1 parent
738d00fb05
commit
a8ec826ef7
17 files changed
+444
-76
No files matched your search
@@ -1,102 +1,100 @@
|
||||
"""Support for Etekcity VeSync switches."""
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from .const import VS_DISCOVERY, VS_DISPATCHERS, VS_SWITCHES, DOMAIN
|
||||
from .common import VeSyncDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_USERNAME): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
})
|
||||
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'
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the VeSync switch platform."""
|
||||
from pyvesync_v2.vesync import VeSync
|
||||
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)
|
||||
|
||||
switches = []
|
||||
disp = async_dispatcher_connect(
|
||||
hass, VS_DISCOVERY.format(VS_SWITCHES), async_discover)
|
||||
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
|
||||
|
||||
manager = VeSync(config.get(CONF_USERNAME), config.get(CONF_PASSWORD))
|
||||
_async_setup_entities(
|
||||
hass.data[DOMAIN][VS_SWITCHES],
|
||||
async_add_entities
|
||||
)
|
||||
return True
|
||||
|
||||
if not manager.login():
|
||||
_LOGGER.error("Unable to login to VeSync")
|
||||
return
|
||||
|
||||
manager.update()
|
||||
|
||||
if manager.devices is not None and manager.devices:
|
||||
if len(manager.devices) == 1:
|
||||
count_string = 'switch'
|
||||
@callback
|
||||
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':
|
||||
dev_list.append(VeSyncSwitchHA(dev))
|
||||
elif DEV_TYPE_TO_HA.get(dev.device_type) == 'switch':
|
||||
dev_list.append(VeSyncLightSwitch(dev))
|
||||
else:
|
||||
count_string = 'switches'
|
||||
_LOGGER.warning("%s - Unkown device type - %s",
|
||||
dev.device_name, dev.device_type)
|
||||
continue
|
||||
|
||||
_LOGGER.info("Discovered %d VeSync %s",
|
||||
len(manager.devices), count_string)
|
||||
|
||||
for switch in manager.devices:
|
||||
switches.append(VeSyncSwitchHA(switch))
|
||||
_LOGGER.info("Added a VeSync switch named '%s'",
|
||||
switch.device_name)
|
||||
else:
|
||||
_LOGGER.info("No VeSync devices found")
|
||||
|
||||
add_entities(switches)
|
||||
async_add_entities(
|
||||
dev_list,
|
||||
update_before_add=True
|
||||
)
|
||||
|
||||
|
||||
class VeSyncSwitchHA(SwitchDevice):
|
||||
class VeSyncSwitchHA(VeSyncDevice, SwitchDevice):
|
||||
"""Representation of a VeSync switch."""
|
||||
|
||||
def __init__(self, plug):
|
||||
"""Initialize the VeSync switch device."""
|
||||
super().__init__(plug)
|
||||
self.smartplug = plug
|
||||
self._current_power_w = None
|
||||
self._today_energy_kwh = None
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the ID of this switch."""
|
||||
return self.smartplug.cid
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the switch."""
|
||||
return self.smartplug.device_name
|
||||
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
|
||||
return attr
|
||||
|
||||
@property
|
||||
def current_power_w(self):
|
||||
"""Return the current power usage in W."""
|
||||
return self._current_power_w
|
||||
return self.smartplug.power
|
||||
|
||||
@property
|
||||
def today_energy_kwh(self):
|
||||
"""Return the today total energy usage in kWh."""
|
||||
return self._today_energy_kwh
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return True if switch is available."""
|
||||
return self.smartplug.connection_status == "online"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return True if switch is on."""
|
||||
return self.smartplug.device_status == "on"
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the switch on."""
|
||||
self.smartplug.turn_on()
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the switch off."""
|
||||
self.smartplug.turn_off()
|
||||
return self.smartplug.energy_today
|
||||
|
||||
def update(self):
|
||||
"""Handle data changes for node values."""
|
||||
"""Update outlet details and energy usage."""
|
||||
self.smartplug.update()
|
||||
if self.smartplug.devtype == 'outlet':
|
||||
self._current_power_w = self.smartplug.get_power()
|
||||
self._today_energy_kwh = self.smartplug.get_kwh_today()
|
||||
self.smartplug.update_energy()
|
||||
|
||||
|
||||
class VeSyncLightSwitch(VeSyncDevice, SwitchDevice):
|
||||
"""Handle representation of VeSync Light Switch."""
|
||||
|
||||
def __init__(self, switch):
|
||||
"""Initialize Light Switch device class."""
|
||||
super().__init__(switch)
|
||||
self.switch = switch
|
||||
Reference in new issue
Block a user