Replace RuntimeError with ServiceValidationError in Tuya

This commit is contained in:
epenet 2025-07-21 10:07:55 +00:00
parent d774de79db
commit 82c64febc5
5 changed files with 31 additions and 15 deletions

View File

@ -20,11 +20,12 @@ from homeassistant.components.climate import (
)
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import TuyaConfigEntry
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .entity import TuyaEntity
from .models import IntegerTypeData
@ -315,8 +316,9 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
def set_humidity(self, humidity: int) -> None:
"""Set new target humidity."""
if self._set_humidity is None:
raise RuntimeError(
"Cannot set humidity, device doesn't provide methods to set it"
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="action_dpcode_not_found",
)
self._send_command(
@ -356,9 +358,9 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
def set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
if self._set_temperature is None:
raise RuntimeError(
"Cannot set target temperature, device doesn't provide methods to"
" set it"
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="dpcode_not_found",
)
self._send_command(

View File

@ -16,11 +16,12 @@ from homeassistant.components.cover import (
CoverEntityFeature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import TuyaConfigEntry
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .entity import TuyaEntity
from .models import IntegerTypeData
@ -334,8 +335,9 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity):
def set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position."""
if self._set_position is None:
raise RuntimeError(
"Cannot set position, device doesn't provide methods to set it"
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="dpcode_not_found",
)
self._send_command(
@ -365,8 +367,9 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity):
def set_cover_tilt_position(self, **kwargs: Any) -> None:
"""Move the cover tilt to a specific position."""
if self._tilt is None:
raise RuntimeError(
"Cannot set tilt, device doesn't provide methods to set it"
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="dpcode_not_found",
)
self._send_command(

View File

@ -14,11 +14,12 @@ from homeassistant.components.humidifier import (
HumidifierEntityFeature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import TuyaConfigEntry
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .entity import TuyaEntity
from .models import IntegerTypeData
@ -178,8 +179,9 @@ class TuyaHumidifierEntity(TuyaEntity, HumidifierEntity):
def set_humidity(self, humidity: int) -> None:
"""Set new target humidity."""
if self._set_humidity is None:
raise RuntimeError(
"Cannot set humidity, device doesn't provide methods to set it"
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="dpcode_not_found",
)
self._send_command(

View File

@ -12,6 +12,7 @@ from homeassistant.components.number import (
)
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@ -463,7 +464,10 @@ class TuyaNumberEntity(TuyaEntity, NumberEntity):
def set_native_value(self, value: float) -> None:
"""Set new value."""
if self._number is None:
raise RuntimeError("Cannot set value, device doesn't provide type data")
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="dpcode_not_found",
)
self._send_command(
[

View File

@ -942,5 +942,10 @@
"name": "Siren"
}
}
},
"exceptions": {
"action_dpcode_not_found": {
"message": "Unable to process action as the device does not provide corresponding DPCode."
}
}
}