diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index b2e467d..de205b2 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -260,6 +260,14 @@ class LocalTuyaEntity(Entity): return value + def dps_conf(self, conf_item): + """Return value of datapoint for user speified config item. + + This method looks up which DP a certain config item uses based on + user configuration and returns its value. + """ + return self.dps(self._config.get(conf_item)) + def status_updated(self): """Device status was updated. diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index 23b93e0..a1d6351 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -2,6 +2,13 @@ import logging from functools import partial +import voluptuous as vol + +from homeassistant.const import ( + CONF_ID, + CONF_BRIGHTNESS, + CONF_COLOR_TEMP, +) from homeassistant.components.light import ( LightEntity, DOMAIN, @@ -20,16 +27,13 @@ MIN_MIRED = 153 MAX_MIRED = 370 UPDATE_RETRY_LIMIT = 3 -DPS_INDEX_ON = "1" -DPS_INDEX_MODE = "2" -DPS_INDEX_BRIGHTNESS = "3" -DPS_INDEX_COLOURTEMP = "4" -DPS_INDEX_COLOUR = "5" - def flow_schema(dps): """Return schema used in config flow.""" - return {} + return { + vol.Optional(CONF_BRIGHTNESS): vol.In(dps), + vol.Optional(CONF_COLOR_TEMP): vol.In(dps), + } class LocaltuyaLight(LocalTuyaEntity, LightEntity): @@ -90,7 +94,7 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): if ATTR_BRIGHTNESS in kwargs: self._device.set_dps( - max(int(kwargs[ATTR_BRIGHTNESS]), 25), DPS_INDEX_BRIGHTNESS + max(int(kwargs[ATTR_BRIGHTNESS]), 25), self._config.get(CONF_BRIGHTNESS) ) if ATTR_HS_COLOR in kwargs: @@ -102,7 +106,7 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): - (255 / (MAX_MIRED - MIN_MIRED)) * (int(kwargs[ATTR_COLOR_TEMP]) - MIN_MIRED) ) - self._device.set_dps(color_temp, DPS_INDEX_COLOURTEMP) + self._device.set_dps(color_temp, self._config.get(CONF_COLOR_TEMP)) def turn_off(self, **kwargs): """Turn Tuya light off.""" @@ -112,13 +116,13 @@ class LocaltuyaLight(LocalTuyaEntity, LightEntity): """Device status was updated.""" self._state = self.dps(self._dps_id) - brightness = self.dps(DPS_INDEX_BRIGHTNESS) + brightness = self.dps_conf(CONF_BRIGHTNESS) if brightness is not None: brightness = min(brightness, 255) brightness = max(brightness, 25) self._brightness = brightness - self._color_temp = self.dps(DPS_INDEX_COLOURTEMP) + self._color_temp = self.dps_conf(CONF_COLOR_TEMP) async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaLight, flow_schema) diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 549eb36..5ab3783 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -55,7 +55,9 @@ "device_class": "Device Class", "scaling": "Scaling Factor", "state_on": "On Value", - "state_off": "Off Value" + "state_off": "Off Value", + "brightness": "Brightness", + "color_temp": "Color Temperature" } } } @@ -90,7 +92,9 @@ "device_class": "Device Class", "scaling": "Scaling Factor", "state_on": "On Value", - "state_off": "Off Value" + "state_off": "Off Value", + "brightness": "Brightness", + "color_temp": "Color Temperature" } }, "yaml_import": {