mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Improve
This commit is contained in:
parent
d0c70eca7d
commit
5484663f45
@ -14,14 +14,14 @@ from homeassistant.components.humidifier import (
|
|||||||
HumidifierEntityFeature,
|
HumidifierEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ServiceValidationError
|
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from . import TuyaConfigEntry
|
from . import TuyaConfigEntry
|
||||||
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
|
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType
|
||||||
from .entity import TuyaEntity
|
from .entity import TuyaEntity
|
||||||
from .models import IntegerTypeData
|
from .models import IntegerTypeData
|
||||||
|
from .util import ActionDPCodeNotFoundError
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@ -171,27 +171,27 @@ class TuyaHumidifierEntity(TuyaEntity, HumidifierEntity):
|
|||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
if self._switch_dpcode is None:
|
if self._switch_dpcode is None:
|
||||||
raise ServiceValidationError(
|
raise ActionDPCodeNotFoundError(
|
||||||
translation_domain=DOMAIN,
|
self.device,
|
||||||
translation_key="action_dpcode_not_found",
|
self.entity_description.dpcode or self.entity_description.key,
|
||||||
)
|
)
|
||||||
self._send_command([{"code": self._switch_dpcode, "value": True}])
|
self._send_command([{"code": self._switch_dpcode, "value": True}])
|
||||||
|
|
||||||
def turn_off(self, **kwargs: Any) -> None:
|
def turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
if self._switch_dpcode is None:
|
if self._switch_dpcode is None:
|
||||||
raise ServiceValidationError(
|
raise ActionDPCodeNotFoundError(
|
||||||
translation_domain=DOMAIN,
|
self.device,
|
||||||
translation_key="action_dpcode_not_found",
|
self.entity_description.dpcode or self.entity_description.key,
|
||||||
)
|
)
|
||||||
self._send_command([{"code": self._switch_dpcode, "value": False}])
|
self._send_command([{"code": self._switch_dpcode, "value": False}])
|
||||||
|
|
||||||
def set_humidity(self, humidity: int) -> None:
|
def set_humidity(self, humidity: int) -> None:
|
||||||
"""Set new target humidity."""
|
"""Set new target humidity."""
|
||||||
if self._set_humidity is None:
|
if self._set_humidity is None:
|
||||||
raise ServiceValidationError(
|
raise ActionDPCodeNotFoundError(
|
||||||
translation_domain=DOMAIN,
|
self.device,
|
||||||
translation_key="action_dpcode_not_found",
|
self.entity_description.humidity,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._send_command(
|
self._send_command(
|
||||||
|
@ -12,7 +12,6 @@ from homeassistant.components.number import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
|
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ServiceValidationError
|
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
@ -27,6 +26,7 @@ from .const import (
|
|||||||
)
|
)
|
||||||
from .entity import TuyaEntity
|
from .entity import TuyaEntity
|
||||||
from .models import IntegerTypeData
|
from .models import IntegerTypeData
|
||||||
|
from .util import ActionDPCodeNotFoundError
|
||||||
|
|
||||||
# All descriptions can be found here. Mostly the Integer data types in the
|
# All descriptions can be found here. Mostly the Integer data types in the
|
||||||
# default instructions set of each category end up being a number.
|
# default instructions set of each category end up being a number.
|
||||||
@ -464,10 +464,7 @@ class TuyaNumberEntity(TuyaEntity, NumberEntity):
|
|||||||
def set_native_value(self, value: float) -> None:
|
def set_native_value(self, value: float) -> None:
|
||||||
"""Set new value."""
|
"""Set new value."""
|
||||||
if self._number is None:
|
if self._number is None:
|
||||||
raise ServiceValidationError(
|
raise ActionDPCodeNotFoundError(self.device, self.entity_description.key)
|
||||||
translation_domain=DOMAIN,
|
|
||||||
translation_key="action_dpcode_not_found",
|
|
||||||
)
|
|
||||||
|
|
||||||
self._send_command(
|
self._send_command(
|
||||||
[
|
[
|
||||||
|
@ -945,7 +945,7 @@
|
|||||||
},
|
},
|
||||||
"exceptions": {
|
"exceptions": {
|
||||||
"action_dpcode_not_found": {
|
"action_dpcode_not_found": {
|
||||||
"message": "Unable to process action as the device does not provide corresponding DPCode."
|
"message": "Unable to process action as the device does not provide corresponding DPCode (expected one of {expected} in {available})."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from tuya_sharing import CustomerDevice
|
||||||
|
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
|
||||||
|
from .const import DOMAIN, DPCode
|
||||||
|
|
||||||
|
|
||||||
def remap_value(
|
def remap_value(
|
||||||
value: float,
|
value: float,
|
||||||
@ -15,3 +21,25 @@ def remap_value(
|
|||||||
if reverse:
|
if reverse:
|
||||||
value = from_max - value + from_min
|
value = from_max - value + from_min
|
||||||
return ((value - from_min) / (from_max - from_min)) * (to_max - to_min) + to_min
|
return ((value - from_min) / (from_max - from_min)) * (to_max - to_min) + to_min
|
||||||
|
|
||||||
|
|
||||||
|
class ActionDPCodeNotFoundError(ServiceValidationError):
|
||||||
|
"""Custom exception for action DP code not found errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, device: CustomerDevice, expected: str | DPCode | tuple[DPCode, ...] | None
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the error with device and expected DP codes."""
|
||||||
|
if expected is None:
|
||||||
|
expected = () # empty tuple for no expected codes
|
||||||
|
elif isinstance(expected, str):
|
||||||
|
expected = (DPCode(expected),)
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="action_dpcode_not_found",
|
||||||
|
translation_placeholders={
|
||||||
|
"expected": str(sorted([dp.value for dp in expected])),
|
||||||
|
"available": str(sorted(device.function.keys())),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
@ -171,6 +171,10 @@ async def test_turn_on_unsupported(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert err.value.translation_key == "action_dpcode_not_found"
|
assert err.value.translation_key == "action_dpcode_not_found"
|
||||||
|
assert err.value.translation_placeholders == {
|
||||||
|
"expected": "['switch', 'switch_spray']",
|
||||||
|
"available": ("[]"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -197,6 +201,10 @@ async def test_turn_off_unsupported(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert err.value.translation_key == "action_dpcode_not_found"
|
assert err.value.translation_key == "action_dpcode_not_found"
|
||||||
|
assert err.value.translation_placeholders == {
|
||||||
|
"expected": "['switch', 'switch_spray']",
|
||||||
|
"available": ("[]"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -226,3 +234,7 @@ async def test_set_humidity_unsupported(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert err.value.translation_key == "action_dpcode_not_found"
|
assert err.value.translation_key == "action_dpcode_not_found"
|
||||||
|
assert err.value.translation_placeholders == {
|
||||||
|
"expected": "['dehumidify_set_value']",
|
||||||
|
"available": ("[]"),
|
||||||
|
}
|
||||||
|
@ -119,3 +119,12 @@ async def test_set_value_no_function(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert err.value.translation_key == "action_dpcode_not_found"
|
assert err.value.translation_key == "action_dpcode_not_found"
|
||||||
|
assert err.value.translation_placeholders == {
|
||||||
|
"expected": "['delay_set']",
|
||||||
|
"available": (
|
||||||
|
"['alarm_delay_time', 'alarm_time', 'master_mode', 'master_state', "
|
||||||
|
"'muffling', 'sub_admin', 'sub_class', 'switch_alarm_light', "
|
||||||
|
"'switch_alarm_propel', 'switch_alarm_sound', 'switch_kb_light', "
|
||||||
|
"'switch_kb_sound', 'switch_mode_sound']"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user