LocaltuyaCover now inherits from LocalTuyaEntity
This commit is contained in:
@@ -112,6 +112,19 @@ class LocalTuyaEntity(Entity):
|
|||||||
self._dps_id = dps_id
|
self._dps_id = dps_id
|
||||||
self._status = None
|
self._status = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self):
|
||||||
|
return {
|
||||||
|
"identifiers": {
|
||||||
|
# Serial numbers are unique identifiers within a specific domain
|
||||||
|
("LocalTuya", f"local_{self._device.unique_id}")
|
||||||
|
},
|
||||||
|
"name": self._device._friendly_name,
|
||||||
|
"manufacturer": "Unknown",
|
||||||
|
"model": "Tuya generic",
|
||||||
|
"sw_version": "3.3",
|
||||||
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Get name of Tuya entity."""
|
"""Get name of Tuya entity."""
|
||||||
|
@@ -39,7 +39,17 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import BASE_PLATFORM_SCHEMA, prepare_setup_entities, import_from_yaml
|
from . import (
|
||||||
|
BASE_PLATFORM_SCHEMA,
|
||||||
|
LocalTuyaEntity,
|
||||||
|
prepare_setup_entities,
|
||||||
|
import_from_yaml,
|
||||||
|
)
|
||||||
|
from .const import (
|
||||||
|
CONF_OPEN_CMD,
|
||||||
|
CONF_CLOSE_CMD,
|
||||||
|
CONF_STOP_CMD,
|
||||||
|
)
|
||||||
from .const import CONF_OPEN_CMD, CONF_CLOSE_CMD, CONF_STOP_CMD
|
from .const import CONF_OPEN_CMD, CONF_CLOSE_CMD, CONF_STOP_CMD
|
||||||
from .pytuya import TuyaDevice
|
from .pytuya import TuyaDevice
|
||||||
|
|
||||||
@@ -85,15 +95,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
covers.append(
|
covers.append(
|
||||||
LocaltuyaCover(
|
LocaltuyaCover(
|
||||||
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
||||||
device_config[CONF_FRIENDLY_NAME],
|
config_entry,
|
||||||
device_config[CONF_ID],
|
device_config[CONF_ID],
|
||||||
device_config.get(CONF_OPEN_CMD),
|
|
||||||
device_config.get(CONF_CLOSE_CMD),
|
|
||||||
device_config.get(CONF_STOP_CMD),
|
|
||||||
)
|
)
|
||||||
# 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.debug("Cover %s uses open_cmd=%s close_cmd=%s stop_cmd=%s", config.get(CONF_FRIENDLY_NAME), config.get(CONF_OPEN_CMD), config.get(CONF_CLOSE_CMD), config.get(CONF_STOP_CMD) )
|
|
||||||
)
|
)
|
||||||
|
|
||||||
device.set_dpsUsed(dps)
|
device.set_dpsUsed(dps)
|
||||||
@@ -175,65 +179,26 @@ class TuyaCache:
|
|||||||
finally:
|
finally:
|
||||||
self._lock.release()
|
self._lock.release()
|
||||||
|
|
||||||
class LocaltuyaCover(CoverEntity):
|
class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
|
||||||
"""Tuya cover devices."""
|
"""Tuya cover devices."""
|
||||||
|
|
||||||
def __init__(self, device, friendly_name, switchid, open_cmd, close_cmd, stop_cmd):
|
def __init__(
|
||||||
self._device = device
|
self,
|
||||||
self._available = False
|
device,
|
||||||
self._name = friendly_name
|
config_entry,
|
||||||
self._switch_id = switchid
|
switchid,
|
||||||
self._status = None
|
**kwargs,
|
||||||
|
):
|
||||||
|
#_LOGGER.info("running def __init__ of LocaltuyaCover(CoverEntity) with self=%s device=%s name=%s friendly_name=%s icon=%s switchid=%s open_cmd=%s close_cmd=%s stop_cmd=%s", self, device, name, friendly_name, icon, switchid, open_cmd, close_cmd, stop_cmd)
|
||||||
|
super().__init__(device, config_entry, switchid, **kwargs)
|
||||||
self._state = None
|
self._state = None
|
||||||
self._position = 50
|
self._position = 50
|
||||||
self._open_cmd = open_cmd
|
|
||||||
self._close_cmd = close_cmd
|
|
||||||
self._stop_cmd = stop_cmd
|
|
||||||
#_LOGGER.info("running def __init__ of LocaltuyaCover(CoverEntity) with self=%s device=%s name=%s friendly_name=%s icon=%s switchid=%s open_cmd=%s close_cmd=%s stop_cmd=%s", self, device, name, friendly_name, icon, switchid, open_cmd, close_cmd, stop_cmd)
|
|
||||||
print(
|
print(
|
||||||
"Initialized tuya cover [{}] with switch status [{}] and state [{}]".format(
|
"Initialized tuya cover [{}] with switch status [{}] and state [{}]".format(
|
||||||
self._name, self._status, self._state
|
self.name, self._status, self._state
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self):
|
|
||||||
return {
|
|
||||||
"identifiers": {
|
|
||||||
# Serial numbers are unique identifiers within a specific domain
|
|
||||||
("LocalTuya", f"local_{self._device.unique_id}")
|
|
||||||
},
|
|
||||||
"name": self._device._friendly_name,
|
|
||||||
"manufacturer": "Tuya generic",
|
|
||||||
"model": "SmartCover",
|
|
||||||
"sw_version": "3.3",
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return unique device identifier."""
|
|
||||||
return f"local_{self._device.unique_id}_{self._switch_id}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Get name of Tuya switch."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def open_cmd(self):
|
|
||||||
"""Get name of open command."""
|
|
||||||
return self._open_cmd
|
|
||||||
|
|
||||||
@property
|
|
||||||
def close_cmd(self):
|
|
||||||
"""Get name of close command."""
|
|
||||||
return self._close_cmd
|
|
||||||
|
|
||||||
@property
|
|
||||||
def stop_cmd(self):
|
|
||||||
"""Get name of stop command."""
|
|
||||||
return self._stop_cmd
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return if device is available or not."""
|
"""Return if device is available or not."""
|
||||||
@@ -307,39 +272,22 @@ class LocaltuyaCover(CoverEntity):
|
|||||||
self.stop_cover()
|
self.stop_cover()
|
||||||
self._position = 50 # newpos
|
self._position = 50 # newpos
|
||||||
|
|
||||||
# self._state = 'on'
|
|
||||||
# self._device._device.open_cover()
|
|
||||||
|
|
||||||
def open_cover(self, **kwargs):
|
def open_cover(self, **kwargs):
|
||||||
"""Open the cover."""
|
"""Open the cover."""
|
||||||
#_LOGGER.info("running open_cover from cover")
|
_LOGGER.info("Launching command %s to cover ", self._config[CONF_OPEN_CMD])
|
||||||
self._device.set_dps(self._open_cmd, self._switch_id)
|
self._device.set_dps(self._config[CONF_OPEN_CMD], self._dps_id)
|
||||||
# self._state = 'on'
|
|
||||||
# self._device._device.open_cover()
|
|
||||||
|
|
||||||
def close_cover(self, **kwargs):
|
def close_cover(self, **kwargs):
|
||||||
# _LOGGER.info("running close_cover from cover")
|
|
||||||
"""Close cover."""
|
"""Close cover."""
|
||||||
#_LOGGER.info('about to set_dps from cover of off, %s', self._switch_id)
|
_LOGGER.info("Launching command %s to cover ", self._config[CONF_CLOSE_CMD])
|
||||||
self._device.set_dps(self._close_cmd, self._switch_id)
|
self._device.set_dps(self._config[CONF_CLOSE_CMD], self._dps_id)
|
||||||
# self._state = 'off'
|
|
||||||
# self._device._device.close_cover()
|
|
||||||
|
|
||||||
def stop_cover(self, **kwargs):
|
def stop_cover(self, **kwargs):
|
||||||
# _LOGGER.info("running stop_cover from cover")
|
|
||||||
"""Stop the cover."""
|
"""Stop the cover."""
|
||||||
self._device.set_dps(self._stop_cmd, self._switch_id)
|
_LOGGER.info("Launching command %s to cover ", self._config[CONF_STOP_CMD])
|
||||||
# self._state = 'stop'
|
self._device.set_dps(self._config[CONF_STOP_CMD], self._dps_id)
|
||||||
# self._device._device.stop_cover()
|
|
||||||
|
|
||||||
def update(self):
|
def status_updated(self):
|
||||||
"""Get state of Tuya switch."""
|
"""Device status was updated. """
|
||||||
# _LOGGER.info("running update(self) from cover")
|
self._state = self.dps(self._dps_id)
|
||||||
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
|
|
||||||
|
@@ -170,6 +170,7 @@ class TuyaCache:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# raise ConnectionError("Failed to set status.")
|
# raise ConnectionError("Failed to set status.")
|
||||||
|
self.update()
|
||||||
|
|
||||||
def status(self):
|
def status(self):
|
||||||
"""Get state of Tuya switch and cache the results."""
|
"""Get state of Tuya switch and cache the results."""
|
||||||
@@ -204,19 +205,6 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self):
|
|
||||||
return {
|
|
||||||
"identifiers": {
|
|
||||||
# Serial numbers are unique identifiers within a specific domain
|
|
||||||
("LocalTuya", f"local_{self._device.unique_id}")
|
|
||||||
},
|
|
||||||
"name": self._device._friendly_name,
|
|
||||||
"manufacturer": "Tuya generic",
|
|
||||||
"model": "SmartSwitch",
|
|
||||||
"sw_version": "3.3",
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Check if Tuya switch is on."""
|
"""Check if Tuya switch is on."""
|
||||||
|
Reference in New Issue
Block a user