Add Tuya component and switch support (#15399)

* support for tuya platform

* support tuya platform

* lint fix

* change dependency

* add tuya platform support

* remove tuya platform except switch. fix code as required

* fix the code as review required

* fix as required

* fix a mistake
pull/15425/merge
huangyupeng 2018-07-12 16:19:35 +08:00 committed by Martin Hjelmare
parent 6197fe0121
commit 43b31e88ba
4 changed files with 213 additions and 0 deletions

View File

@ -343,6 +343,9 @@ omit =
homeassistant/components/zoneminder.py
homeassistant/components/*/zoneminder.py
homeassistant/components/tuya.py
homeassistant/components/*/tuya.py
homeassistant/components/alarm_control_panel/alarmdotcom.py
homeassistant/components/alarm_control_panel/canary.py
homeassistant/components/alarm_control_panel/concord232.py

View File

@ -0,0 +1,47 @@
"""
Support for Tuya switch.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.tuya/
"""
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
from homeassistant.components.tuya import DATA_TUYA, TuyaDevice
DEPENDENCIES = ['tuya']
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up Tuya Switch device."""
if discovery_info is None:
return
tuya = hass.data[DATA_TUYA]
dev_ids = discovery_info.get('dev_ids')
devices = []
for dev_id in dev_ids:
device = tuya.get_device_by_id(dev_id)
if device is None:
continue
devices.append(TuyaSwitch(device))
add_devices(devices)
class TuyaSwitch(TuyaDevice, SwitchDevice):
"""Tuya Switch Device."""
def __init__(self, tuya):
"""Init Tuya switch device."""
super().__init__(tuya)
self.entity_id = ENTITY_ID_FORMAT.format(tuya.object_id())
@property
def is_on(self):
"""Return true if switch is on."""
return self.tuya.state()
def turn_on(self, **kwargs):
"""Turn the switch on."""
self.tuya.turn_on()
def turn_off(self, **kwargs):
"""Turn the device off."""
self.tuya.turn_off()

View File

@ -0,0 +1,160 @@
"""
Support for Tuya Smart devices.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/tuya/
"""
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD)
from homeassistant.helpers import discovery
from homeassistant.helpers.dispatcher import (
dispatcher_send, async_dispatcher_connect)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import track_time_interval
REQUIREMENTS = ['tuyapy==0.1.1']
_LOGGER = logging.getLogger(__name__)
CONF_COUNTRYCODE = 'country_code'
DOMAIN = 'tuya'
DATA_TUYA = 'data_tuya'
SIGNAL_DELETE_ENTITY = 'tuya_delete'
SIGNAL_UPDATE_ENTITY = 'tuya_update'
SERVICE_FORCE_UPDATE = 'force_update'
SERVICE_PULL_DEVICES = 'pull_devices'
TUYA_TYPE_TO_HA = {
'switch': 'switch'
}
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_COUNTRYCODE): cv.string
})
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Set up Tuya Component."""
from tuyapy import TuyaApi
tuya = TuyaApi()
username = config[DOMAIN][CONF_USERNAME]
password = config[DOMAIN][CONF_PASSWORD]
country_code = config[DOMAIN][CONF_COUNTRYCODE]
hass.data[DATA_TUYA] = tuya
tuya.init(username, password, country_code)
hass.data[DOMAIN] = {
'entities': {}
}
def load_devices(device_list):
"""Load new devices by device_list."""
device_type_list = {}
for device in device_list:
dev_type = device.device_type()
if (dev_type in TUYA_TYPE_TO_HA and
device.object_id() not in hass.data[DOMAIN]['entities']):
ha_type = TUYA_TYPE_TO_HA[dev_type]
if ha_type not in device_type_list:
device_type_list[ha_type] = []
device_type_list[ha_type].append(device.object_id())
hass.data[DOMAIN]['entities'][device.object_id()] = None
for ha_type, dev_ids in device_type_list.items():
discovery.load_platform(
hass, ha_type, DOMAIN, {'dev_ids': dev_ids}, config)
device_list = tuya.get_all_devices()
load_devices(device_list)
def poll_devices_update(event_time):
"""Check if accesstoken is expired and pull device list from server."""
_LOGGER.debug("Pull devices from Tuya.")
tuya.poll_devices_update()
# Add new discover device.
device_list = tuya.get_all_devices()
load_devices(device_list)
# Delete not exist device.
newlist_ids = []
for device in device_list:
newlist_ids.append(device.object_id())
for dev_id in list(hass.data[DOMAIN]['entities']):
if dev_id not in newlist_ids:
dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
hass.data[DOMAIN]['entities'].pop(dev_id)
track_time_interval(hass, poll_devices_update, timedelta(minutes=5))
hass.services.register(DOMAIN, SERVICE_PULL_DEVICES, poll_devices_update)
def force_update(call):
"""Force all devices to pull data."""
dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)
hass.services.register(DOMAIN, SERVICE_FORCE_UPDATE, force_update)
return True
class TuyaDevice(Entity):
"""Tuya base device."""
def __init__(self, tuya):
"""Init Tuya devices."""
self.tuya = tuya
async def async_added_to_hass(self):
"""Call when entity is added to hass."""
dev_id = self.tuya.object_id()
self.hass.data[DOMAIN]['entities'][dev_id] = self.entity_id
async_dispatcher_connect(
self.hass, SIGNAL_DELETE_ENTITY, self._delete_callback)
async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback)
@property
def object_id(self):
"""Return Tuya device id."""
return self.tuya.object_id()
@property
def name(self):
"""Return Tuya device name."""
return self.tuya.name()
@property
def icon(self):
"""Return the entity picture to use in the frontend, if any."""
return self.tuya.iconurl()
@property
def available(self):
"""Return if the device is available."""
return self.tuya.available()
def update(self):
"""Refresh Tuya device data."""
self.tuya.update()
@callback
def _delete_callback(self, dev_id):
"""Remove this entity."""
if dev_id == self.object_id:
self.hass.async_add_job(self.async_remove())
@callback
def _update_callback(self):
"""Call update method."""
self.async_schedule_update_ha_state(True)

View File

@ -1344,6 +1344,9 @@ total_connect_client==0.18
# homeassistant.components.switch.transmission
transmissionrpc==0.11
# homeassistant.components.tuya
tuyapy==0.1.1
# homeassistant.components.twilio
twilio==5.7.0