Skip to content

Commit

Permalink
rename functions dps to dp_value and dps_conf tp dp_value_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Oct 25, 2023
1 parent 402cb0f commit dd22b91
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 61 deletions.
2 changes: 1 addition & 1 deletion custom_components/localtuya/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def status_updated(self):
"""Device status was updated."""
super().status_updated()

state = str(self.dps(self._dp_id)).lower()
state = str(self.dp_value(self._dp_id)).lower()
if state == self._config[CONF_STATE_ON].lower() or state == "true":
self._is_on = True
else:
Expand Down
18 changes: 9 additions & 9 deletions custom_components/localtuya/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ async def async_set_preset_mode(self, preset_mode):
def min_temp(self):
"""Return the minimum temperature."""
if _min_temp := self._config.get(CONF_MIN_TEMP_DP):
return self.dps_conf(CONF_MIN_TEMP_DP)
return self.dp_value_conf(CONF_MIN_TEMP_DP)
# DEFAULT_MIN_TEMP is in C
if self.temperature_unit == TEMP_FAHRENHEIT:
return DEFAULT_MIN_TEMP * 1.8 + 32
Expand All @@ -386,7 +386,7 @@ def min_temp(self):
def max_temp(self):
"""Return the maximum temperature."""
if self.has_config(CONF_MAX_TEMP_DP):
return self.dps_conf(CONF_MAX_TEMP_DP)
return self.dp_value_conf(CONF_MAX_TEMP_DP)
# DEFAULT_MAX_TEMP is in C
if self.temperature_unit == TEMP_FAHRENHEIT:
return DEFAULT_MAX_TEMP * 1.8 + 32
Expand All @@ -395,27 +395,27 @@ def max_temp(self):

def status_updated(self):
"""Device status was updated."""
self._state = self.dps(self._dp_id)
self._state = self.dp_value(self._dp_id)

if self.has_config(CONF_TARGET_TEMPERATURE_DP):
self._target_temperature = (
self.dps_conf(CONF_TARGET_TEMPERATURE_DP) * self._target_precision
self.dp_value_conf(CONF_TARGET_TEMPERATURE_DP) * self._target_precision
)

if self.has_config(CONF_CURRENT_TEMPERATURE_DP):
self._current_temperature = (
self.dps_conf(CONF_CURRENT_TEMPERATURE_DP) * self._precision
self.dp_value_conf(CONF_CURRENT_TEMPERATURE_DP) * self._precision
)

if self._has_presets:
if (
self.has_config(CONF_ECO_DP)
and self.dps_conf(CONF_ECO_DP) == self._conf_eco_value
and self.dp_value_conf(CONF_ECO_DP) == self._conf_eco_value
):
self._preset_mode = PRESET_ECO
else:
for preset, value in self._conf_preset_set.items(): # todo remove
if self.dps_conf(CONF_PRESET_DP) == value:
if self.dp_value_conf(CONF_PRESET_DP) == value:
self._preset_mode = preset
break
else:
Expand All @@ -427,7 +427,7 @@ def status_updated(self):
self._hvac_mode = HVACMode.OFF
else:
for mode, value in self._conf_hvac_mode_set.items():
if self.dps_conf(CONF_HVAC_MODE_DP) == value:
if self.dp_value_conf(CONF_HVAC_MODE_DP) == value:
self._hvac_mode = mode
break
else:
Expand All @@ -436,7 +436,7 @@ def status_updated(self):

# Update the current action
for action, value in self._conf_hvac_action_set.items():
if self.dps_conf(CONF_HVAC_ACTION_DP) == value:
if self.dp_value_conf(CONF_HVAC_ACTION_DP) == value:
self._hvac_action = action


Expand Down
14 changes: 5 additions & 9 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ def device_class(self):
"""Return the class of this device."""
return self._config.get(CONF_DEVICE_CLASS, None)

