rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.82k stars 544 forks source link

Regular users cannot call `localtuya.set_dp` #1134

Closed ov1d1u closed 2 months ago

ov1d1u commented 1 year ago

The problem

Running a script by a regular user (which is not an admin) that calls the localtuya.set_dp service yelds into an "Unauthorized" result. Running the script as an administrator works.

The service call:

service: localtuya.set_dp
data:
  device_id: [redacted]
  dp: 245
  value: "2"

The result:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 484, in _async_run
    return await self.script.async_run(script_vars, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1524, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 409, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 453, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 476, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1744, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1781, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 744, in admin_handler
    raise Unauthorized(context=call.context)
homeassistant.exceptions.Unauthorized: Unauthorized

Environment

Steps to reproduce

  1. Create a service that calls localtuya.set_dp
  2. Call the service from a non-admin user

Configuration configuration.yaml or config_flow

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2022.11.3",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.10.7",
    "docker": true,
    "arch": "aarch64",
    "timezone": "Europe/Bucharest",
    "os_name": "Linux",
    "os_version": "5.15.61-v8",
    "supervisor": "2022.10.2",
    "host_os": "Home Assistant OS 9.3",
    "docker_version": "20.10.18",
    "chassis": "embedded",
    "run_as_root": true
  },
  "custom_components": {
    "localtuya": {
      "version": "4.1.1",
      "requirements": []
    }
  },
  "integration_manifest": {
    "domain": "localtuya",
    "name": "LocalTuya integration",
    "version": "4.1.1",
    "documentation": "https://github.com/rospogrigio/localtuya/",
    "dependencies": [],
    "codeowners": [
      "@rospogrigio",
      "@postlund"
    ],
    "issue_tracker": "https://github.com/rospogrigio/localtuya/issues",
    "requirements": [],
    "config_flow": true,
    "iot_class": "local_push",
    "is_built_in": false
  },
  "data": {
    "region": "eu",
    "username": "localtuya",
    "no_cloud": true,
    "client_id": "...",
    "client_secret": "...",
    "user_id": "...",
    "devices": {
      "bf9da2901bc2f8614dtnlt": {
        "friendly_name": "Smart Pet Feeder",
        "local_key": "d24...f5f",
        "host": "192.168.0.123",
        "device_id": "bf9...nlt",
        "protocol_version": "3.3",
        "manual_dps_strings": "101,239,240,241,243",
        "dps_strings": [
          "101 (value: -1)",
          "239 (value: -1)",
          "240 (value: -1)",
          "241 (value: -1)",
          "243 (value: -1)"
        ],
        "entities": [
          {
            "id": 101,
            "friendly_name": "Smart Pet Feeder Indicator Light",
            "restore_on_reconnect": true,
            "is_passive_entity": false,
            "platform": "switch"
          },
          {
            "id": 239,
            "friendly_name": "Smart Pet Feeder Food Bucket Empty",
            "state_on": "less",
            "state_off": "full",
            "platform": "binary_sensor"
          },
          {
            "id": 240,
            "friendly_name": "Smart Pet Feeder Jam State",
            "state_on": "on",
            "state_off": "off",
            "platform": "binary_sensor"
          },
          {
            "id": 241,
            "friendly_name": "Smart Pet Feeder Stuck State",
            "state_on": "on",
            "state_off": "off",
            "platform": "binary_sensor"
          },
          {
            "id": 243,
            "friendly_name": "Smart Pet Feeder Last Quantity",
            "platform": "sensor"
          }
        ]
      }
    },
    "updated_at": "1668669685794",
    "cloud_devices": {}
  }
}
ov1d1u commented 2 months ago

Closing this as it was fixed by #1135