diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index b455621..f607c36 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -71,7 +71,7 @@ from homeassistant.helpers.reload import async_integration_yaml_config from .common import TuyaDevice from .config_flow import config_schema -from .const import DATA_DISCOVERY, DOMAIN, TUYA_DEVICE +from .const import CONF_PRODUCT_KEY, DATA_DISCOVERY, DOMAIN, TUYA_DEVICE from .discovery import TuyaDiscovery _LOGGER = logging.getLogger(__name__) @@ -129,6 +129,7 @@ async def async_setup(hass: HomeAssistant, config: dict): """Update address of device if it has changed.""" device_ip = device["ip"] device_id = device["gwId"] + product_key = device["productKey"] # If device is not in cache, check if a config entry exists if device_id not in device_cache: @@ -138,16 +139,27 @@ async def async_setup(hass: HomeAssistant, config: dict): # potential update below device_cache[device_id] = entry.data[CONF_HOST] - # If device is in cache and address changed... - if device_id in device_cache and device_cache[device_id] != device_ip: - _LOGGER.debug("Device %s changed IP to %s", device_id, device_ip) + if device_id not in device_cache: + return - entry = _entry_by_device_id(device_id) - if entry: - hass.config_entries.async_update_entry( - entry, data={**entry.data, CONF_HOST: device_ip} - ) - device_cache[device_id] = device_ip + entry = _entry_by_device_id(device_id) + if entry is None: + return + + updates = {} + + if device_cache[device_id] != device_ip: + updates[CONF_HOST] = device_ip + device_cache[device_id] = device_ip + + if entry.data.get(CONF_PRODUCT_KEY) != product_key: + updates[CONF_PRODUCT_KEY] = product_key + + if updates: + _LOGGER.debug("Update keys for device %s: %s", updates) + hass.config_entries.async_update_entry( + entry, data={**entry.data, **updates} + ) discovery = TuyaDiscovery(_device_discovered) diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index 75410d3..c6653c4 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -19,7 +19,13 @@ from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.entity import Entity from . import pytuya -from .const import CONF_LOCAL_KEY, CONF_PROTOCOL_VERSION, DOMAIN, TUYA_DEVICE +from .const import ( + CONF_LOCAL_KEY, + CONF_PRODUCT_KEY, + CONF_PROTOCOL_VERSION, + DOMAIN, + TUYA_DEVICE, +) _LOGGER = logging.getLogger(__name__) @@ -257,7 +263,7 @@ class LocalTuyaEntity(Entity, pytuya.ContextualLogger): }, "name": self._config_entry.data[CONF_FRIENDLY_NAME], "manufacturer": "Unknown", - "model": "Tuya generic", + "model": self._config_entry.data.get(CONF_PRODUCT_KEY, "Tuya generic"), "sw_version": self._config_entry.data[CONF_PROTOCOL_VERSION], } diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index 118e6f9..fc0c01e 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -20,6 +20,7 @@ from . import pytuya from .const import CONF_DPS_STRINGS # pylint: disable=unused-import from .const import ( CONF_LOCAL_KEY, + CONF_PRODUCT_KEY, CONF_PROTOCOL_VERSION, DATA_DISCOVERY, DOMAIN, @@ -212,7 +213,8 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors = {} if user_input is not None: if user_input[DISCOVERED_DEVICE] != CUSTOM_DEVICE: - self.selected_device = user_input[DISCOVERED_DEVICE].split(" ")[0] + device = user_input[DISCOVERED_DEVICE].split(" ")[0] + self.selected_device = self.devices[device] return await self.async_step_basic_info() # Use cache if available or fallback to manual discovery @@ -251,6 +253,10 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): try: self.basic_info = user_input + if self.selected_device is not None: + self.basic_info[CONF_PRODUCT_KEY] = self.selected_device[ + "productKey" + ] self.dps_strings = await validate_input(self.hass, user_input) return await self.async_step_pick_entity_type() except CannotConnect: @@ -266,10 +272,9 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): defaults = {} defaults.update(user_input or {}) if self.selected_device is not None: - device = self.devices[self.selected_device] - defaults[CONF_HOST] = device.get("ip") - defaults[CONF_DEVICE_ID] = device.get("gwId") - defaults[CONF_PROTOCOL_VERSION] = device.get("version") + defaults[CONF_HOST] = self.selected_device.get("ip") + defaults[CONF_DEVICE_ID] = self.selected_device.get("gwId") + defaults[CONF_PROTOCOL_VERSION] = self.selected_device.get("version") return self.async_show_form( step_id="basic_info", diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index 0209bb3..bd8a5d3 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -7,6 +7,7 @@ ATTR_VOLTAGE = "voltage" CONF_LOCAL_KEY = "local_key" CONF_PROTOCOL_VERSION = "protocol_version" CONF_DPS_STRINGS = "dps_strings" +CONF_PRODUCT_KEY = "product_key" # light CONF_BRIGHTNESS_LOWER = "brightness_lower" diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index 2e5a115..dd21e02 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -1,7 +1,7 @@ """Platform to locally control Tuya-based cover devices.""" import asyncio -import time import logging +import time from functools import partial import voluptuous as vol