From 9a45eb8398045f5286f7a22553fdecd2791302bb Mon Sep 17 00:00:00 2001 From: rospogrigio Date: Fri, 11 Sep 2020 11:55:43 +0200 Subject: [PATCH 1/4] Fixed conflicts --- custom_components/localtuya/__init__.py | 3 +++ custom_components/localtuya/config_flow.py | 3 ++- custom_components/localtuya/switch.py | 23 +++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index f3c215a..a00b436 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -17,6 +17,9 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up LocalTuya integration from a config entry.""" + + print('async_setup_entry localtuya subswitch [{}] '.format(entry.data)) + for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index 6d814ef..b86b9a3 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -180,8 +180,9 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): else: self.entities.append(_convert_entity(user_input)) + print('ENTITIES: [{}] '.format(self.entities)) config = { - CONF_NAME: f"{user_input[CONF_DEVICE_ID]} (import from configuration.yaml)", + CONF_NAME: f"{user_input[CONF_FRIENDLY_NAME]} (YAML)", CONF_HOST: user_input[CONF_HOST], CONF_DEVICE_ID: user_input[CONF_DEVICE_ID], CONF_LOCAL_KEY: user_input[CONF_LOCAL_KEY], diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index 9b2ae03..36aeb54 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -103,6 +103,10 @@ DPS_FIELDS = [ async def async_setup_entry(hass, config_entry, async_add_entities): """Setup a Tuya switch based on a config entry.""" + + print('config_entry: [{}] '.format(config_entry.data)) + + switches_to_setup = [ entity for entity in config_entry.data[CONF_ENTITIES] @@ -120,9 +124,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities): pytuyadevice.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION])) pytuyadevice.set_dpsUsed({}) + print('switches_to_setup: [{}] '.format(switches_to_setup)) + for device_config in switches_to_setup: switches.append( - TuyaDevice( + LocaltuyaSwitch( TuyaCache(pytuyadevice), device_config[CONF_FRIENDLY_NAME], device_config[CONF_ID], @@ -213,6 +219,7 @@ class TuyaCache: finally: self._lock.release() + class LocaltuyaSwitch(SwitchEntity): """Representation of a Tuya switch.""" @@ -241,6 +248,20 @@ class LocaltuyaSwitch(SwitchEntity): ) ) + @property + def device_info(self): + print("ZIO KEN DEVINFO [{}]".format(self._device) ) + return { + "identifiers": { + # Serial numbers are unique identifiers within a specific domain + ("LocalTuya", self.unique_id) + }, + "name": "MyName", + "manufacturer": "Tuya generic", + "model": "SmartSwitch", + "sw_version": "3.3", + } + @property def name(self): """Get name of Tuya switch.""" From c008339f437e5cd2ef99ab2bbe0fada1fd821265 Mon Sep 17 00:00:00 2001 From: rospogrigio Date: Fri, 11 Sep 2020 14:48:49 +0200 Subject: [PATCH 2/4] First working implementation of Devices --- custom_components/localtuya/config_flow.py | 10 ++++++---- custom_components/localtuya/cover.py | 8 +++++++- custom_components/localtuya/fan.py | 8 +++++++- custom_components/localtuya/light.py | 8 +++++++- custom_components/localtuya/pytuya/__init__.py | 8 +++----- custom_components/localtuya/strings.json | 1 + custom_components/localtuya/switch.py | 7 ++++--- custom_components/localtuya/translations/en.json | 1 + 8 files changed, 36 insertions(+), 15 deletions(-) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index b86b9a3..0040d8e 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -32,6 +32,7 @@ NO_ADDITIONAL_PLATFORMS = "no_additional_platforms" USER_SCHEMA = vol.Schema( { vol.Required(CONF_NAME): str, + vol.Required(CONF_FRIENDLY_NAME): str, vol.Required(CONF_HOST): str, vol.Required(CONF_DEVICE_ID): str, vol.Required(CONF_LOCAL_KEY): str, @@ -64,8 +65,8 @@ def strip_dps_values(user_input, fields): async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows us to connect.""" - pytuyadevice = pytuya.OutletDevice( - data[CONF_DEVICE_ID], data[CONF_HOST], data[CONF_LOCAL_KEY] + pytuyadevice = pytuya.PytuyaDevice( + data[CONF_DEVICE_ID], data[CONF_HOST], data[CONF_LOCAL_KEY], data[CONF_NAME] ) pytuyadevice.set_version(float(data[CONF_PROTOCOL_VERSION])) pytuyadevice.set_dpsUsed({}) @@ -180,9 +181,10 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): else: self.entities.append(_convert_entity(user_input)) - print('ENTITIES: [{}] '.format(self.entities)) + #print('ENTITIES: [{}] '.format(self.entities)) config = { - CONF_NAME: f"{user_input[CONF_FRIENDLY_NAME]} (YAML)", + CONF_NAME: f"{user_input[CONF_FRIENDLY_NAME]} (YAML)", + CONF_FRIENDLY_NAME: f"{user_input[CONF_FRIENDLY_NAME]} (YAML)", CONF_HOST: user_input[CONF_HOST], CONF_DEVICE_ID: user_input[CONF_DEVICE_ID], CONF_LOCAL_KEY: user_input[CONF_LOCAL_KEY], diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index 7078343..d47a490 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -84,7 +84,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None): #_LOGGER.info("conf_STOP_cmd is %s", config.get(CONF_STOP_CMD)) covers = [] - pytuyadevice = pytuya.PytuyaDevice(config.get(CONF_DEVICE_ID), config.get(CONF_HOST), config.get(CONF_LOCAL_KEY)) + pytuyadevice = pytuya.PytuyaDevice( + config_entry.data[CONF_DEVICE_ID], + config_entry.data[CONF_HOST], + config_entry.data[CONF_LOCAL_KEY], + config_entry.data[CONF_FRIENDLY_NAME], + config_entry.data[CONF_NAME], + ) pytuyadevice.set_version(float(config.get(CONF_PROTOCOL_VERSION))) dps = {} dps[config.get(CONF_ID)]=None diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 445c79c..e7087ad 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -58,7 +58,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up of the Tuya switch.""" from . import pytuya fans = [] - pytuyadevice = pytuya.PytuyaDevice(config.get(CONF_DEVICE_ID), config.get(CONF_HOST), config.get(CONF_LOCAL_KEY)) + pytuyadevice = pytuya.PytuyaDevice( + config_entry.data[CONF_DEVICE_ID], + config_entry.data[CONF_HOST], + config_entry.data[CONF_LOCAL_KEY], + config_entry.data[CONF_FRIENDLY_NAME], + config_entry.data[CONF_NAME], + ) pytuyadevice.set_version(float(config.get(CONF_PROTOCOL_VERSION))) _LOGGER.debug("localtuya fan: setup_platform: %s", pytuyadevice) diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index dd07d20..ae0fdac 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -62,7 +62,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None): from . import pytuya lights = [] - pytuyadevice = pytuya.PytuyaDevice(config.get(CONF_DEVICE_ID), config.get(CONF_HOST), config.get(CONF_LOCAL_KEY)) + pytuyadevice = pytuya.PytuyaDevice( + config_entry.data[CONF_DEVICE_ID], + config_entry.data[CONF_HOST], + config_entry.data[CONF_LOCAL_KEY], + config_entry.data[CONF_FRIENDLY_NAME], + config_entry.data[CONF_NAME], + ) pytuyadevice.set_version(float(config.get(CONF_PROTOCOL_VERSION))) bulb_device = TuyaCache(pytuyadevice) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index 1fd57a3..819dfa1 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -172,13 +172,9 @@ payload_dict = { } } -#class PytuyaDevice(XenonDevice): -# def __init__(self, dev_id, address, local_key=None, dev_type=None): -# super(PytuyaDevice, self).__init__(dev_id, address, local_key, dev_type) -#class XenonDevice(object): class PytuyaDevice(object): - def __init__(self, dev_id, address, local_key=None, connection_timeout=10): + def __init__(self, dev_id, address, local_key, friendly_name, name = "", connection_timeout=10): """ Represents a Tuya device. @@ -194,6 +190,8 @@ class PytuyaDevice(object): self.id = dev_id self.address = address self.local_key = local_key.encode('latin1') + self.friendly_name = friendly_name + self.name = name self.connection_timeout = connection_timeout self.version = 3.1 self.dev_type = 'type_0a' diff --git a/custom_components/localtuya/strings.json b/custom_components/localtuya/strings.json index 898b99d..288097b 100644 --- a/custom_components/localtuya/strings.json +++ b/custom_components/localtuya/strings.json @@ -16,6 +16,7 @@ "description": "Fill in the basic details and pick device type. The name entered here will be used to identify the integration itself (as seen in the `Integrations` page). You will name each sub-device in the following steps.", "data": { "name": "Name", + "friendly_name": "Friendly name", "host": "Host", "device_id": "Device ID", "local_key": "Local key", diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index 36aeb54..74b3c52 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -120,6 +120,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities): config_entry.data[CONF_DEVICE_ID], config_entry.data[CONF_HOST], config_entry.data[CONF_LOCAL_KEY], + config_entry.data[CONF_FRIENDLY_NAME], + config_entry.data[CONF_NAME], ) pytuyadevice.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION])) pytuyadevice.set_dpsUsed({}) @@ -250,13 +252,12 @@ class LocaltuyaSwitch(SwitchEntity): @property def device_info(self): - print("ZIO KEN DEVINFO [{}]".format(self._device) ) return { "identifiers": { # Serial numbers are unique identifiers within a specific domain - ("LocalTuya", self.unique_id) + ("LocalTuya", f"local_{self._device.unique_id}") }, - "name": "MyName", + "name": self._device._device.friendly_name, "manufacturer": "Tuya generic", "model": "SmartSwitch", "sw_version": "3.3", diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 6ed5cfc..1cb21bd 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -15,6 +15,7 @@ "description": "Fill in the basic details and pick device type. The name entered here will be used to identify the integration itself (as seen in the `Integrations` page). You will add entities and give them names in the following steps.", "data": { "name": "Name", + "friendly_name": "Friendly name", "host": "Host", "device_id": "Device ID", "local_key": "Local key", From 22ccd5192686be13fe52331089954dccf9bdb3bc Mon Sep 17 00:00:00 2001 From: rospogrigio Date: Tue, 15 Sep 2020 09:24:53 +0200 Subject: [PATCH 3/4] Fixed TuyaDevice init calls --- custom_components/localtuya/__init__.py | 2 ++ custom_components/localtuya/config_flow.py | 6 +++++- custom_components/localtuya/pytuya/__init__.py | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index bebbba2..888c5e3 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -50,6 +50,8 @@ def prepare_setup_entities(config_entry, platform): config_entry.data[CONF_DEVICE_ID], config_entry.data[CONF_HOST], config_entry.data[CONF_LOCAL_KEY], + config_entry.data[CONF_FRIENDLY_NAME], + config_entry.data[CONF_NAME], ) device.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION])) device.set_dpsUsed({}) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index 0cece50..0135e80 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -74,7 +74,11 @@ def strip_dps_values(user_input, dps_strings): async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows us to connect.""" pytuyadevice = pytuya.TuyaDevice( - data[CONF_DEVICE_ID], data[CONF_HOST], data[CONF_LOCAL_KEY] + data[CONF_DEVICE_ID], + data[CONF_HOST], + data[CONF_LOCAL_KEY], + data[CONF_FRIENDLY_NAME], + data[CONF_NAME], ) pytuyadevice.set_version(float(data[CONF_PROTOCOL_VERSION])) pytuyadevice.set_dpsUsed({}) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index e27098b..b7468ae 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -173,7 +173,7 @@ payload_dict = { class TuyaDevice(object): - def __init__(self, dev_id, address, local_key=None, connection_timeout=10): + def __init__(self, dev_id, address, local_key, friendly_name, name = "", connection_timeout=10): """ Represents a Tuya device. From c99cc1f077e4dad8999c489f6f5635a652806662 Mon Sep 17 00:00:00 2001 From: rospogrigio Date: Tue, 15 Sep 2020 23:31:33 +0200 Subject: [PATCH 4/4] Removed unneeded friendly_name fields --- custom_components/localtuya/__init__.py | 2 -- custom_components/localtuya/config_flow.py | 3 --- custom_components/localtuya/cover.py | 7 ++++--- custom_components/localtuya/light.py | 18 ++++++++++++++++-- custom_components/localtuya/pytuya/__init__.py | 4 +--- custom_components/localtuya/strings.json | 1 - custom_components/localtuya/switch.py | 7 ++++--- .../localtuya/translations/en.json | 1 - 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index ad29b32..b83a244 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -50,8 +50,6 @@ def prepare_setup_entities(config_entry, platform): config_entry.data[CONF_DEVICE_ID], config_entry.data[CONF_HOST], config_entry.data[CONF_LOCAL_KEY], - config_entry.data[CONF_FRIENDLY_NAME], - config_entry.data[CONF_NAME], ) device.set_version(float(config_entry.data[CONF_PROTOCOL_VERSION])) device.set_dpsUsed({}) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index 0135e80..a58c096 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -32,7 +32,6 @@ NO_ADDITIONAL_PLATFORMS = "no_additional_platforms" USER_SCHEMA = vol.Schema( { vol.Required(CONF_NAME): str, - vol.Required(CONF_FRIENDLY_NAME): str, vol.Required(CONF_HOST): str, vol.Required(CONF_DEVICE_ID): str, vol.Required(CONF_LOCAL_KEY): str, @@ -77,8 +76,6 @@ async def validate_input(hass: core.HomeAssistant, data): data[CONF_DEVICE_ID], data[CONF_HOST], data[CONF_LOCAL_KEY], - data[CONF_FRIENDLY_NAME], - data[CONF_NAME], ) pytuyadevice.set_version(float(data[CONF_PROTOCOL_VERSION])) pytuyadevice.set_dpsUsed({}) diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index 8ece378..70e7a24 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -84,7 +84,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): dps[device_config[CONF_ID]] = None covers.append( LocaltuyaCover( - TuyaCache(device), + TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]), device_config[CONF_FRIENDLY_NAME], device_config[CONF_ID], device_config.get(CONF_OPEN_CMD), @@ -107,11 +107,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class TuyaCache: """Cache wrapper for pytuya.TuyaDevice""" - def __init__(self, device): + def __init__(self, device, friendly_name): """Initialize the cache.""" self._cached_status = "" self._cached_status_time = 0 self._device = device + self._friendly_name = friendly_name self._lock = Lock() @property @@ -202,7 +203,7 @@ class LocaltuyaCover(CoverEntity): # Serial numbers are unique identifiers within a specific domain ("LocalTuya", f"local_{self._device.unique_id}") }, - "name": self._device._device.friendly_name, + "name": self._device._friendly_name, "manufacturer": "Tuya generic", "model": "SmartCover", "sw_version": "3.3", diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 765261c..d43a168 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -63,7 +63,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): for device_config in entities_to_setup: lights.append( LocaltuyaLight( - TuyaCache(device), + TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]), device_config[CONF_FRIENDLY_NAME], device_config[CONF_ID], ) @@ -80,11 +80,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class TuyaCache: """Cache wrapper for pytuya.TuyaDevices""" - def __init__(self, device): + def __init__(self, device, friendly_name): """Initialize the cache.""" self._cached_status = "" self._cached_status_time = 0 self._device = device + self._friendly_name = friendly_name self._lock = Lock() @property @@ -147,6 +148,19 @@ class LocaltuyaLight(LightEntity): self._color_temp = 127 self._bulb_id = bulbid + @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": "SmartLight", + "sw_version": "3.3", + } + def state(self): self._device.state() diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index b7468ae..58fc637 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -173,7 +173,7 @@ payload_dict = { class TuyaDevice(object): - def __init__(self, dev_id, address, local_key, friendly_name, name = "", connection_timeout=10): + def __init__(self, dev_id, address, local_key, connection_timeout=10): """ Represents a Tuya device. @@ -189,8 +189,6 @@ class TuyaDevice(object): self.id = dev_id self.address = address self.local_key = local_key.encode('latin1') - self.friendly_name = friendly_name - self.name = name self.connection_timeout = connection_timeout self.version = 3.1 self.dev_type = 'type_0a' diff --git a/custom_components/localtuya/strings.json b/custom_components/localtuya/strings.json index 288097b..898b99d 100644 --- a/custom_components/localtuya/strings.json +++ b/custom_components/localtuya/strings.json @@ -16,7 +16,6 @@ "description": "Fill in the basic details and pick device type. The name entered here will be used to identify the integration itself (as seen in the `Integrations` page). You will name each sub-device in the following steps.", "data": { "name": "Name", - "friendly_name": "Friendly name", "host": "Host", "device_id": "Device ID", "local_key": "Local key", diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index 696f7ea..41768c2 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -101,7 +101,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): for device_config in entities_to_setup: switches.append( LocaltuyaSwitch( - TuyaCache(device), + TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]), device_config[CONF_FRIENDLY_NAME], device_config[CONF_ID], device_config.get(CONF_CURRENT), @@ -121,11 +121,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class TuyaCache: """Cache wrapper for pytuya.TuyaDevice""" - def __init__(self, device): + def __init__(self, device, friendly_name): """Initialize the cache.""" self._cached_status = "" self._cached_status_time = 0 self._device = device + self._friendly_name = friendly_name self._lock = Lock() @property @@ -220,7 +221,7 @@ class LocaltuyaSwitch(SwitchEntity): # Serial numbers are unique identifiers within a specific domain ("LocalTuya", f"local_{self._device.unique_id}") }, - "name": self._device._device.friendly_name, + "name": self._device._friendly_name, "manufacturer": "Tuya generic", "model": "SmartSwitch", "sw_version": "3.3", diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 002eaec..c0be82a 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -15,7 +15,6 @@ "description": "Fill in the basic details and pick device type. The name entered here will be used to identify the integration itself (as seen in the `Integrations` page). You will add entities and give them names in the following steps.", "data": { "name": "Name", - "friendly_name": "Friendly name", "host": "Host", "device_id": "Device ID", "local_key": "Local key",