diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index 17221d3..bda3a97 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -29,7 +29,7 @@ from homeassistant.components.cover import ( ) """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) import homeassistant.helpers.config_validation as cv 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))) _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: @@ -101,6 +101,11 @@ class TuyaCoverCache: self._device = device self._lock = Lock() + @property + def unique_id(self): + """Return unique device identifier.""" + return self._device.id + def __get_status(self): for i in range(5): try: @@ -146,7 +151,7 @@ class TuyaDevice(CoverEntity): def __init__(self, device, name, friendly_name, icon, switchid): self._device = device - self.entity_id = ENTITY_ID_FORMAT.format(name) + self._available = False self._name = friendly_name self._friendly_name = friendly_name self._icon = icon @@ -161,6 +166,16 @@ class TuyaDevice(CoverEntity): """Get name of Tuya switch.""" 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 def supported_features(self): """Flag supported features.""" @@ -252,6 +267,11 @@ class TuyaDevice(CoverEntity): def update(self): """Get state of Tuya switch.""" - self._status = self._device.status() - self._state = self._status['dps'][self._switch_id] - #print('update() : state [{}]'.format(self._state)) + try: + self._status = self._device.status() + self._state = self._status['dps'][self._switch_id] + #print('update() : state [{}]'.format(self._state)) + except Exception: + self._available = False + else: + self._available = True diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index a9dd6b6..75d904f 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -86,6 +86,11 @@ class TuyaCache: self._device = device self._lock = Lock() + @property + def unique_id(self): + """Return unique device identifier.""" + return self._device.id + def __get_status(self, switchid): for _ in range(UPDATE_RETRY_LIMIT): try: @@ -193,6 +198,7 @@ class TuyaDevice(Light): def __init__(self, device, name, icon, bulbid): """Initialize the Tuya switch.""" self._device = device + self._available = False self._name = name self._state = False self._brightness = 127 @@ -205,6 +211,16 @@ class TuyaDevice(Light): """Get name of Tuya switch.""" 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 def is_on(self): """Check if Tuya switch is on.""" @@ -217,6 +233,14 @@ class TuyaDevice(Light): def update(self): """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) self._state = status try: diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index 0a597a9..b5b4584 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -27,7 +27,7 @@ switch: import logging 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) import homeassistant.helpers.config_validation as cv 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))) _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: """Cache wrapper for pytuya.OutletDevice""" @@ -154,6 +154,11 @@ class TuyaCache: self._device = device self._lock = Lock() + @property + def unique_id(self): + """Return unique device identifier.""" + return self._device.id + def __get_status(self): for i in range(5): try: @@ -200,8 +205,8 @@ class TuyaDevice(SwitchEntity): def __init__(self, device, name, friendly_name, icon, switchid, attr_current, attr_consumption, attr_voltage): """Initialize the Tuya switch.""" self._device = device - self.entity_id = ENTITY_ID_FORMAT.format(name) self._name = friendly_name + self._available = False self._icon = icon self._switch_id = switchid self._attr_current = attr_current @@ -216,6 +221,15 @@ class TuyaDevice(SwitchEntity): """Get name of Tuya switch.""" 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 def is_on(self): @@ -252,5 +266,10 @@ class TuyaDevice(SwitchEntity): def update(self): """Get state of Tuya switch.""" - self._status = self._device.status() - self._state = self._status['dps'][self._switch_id] + try: + self._status = self._device.status() + self._state = self._status['dps'][self._switch_id] + except Exception: + self._available = False + else: + self._available = True