Skip to content

Commit

Permalink
Merge branch 'rospogrigio:master' into add_light_scene_config_rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tcm0116 committed Feb 15, 2024
2 parents ffc3a04 + 490ad9e commit 997c4c4
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 63 deletions.
7 changes: 4 additions & 3 deletions custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async def _async_reconnect(now):
_handle_reload,
)

hass.helpers.service.async_register_admin_service(
hass.services.async_register(
DOMAIN, SERVICE_SET_DP, _handle_set_dp, schema=SERVICE_SET_DP_SCHEMA
)

Expand Down Expand Up @@ -255,8 +255,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
res = await tuya_api.async_get_access_token()
if res != "ok":
_LOGGER.error("Cloud API connection failed: %s", res)
_LOGGER.info("Cloud API connection succeeded.")
res = await tuya_api.async_get_devices_list()
else:
_LOGGER.info("Cloud API connection succeeded.")
res = await tuya_api.async_get_devices_list()
hass.data[DOMAIN][DATA_CLOUD] = tuya_api

async def setup_entities(device_ids):
Expand Down
29 changes: 17 additions & 12 deletions custom_components/localtuya/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@
PRESET_ECO,
PRESET_HOME,
PRESET_NONE,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE,
ClimateEntityFeature,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_TEMPERATURE_UNIT,
PRECISION_HALVES,
PRECISION_TENTHS,
PRECISION_WHOLE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature,
)

from .common import LocalTuyaEntity, async_setup_entry
Expand Down Expand Up @@ -191,11 +188,11 @@ def supported_features(self):
"""Flag supported features."""
supported_features = 0
if self.has_config(CONF_TARGET_TEMPERATURE_DP):
supported_features = supported_features | SUPPORT_TARGET_TEMPERATURE
supported_features = supported_features | ClimateEntityFeature.TARGET_TEMPERATURE
if self.has_config(CONF_MAX_TEMP_DP):
supported_features = supported_features | SUPPORT_TARGET_TEMPERATURE_RANGE
supported_features = supported_features | ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
if self.has_config(CONF_PRESET_DP) or self.has_config(CONF_ECO_DP):
supported_features = supported_features | SUPPORT_PRESET_MODE
supported_features = supported_features | ClimateEntityFeature.PRESET_MODE
return supported_features

@property
Expand All @@ -215,8 +212,8 @@ def temperature_unit(self):
self._config.get(CONF_TEMPERATURE_UNIT, DEFAULT_TEMPERATURE_UNIT)
== TEMPERATURE_FAHRENHEIT
):
return TEMP_FAHRENHEIT
return TEMP_CELSIUS
return UnitOfTemperature.FAHRENHEIT
return UnitOfTemperature.CELSIUS

@property
def hvac_mode(self):
Expand Down Expand Up @@ -343,14 +340,22 @@ def min_temp(self):
"""Return the minimum temperature."""
if self.has_config(CONF_MIN_TEMP_DP):
return self.dps_conf(CONF_MIN_TEMP_DP)
return DEFAULT_MIN_TEMP
# DEFAULT_MIN_TEMP is in C
if self.temperature_unit == UnitOfTemperature.FAHRENHEIT:
return DEFAULT_MIN_TEMP * 1.8 + 32
else:
return DEFAULT_MIN_TEMP

@property
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 DEFAULT_MAX_TEMP
# DEFAULT_MAX_TEMP is in C
if self.temperature_unit == UnitOfTemperature.FAHRENHEIT:
return DEFAULT_MAX_TEMP * 1.8 + 32
else:
return DEFAULT_MAX_TEMP

def status_updated(self):
"""Device status was updated."""
Expand Down
5 changes: 4 additions & 1 deletion custom_components/localtuya/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ async def async_make_request(self, method, url, body=None, headers={}):

async def async_get_access_token(self):
"""Obtain a valid access token."""
resp = await self.async_make_request("GET", "/v1.0/token?grant_type=1")
try:
resp = await self.async_make_request("GET", "/v1.0/token?grant_type=1")
except requests.exceptions.ConnectionError:
return "Request failed, status ConnectionError"