def dps(self, dp_index):
def dp_value(self, dp_index):
"""Return cached value for DPS index."""
value = self._status.get(str(dp_index))
if value is None and not self._dev_config_entry.get(CONF_NODE_ID):
Expand All @@ -659,27 +659,23 @@ def dps(self, dp_index):

return value

def dps_conf(self, conf_item):
"""Return value of datapoint for user specified config item.
This method looks up which DP a certain config item uses based on
user configuration and returns its value.
"""
def dp_value_conf(self, conf_item):
"""Return DP Value based on config key, If entity has the key."""
dp_index = self._config.get(conf_item)
if dp_index is None:
self.warning(
"Entity %s is requesting unset index for option %s",
self.entity_id,
conf_item,
)
return self.dps(dp_index)
return self.dp_value(dp_index)

def status_updated(self):
"""Device status was updated.
Override in subclasses and update entity specific state.
"""
state = self.dps(self._dp_id)
state = self.dp_value(self._dp_id)
self._state = state

# Keep record in last_state as long as not during connection/re-connection,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/localtuya/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ def status_restored(self, stored_state):
def status_updated(self):
"""Device status was updated."""
self._previous_state = self._state
self._state = self.dps(self._dp_id)
self._state = self.dp_value(self._dp_id)
if self._state.isupper():
self._open_cmd = self._open_cmd.upper()
self._close_cmd = self._close_cmd.upper()
self._stop_cmd = self._stop_cmd.upper()

if self.has_config(CONF_CURRENT_POSITION_DP):
curr_pos = self.dps_conf(CONF_CURRENT_POSITION_DP)
curr_pos = self.dp_value_conf(CONF_CURRENT_POSITION_DP)
if self._config.get(CONF_POSITION_INVERTED):
self._current_cover_position = 100 - curr_pos
else:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/localtuya/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ def speed_count(self) -> int:

def status_updated(self):
"""Get state of Tuya fan."""
self._is_on = self.dps(self._dp_id)
self._is_on = self.dp_value(self._dp_id)

