Add support for unique_id and available
This commit is contained in:
@@ -29,7 +29,7 @@ from homeassistant.components.cover import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
"""from . import DATA_TUYA, TuyaDevice"""
|
"""from . import DATA_TUYA, TuyaDevice"""
|
||||||
from homeassistant.components.cover import ENTITY_ID_FORMAT, CoverEntity, PLATFORM_SCHEMA
|
from homeassistant.components.cover import CoverEntity, PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (CONF_HOST, CONF_ID, CONF_FRIENDLY_NAME, CONF_ICON, CONF_NAME)
|
from homeassistant.const import (CONF_HOST, CONF_ID, CONF_FRIENDLY_NAME, CONF_ICON, CONF_NAME)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from time import time, sleep
|
from time import time, sleep
|
||||||
@@ -88,7 +88,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
print('Setup localtuya cover [{}] with device ID [{}] '.format(config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID)))
|
print('Setup localtuya cover [{}] with device ID [{}] '.format(config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID)))
|
||||||
_LOGGER.info("Setup localtuya cover %s with device ID %s ", config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID) )
|
_LOGGER.info("Setup localtuya cover %s with device ID %s ", config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID) )
|
||||||
|
|
||||||
add_entities(covers)
|
add_entities(covers, True)
|
||||||
|
|
||||||
|
|
||||||
class TuyaCoverCache:
|
class TuyaCoverCache:
|
||||||
@@ -101,6 +101,11 @@ class TuyaCoverCache:
|
|||||||
self._device = device
|
self._device = device
|
||||||
self._lock = Lock()
|
self._lock = Lock()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return unique device identifier."""
|
||||||
|
return self._device.id
|
||||||
|
|
||||||
def __get_status(self):
|
def __get_status(self):
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
try:
|
try:
|
||||||
@@ -146,7 +151,7 @@ class TuyaDevice(CoverEntity):
|
|||||||
|
|
||||||
def __init__(self, device, name, friendly_name, icon, switchid):
|
def __init__(self, device, name, friendly_name, icon, switchid):
|
||||||
self._device = device
|
self._device = device
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(name)
|
self._available = False
|
||||||
self._name = friendly_name
|
self._name = friendly_name
|
||||||
self._friendly_name = friendly_name
|
self._friendly_name = friendly_name
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
@@ -161,6 +166,16 @@ class TuyaDevice(CoverEntity):
|
|||||||
"""Get name of Tuya switch."""
|
"""Get name of Tuya switch."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return unique device identifier."""
|
||||||
|
return self._device.unique_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return if device is available or not."""
|
||||||
|
return self._available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
@@ -252,6 +267,11 @@ class TuyaDevice(CoverEntity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get state of Tuya switch."""
|
"""Get state of Tuya switch."""
|
||||||
self._status = self._device.status()
|
try:
|
||||||
self._state = self._status['dps'][self._switch_id]
|
self._status = self._device.status()
|
||||||
#print('update() : state [{}]'.format(self._state))
|
self._state = self._status['dps'][self._switch_id]
|
||||||
|
#print('update() : state [{}]'.format(self._state))
|
||||||
|
except Exception:
|
||||||
|
self._available = False
|
||||||
|
else:
|
||||||
|
self._available = True
|
||||||
|
@@ -86,6 +86,11 @@ class TuyaCache:
|
|||||||
self._device = device
|
self._device = device
|
||||||
self._lock = Lock()
|
self._lock = Lock()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return unique device identifier."""
|
||||||
|
return self._device.id
|
||||||
|
|
||||||
def __get_status(self, switchid):
|
def __get_status(self, switchid):
|
||||||
for _ in range(UPDATE_RETRY_LIMIT):
|
for _ in range(UPDATE_RETRY_LIMIT):
|
||||||
try:
|
try:
|
||||||
@@ -193,6 +198,7 @@ class TuyaDevice(Light):
|
|||||||
def __init__(self, device, name, icon, bulbid):
|
def __init__(self, device, name, icon, bulbid):
|
||||||
"""Initialize the Tuya switch."""
|
"""Initialize the Tuya switch."""
|
||||||
self._device = device
|
self._device = device
|
||||||
|
self._available = False
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = False
|
self._state = False
|
||||||
self._brightness = 127
|
self._brightness = 127
|
||||||
@@ -205,6 +211,16 @@ class TuyaDevice(Light):
|
|||||||
"""Get name of Tuya switch."""
|
"""Get name of Tuya switch."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return unique device identifier."""
|
||||||
|
return self._device.unique_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return if device is available or not."""
|
||||||
|
return self._available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Check if Tuya switch is on."""
|
"""Check if Tuya switch is on."""
|
||||||
@@ -217,6 +233,14 @@ class TuyaDevice(Light):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get state of Tuya switch."""
|
"""Get state of Tuya switch."""
|
||||||
|
try:
|
||||||
|
self._update_state()
|
||||||
|
except:
|
||||||
|
self._available = False
|
||||||
|
else:
|
||||||
|
self._available = True
|
||||||
|
|
||||||
|
def _update_state(self):
|
||||||
status = self._device.status(self._bulb_id)
|
status = self._device.status(self._bulb_id)
|
||||||
self._state = status
|
self._state = status
|
||||||
try:
|
try:
|
||||||
|
@@ -27,7 +27,7 @@ switch:
|
|||||||
import logging
|
import logging
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity, PLATFORM_SCHEMA
|
from homeassistant.components.switch import SwitchEntity, PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (CONF_HOST, CONF_ID, CONF_SWITCHES, CONF_FRIENDLY_NAME, CONF_ICON, CONF_NAME)
|
from homeassistant.const import (CONF_HOST, CONF_ID, CONF_SWITCHES, CONF_FRIENDLY_NAME, CONF_ICON, CONF_NAME)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from time import time, sleep
|
from time import time, sleep
|
||||||
@@ -142,7 +142,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
print('Setup localtuya switch [{}] with device ID [{}] '.format(config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID)))
|
print('Setup localtuya switch [{}] with device ID [{}] '.format(config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID)))
|
||||||
_LOGGER.info("Setup localtuya switch %s with device ID %s ", config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID) )
|
_LOGGER.info("Setup localtuya switch %s with device ID %s ", config.get(CONF_FRIENDLY_NAME), config.get(CONF_ID) )
|
||||||
|
|
||||||
add_devices(switches)
|
add_devices(switches, True)
|
||||||
|
|
||||||
class TuyaCache:
|
class TuyaCache:
|
||||||
"""Cache wrapper for pytuya.OutletDevice"""
|
"""Cache wrapper for pytuya.OutletDevice"""
|
||||||
@@ -154,6 +154,11 @@ class TuyaCache:
|
|||||||
self._device = device
|
self._device = device
|
||||||
self._lock = Lock()
|
self._lock = Lock()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return unique device identifier."""
|
||||||
|
return self._device.id
|
||||||
|
|
||||||
def __get_status(self):
|
def __get_status(self):
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
try:
|
try:
|
||||||
@@ -200,8 +205,8 @@ class TuyaDevice(SwitchEntity):
|
|||||||
def __init__(self, device, name, friendly_name, icon, switchid, attr_current, attr_consumption, attr_voltage):
|
def __init__(self, device, name, friendly_name, icon, switchid, attr_current, attr_consumption, attr_voltage):
|
||||||
"""Initialize the Tuya switch."""
|
"""Initialize the Tuya switch."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(name)
|
|
||||||
self._name = friendly_name
|
self._name = friendly_name
|
||||||
|
self._available = False
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._switch_id = switchid
|
self._switch_id = switchid
|
||||||
self._attr_current = attr_current
|
self._attr_current = attr_current
|
||||||
@@ -216,6 +221,15 @@ class TuyaDevice(SwitchEntity):
|
|||||||
"""Get name of Tuya switch."""
|
"""Get name of Tuya switch."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return unique device identifier."""
|
||||||
|
return self._device.unique_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return if device is available or not."""
|
||||||
|
return self._available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
@@ -252,5 +266,10 @@ class TuyaDevice(SwitchEntity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get state of Tuya switch."""
|
"""Get state of Tuya switch."""
|
||||||
self._status = self._device.status()
|
try:
|
||||||
self._state = self._status['dps'][self._switch_id]
|
self._status = self._device.status()
|
||||||
|
self._state = self._status['dps'][self._switch_id]
|
||||||
|
except Exception:
|
||||||
|
self._available = False
|
||||||
|
else:
|
||||||
|
self._available = True
|
||||||
|
Reference in New Issue
Block a user