if not resp.ok:
return "Request failed, status " + str(resp.status)
Expand Down
10 changes: 3 additions & 7 deletions custom_components/localtuya/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
from homeassistant.components.cover import (
ATTR_POSITION,
DOMAIN,
SUPPORT_CLOSE,
SUPPORT_OPEN,
SUPPORT_SET_POSITION,
SUPPORT_STOP,
CoverEntity,
CoverEntity, CoverEntityFeature,
)

from .common import LocalTuyaEntity, async_setup_entry
Expand Down Expand Up @@ -80,9 +76,9 @@ def __init__(self, device, config_entry, switchid, **kwargs):
@property
def supported_features(self):
"""Flag supported features."""
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
if self._config[CONF_POSITIONING_MODE] != COVER_MODE_NONE:
supported_features = supported_features | SUPPORT_SET_POSITION
supported_features = supported_features | CoverEntityFeature.SET_POSITION
return supported_features

@property
Expand Down
4 changes: 2 additions & 2 deletions custom_components/localtuya/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ async def start(self):
"""Start discovery by listening to broadcasts."""
loop = asyncio.get_running_loop()
listener = loop.create_datagram_endpoint(
lambda: self, local_addr=("0.0.0.0", 6666)
lambda: self, local_addr=("0.0.0.0", 6666), reuse_port=True
)
encrypted_listener = loop.create_datagram_endpoint(
lambda: self, local_addr=("0.0.0.0", 6667)
lambda: self, local_addr=("0.0.0.0", 6667), reuse_port=True
)