current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL)
current_speed = self.dp_value_conf(CONF_FAN_SPEED_CONTROL)
if self._use_ordered_list:
_LOGGER.debug(
"Fan current_speed ordered_list_item_to_percentage: %s from %s",
Expand All @@ -242,11 +242,11 @@ def status_updated(self):
_LOGGER.debug("Fan current_percentage: %s", self._percentage)

if self.has_config(CONF_FAN_OSCILLATING_CONTROL):
self._oscillating = self.dps_conf(CONF_FAN_OSCILLATING_CONTROL)
self._oscillating = self.dp_value_conf(CONF_FAN_OSCILLATING_CONTROL)
_LOGGER.debug("Fan current_oscillating : %s", self._oscillating)

if self.has_config(CONF_FAN_DIRECTION):
value = self.dps_conf(CONF_FAN_DIRECTION)
value = self.dp_value_conf(CONF_FAN_DIRECTION)
if value is not None:
if value == self._config.get(CONF_FAN_DIRECTION_FWD):
self._direction = DIRECTION_FORWARD
Expand Down
6 changes: 3 additions & 3 deletions custom_components/localtuya/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ def is_on(self) -> bool:
@property
def mode(self) -> str | None:
"""Return the current mode."""
return self.dps_conf(self._dp_mode)
return self.dp_value_conf(self._dp_mode)

@property
def target_humidity(self) -> int | None:
"""Return the humidity we try to reach."""
target_dp = self._config.get(self._dp_set_humidity, None)

return self.dps_conf(target_dp) if target_dp else None
return self.dp_value_conf(target_dp) if target_dp else None

@property
def current_humidity(self) -> int | None:
"""Return the current humidity."""
curr_humidity = self._config.get(self._dp_current_humidity)

return self.dps_conf(self._dp_current_humidity) if curr_humidity else None
return self.dp_value_conf(self._dp_current_humidity) if curr_humidity else None

async def async_turn_on(self, **kwargs):
"""Turn the device on."""
Expand Down
28 changes: 9 additions & 19 deletions custom_components/localtuya/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,6 @@ def __init__(
if self.has_config(CONF_SCENE_VALUES) and self.has_config(
CONF_SCENE_VALUES_FRIENDLY
):
# temporary warnings
if ";" in self._config.get(CONF_SCENE_VALUES):
_LOGGER.warning(
f"{self._config.get(CONF_SCENE_VALUES)} Separate the sence values and friendly by ',' not ';'"
)
if ";" in self._config.get(CONF_SCENE_VALUES_FRIENDLY):
_LOGGER.warning(
f"{self._config.get(CONF_SCENE_VALUES_FRIENDLY)} Separate the sence values and friendly by ',' not ';'"
)
values_list = [
value.strip()
for value in self._config.get(CONF_SCENE_VALUES).split(",")
Expand Down Expand Up @@ -307,7 +298,7 @@ def is_music_mode(self):
return color_mode is not None and color_mode == MODE_MUSIC

def __is_color_rgb_encoded(self):
return len(self.dps_conf(CONF_COLOR)) > 12
return len(self.dp_value_conf(CONF_COLOR)) > 12

def __find_scene_by_scene_data(self, data):
return next(
Expand All @@ -317,14 +308,13 @@ def __find_scene_by_scene_data(self, data):

def __get_color_mode(self):
return (
self.dps_conf(CONF_COLOR_MODE)
self.dp_value_conf(CONF_COLOR_MODE)
if self.has_config(CONF_COLOR_MODE)
else MODE_WHITE
)

async def async_turn_on(self, **kwargs):
"""Turn on or control the light."""
_LOGGER.debug(f"This the kwargs {kwargs}")
states = {}
if not self.is_on:
states[self._dp_id] = True
Expand Down Expand Up @@ -426,14 +416,14 @@ async def async_turn_off(self, **kwargs):

def status_updated(self):
"""Device status was updated."""
self._state = self.dps(self._dp_id)
self._state = self.dp_value(self._dp_id)
supported = self.supported_features
self._effect = None
if supported & SUPPORT_BRIGHTNESS and self.has_config(CONF_BRIGHTNESS):
self._brightness = self.dps_conf(CONF_BRIGHTNESS)
self._brightness = self.dp_value_conf(CONF_BRIGHTNESS)

if supported & SUPPORT_COLOR:
color = self.dps_conf(CONF_COLOR)
color = self.dp_value_conf(CONF_COLOR)
if color is not None and not self.is_white_mode:
if self.__is_color_rgb_encoded():
hue = int(color[6:10], 16)
Expand All @@ -449,16 +439,16 @@ def status_updated(self):
self._brightness = value

if supported & SUPPORT_COLOR_TEMP:
self._color_temp = self.dps_conf(CONF_COLOR_TEMP)
self._color_temp = self.dp_value_conf(CONF_COLOR_TEMP)

if self.is_scene_mode and supported & SUPPORT_EFFECT:
if self.dps_conf(CONF_COLOR_MODE) != MODE_SCENE:
if self.dp_value_conf(CONF_COLOR_MODE) != MODE_SCENE:
self._effect = self.__find_scene_by_scene_data(
self.dps_conf(CONF_COLOR_MODE)
self.dp_value_conf(CONF_COLOR_MODE)
)
else:
self._effect = self.__find_scene_by_scene_data(
self.dps_conf(CONF_SCENE)
self.dp_value_conf(CONF_SCENE)
)
if self._effect == SCENE_CUSTOM:
if SCENE_CUSTOM not in self._effect_list:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def status_updated(self):
"""Device status was updated."""
super().status_updated()

state = self.dps(self._dp_id)
state = self.dp_value(self._dp_id)

# Check that received status update for this entity.
if state is not None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def unit_of_measurement(self):

def status_updated(self):
"""Device status was updated."""
state = self.dps(self._dp_id)
state = self.dp_value(self._dp_id)
scale_factor = self._config.get(CONF_SCALING)
if scale_factor is not None and isinstance(state, (int, float)):
state = round(state * scale_factor, DEFAULT_PRECISION)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def status_updated(self):
"""Device status was updated."""
super().status_updated()

state = str(self.dps(self._dp_id)).lower()
state = str(self.dp_value(self._dp_id)).lower()
if state == self._config[CONF_STATE_ON].lower() or state == "true":
self._is_on = True
else:
Expand Down
6 changes: 3 additions & 3 deletions custom_components/localtuya/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def extra_state_attributes(self):
"""Return device state attributes."""
attrs = {}
if self.has_config(CONF_CURRENT):
attrs[ATTR_CURRENT] = self.dps(self._config[CONF_CURRENT])
attrs[ATTR_CURRENT] = self.dp_value(self._config[CONF_CURRENT])
if self.has_config(CONF_CURRENT_CONSUMPTION):
attrs[ATTR_CURRENT_CONSUMPTION] = (
self.dps(self._config[CONF_CURRENT_CONSUMPTION]) / 10
self.dp_value(self._config[CONF_CURRENT_CONSUMPTION]) / 10
)
if self.has_config(CONF_VOLTAGE):
attrs[ATTR_VOLTAGE] = self.dps(self._config[CONF_VOLTAGE]) / 10
attrs[ATTR_VOLTAGE] = self.dp_value(self._config[CONF_VOLTAGE]) / 10

# Store the state
if self._state is not None:
Expand Down
16 changes: 8 additions & 8 deletions custom_components/localtuya/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async def async_send_command(self, command, params=None, **kwargs):

def status_updated(self):
"""Device status was updated."""
state_value = str(self.dps(self._dp_id))
state_value = str(self.dp_value(self._dp_id))

if state_value in self._idle_status_list:
self._state = STATE_IDLE
Expand All @@ -228,28 +228,28 @@ def status_updated(self):
self._state = STATE_CLEANING

if self.has_config(CONF_BATTERY_DP):
self._battery_level = self.dps_conf(CONF_BATTERY_DP)
self._battery_level = self.dp_value_conf(CONF_BATTERY_DP)

self._cleaning_mode = ""
if self.has_config(CONF_MODES):
self._cleaning_mode = self.dps_conf(CONF_MODE_DP)
self._cleaning_mode = self.dp_value_conf(CONF_MODE_DP)
self._attrs[MODE] = self._cleaning_mode

self._fan_speed = ""
if self.has_config(CONF_FAN_SPEEDS):
self._fan_speed = self.dps_conf(CONF_FAN_SPEED_DP)
self._fan_speed = self.dp_value_conf(CONF_FAN_SPEED_DP)

if self.has_config(CONF_CLEAN_TIME_DP):
self._attrs[CLEAN_TIME] = self.dps_conf(CONF_CLEAN_TIME_DP)
self._attrs[CLEAN_TIME] = self.dp_value_conf(CONF_CLEAN_TIME_DP)

if self.has_config(CONF_CLEAN_AREA_DP):
self._attrs[CLEAN_AREA] = self.dps_conf(CONF_CLEAN_AREA_DP)
self._attrs[CLEAN_AREA] = self.dp_value_conf(CONF_CLEAN_AREA_DP)

if self.has_config(CONF_CLEAN_RECORD_DP):
self._attrs[CLEAN_RECORD] = self.dps_conf(CONF_CLEAN_RECORD_DP)
self._attrs[CLEAN_RECORD] = self.dp_value_conf(CONF_CLEAN_RECORD_DP)

if self.has_config(CONF_FAULT_DP):
self._attrs[FAULT] = self.dps_conf(CONF_FAULT_DP)
self._attrs[FAULT] = self.dp_value_conf(CONF_FAULT_DP)
if self._attrs[FAULT] != 0:
self._state = STATE_ERROR

Expand Down

0 comments on commit dd22b91

Please sign in to comment.