self._listeners = await asyncio.gather(listener, encrypted_listener)
Expand Down
11 changes: 4 additions & 7 deletions custom_components/localtuya/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
DIRECTION_FORWARD,
DIRECTION_REVERSE,
DOMAIN,
SUPPORT_DIRECTION,
SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED,
FanEntity,
FanEntity, FanEntityFeature,
)
from homeassistant.util.percentage import (
int_states_in_range,
Expand Down Expand Up @@ -194,13 +191,13 @@ def supported_features(self) -> int:
features = 0

if self.has_config(CONF_FAN_OSCILLATING_CONTROL):
features |= SUPPORT_OSCILLATE
features |= FanEntityFeature.OSCILLATE

if self.has_config(CONF_FAN_SPEED_CONTROL):
features |= SUPPORT_SET_SPEED
features |= FanEntityFeature.SET_SPEED

if self.has_config(CONF_FAN_DIRECTION):
features |= SUPPORT_DIRECTION
features |= FanEntityFeature.DIRECTION

return features

Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ async def update_dps(self, dps=None):
Args:
dps([int]): list of dps to update, default=detected&whitelisted
"""
if self.version in [3.2, 3.3]: # 3.2 behaves like 3.3 with type_0d
if self.version in [3.2, 3.3, 3.4]: # 3.2 behaves like 3.3 with type_0d
if dps is None:
if not self.dps_cache:
await self.detect_available_dps()
Expand Down
6 changes: 3 additions & 3 deletions custom_components/localtuya/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"power_outlet": {
"title": "Add subswitch",
"description": "You are about to add subswitch number `{number}`. If you want to add another, tick `Add another switch` before continuing.",
"description": "You are about to add subswitch number `{number}`. If you want to add another, tick `Add another switch` before continuing.",
"data": {
"id": "ID",
"name": "Name",
Expand Down Expand Up @@ -103,7 +103,7 @@
"fan_speed_min": "minimum fan speed integer",
"fan_speed_max": "maximum fan speed integer",
"fan_speed_ordered_list": "Fan speed modes list (overrides speed min/max)",
"fan_direction":"fan direction dps",
"fan_direction": "fan direction dps",
"fan_direction_forward": "forward dps string",
"fan_direction_reverse": "reverse dps string",
"fan_dps_type": "DP value type",
Expand Down Expand Up @@ -138,4 +138,4 @@
}
},
"title": "LocalTuya"
}
}
28 changes: 26 additions & 2 deletions custom_components/localtuya/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"fan_speed_min": "minimum fan speed integer",
"fan_speed_max": "maximum fan speed integer",
"fan_speed_ordered_list": "Fan speed modes list (overrides speed min/max)",
"fan_direction":"fan direction dps",
"fan_direction": "fan direction dps",
"fan_direction_forward": "forward dps string",
"fan_direction_reverse": "reverse dps string",
"fan_dps_type": "DP value type",
Expand Down Expand Up @@ -200,5 +200,29 @@
}
}
},
"services": {
"reload": {
"name": "Reload",
"description": "Reload localtuya and reconnect to all devices."
},
"set_dp": {
"name": "Set datapoint",
"description": "Change the value of a datapoint (DP)",
"fields": {
"device_id": {
"name": "Device ID",
"description": "Device ID of device to change datapoint value for"
},
"dp": {
"name": "DP",
"description": "Datapoint index"
},
"value": {
"name": "Value",
"description": "New value to set"
}
}
}
},
"title": "LocalTuya"
}
}
24 changes: 24 additions & 0 deletions custom_components/localtuya/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,29 @@
}
}
},
"services": {
"reload": {
"name": "Reload",
"description": "Reload localtuya and reconnect to all devices."
},
"set_dp": {
"name": "Set datapoint",
"description": "Change the value of a datapoint (DP)",
"fields": {
"device_id": {
"name": "Device ID",
"description": "Device ID of device to change datapoint value for"
},
"dp": {
"name": "DP",
"description": "Datapoint index"
},
"value": {
"name": "Value",
"description": "New value to set"
}
}
}
},
"title": "LocalTuya"
}
24 changes: 24 additions & 0 deletions custom_components/localtuya/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,29 @@
}
}
},
"services": {
"reload": {
"name": "Reload",
"description": "Reload localtuya and reconnect to all devices."
},
"set_dp": {
"name": "Set datapoint",
"description": "Change the value of a datapoint (DP)",
"fields": {
"device_id": {
"name": "Device ID",
"description": "Device ID of device to change datapoint value for"
},
"dp": {
"name": "DP",
"description": "Datapoint index"
},
"value": {
"name": "Value",
"description": "New value to set"
}
}
}
},
"title": "LocalTuya"
}
29 changes: 10 additions & 19 deletions custom_components/localtuya/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@
STATE_IDLE,
STATE_PAUSED,
STATE_RETURNING,
SUPPORT_BATTERY,
SUPPORT_FAN_SPEED,
SUPPORT_LOCATE,
SUPPORT_PAUSE,
SUPPORT_RETURN_HOME,
SUPPORT_START,
SUPPORT_STATE,
SUPPORT_STATUS,
SUPPORT_STOP,
StateVacuumEntity,
StateVacuumEntity, VacuumEntityFeature,
)

from .common import LocalTuyaEntity, async_setup_entry
Expand Down Expand Up @@ -123,21 +114,21 @@ def __init__(self, device, config_entry, switchid, **kwargs):
def supported_features(self):
"""Flag supported features."""
supported_features = (
SUPPORT_START
| SUPPORT_PAUSE
| SUPPORT_STOP
| SUPPORT_STATUS
| SUPPORT_STATE
VacuumEntityFeature.START
| VacuumEntityFeature.PAUSE
| VacuumEntityFeature.STOP
| VacuumEntityFeature.STATUS
| VacuumEntityFeature.STATE
)

if self.has_config(CONF_RETURN_MODE):
supported_features = supported_features | SUPPORT_RETURN_HOME
supported_features = supported_features | VacuumEntityFeature.RETURN_HOME
if self.has_config(CONF_FAN_SPEED_DP):
supported_features = supported_features | SUPPORT_FAN_SPEED
supported_features = supported_features | VacuumEntityFeature.FAN_SPEED
if self.has_config(CONF_BATTERY_DP):
supported_features = supported_features | SUPPORT_BATTERY
supported_features = supported_features | VacuumEntityFeature.BATTERY
if self.has_config(CONF_LOCATE_DP):
supported_features = supported_features | SUPPORT_LOCATE
supported_features = supported_features | VacuumEntityFeature.LOCATE

return supported_features

Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "Local Tuya",
"homeassistant": "0.116.0"
"homeassistant": "2024.1.0"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.black]
target-version = ["py38", "py39"]
target-version = ["py311"]
include = 'custom_components/localtuya/.*\.py'

# pylint config stolen from Home Assistant
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ max-line-length = 120
ignore = E203, W503

[mypy]
python_version = 3.9
python_version = 3.11
ignore_errors = true
follow_imports = silent
ignore_missing_imports = true
Expand Down
Loading

0 comments on commit 997c4c4

Please sign in to comment.