tinytuya.BulbDevice

class tinytuya.BulbDevice(*args, **kwargs)

Bases: Device

Represents a Tuya based Smart Light/Bulb.

Methods provided by this class

__init__(*args, **kwargs)

Represents a Tuya device.

brightness([state, nowait])

Return brightness value

bulb_has_capability(feature[, nowait])

colour_hsv([state, nowait])

Return colour as HSV value

colour_rgb([state, nowait])

Return colour as RGB value

colourtemp([state, nowait])

Return colour temperature

detect_bulb([response, nowait])

Attempt to determine BulbDevice Type A, B or C based on:

get_brightness_percentage([state, nowait])

get_colourtemp_percentage([state, nowait])

get_mode([state, nowait])

Return current working mode

get_value(feature[, state, nowait])

hexvalue_to_hsv(hexvalue[, hexformat])

Converts the hexvalue used by Tuya for colour representation into an HSV value.

hexvalue_to_rgb(hexvalue[, hexformat])

Converts the hexvalue used by Tuya for colour representation into an RGB value.

hsv_to_hexvalue(h, s, v, hexformat)

Convert an HSV value to the hex representation expected by Tuya Bulb.

rgb_to_hexvalue(r, g, b, hexformat)

Convert an RGB value to the hex representation expected by Tuya Bulb.

set_brightness(brightness[, nowait])

DEPRECATED Set the brightness value of an rgb bulb.

set_brightness_percentage([brightness, nowait])

Set the brightness value of an rgb bulb.

set_bulb_capabilities(mapping)

set_bulb_type([bulb_type, mapping])

set_colour(r, g, b[, nowait])

Set colour of an rgb bulb.

set_colourtemp(colourtemp[, nowait])

DEPRECATED Set the colour temperature of an rgb bulb.

set_colourtemp_percentage([colourtemp, nowait])

Set the colour temperature of an rgb bulb.

set_hsv(h, s, v[, nowait])

Set colour of an rgb bulb using h, s, v.

set_mode([mode, nowait])

Set bulb mode

set_music_colour(transition, red, green, blue)

Set a colour while in music mode

set_scene(scene[, scene_data, nowait])

Set to scene mode

set_timer(num_secs[, dps_id, nowait])

Set the timer

set_white([brightness, colourtemp, nowait])

DEPRECATED Set white coloured theme of an rgb bulb.

set_white_percentage([brightness, ...])

Set white coloured theme of an rgb bulb.

state([nowait])

Return state of Bulb

status([nowait])

Return device status.

turn_off([switch, nowait])

Turn the device off

turn_on([switch, nowait])

Turn the device on

turn_onoff(on[, switch, nowait])

Turn the device on or off

white_percentage([state, nowait])

Methods inherited from parent class

add_dps_to_request(dp_indicies)

Add a datapoint (DP) to be included in requests.

cache_clear()

cached_status([historic, nowait])

Return device last status if a persistent connection is open.

close()

detect_available_dps()

Return which datapoints are supported by the device.

find(did)

Mainly here for backwards compatibility.

generate_payload(command[, data, gwId, ...])

Generate the payload to send.

heartbeat([nowait])

Send a keep-alive HEART_BEAT command to keep the TCP connection open.

product()

Request AP_CONFIG Product Info from device.

receive()

Poll device to read any payload in the buffer.

send(payload)

Send single buffer payload.

set_dpsUsed(dps_to_request)

set_multiple_values(data[, nowait])

Set multiple indexes at the same time

set_retry(retry)

set_sendWait(s)

set_socketNODELAY(nodelay)

set_socketPersistent(persist)

set_socketRetryDelay(delay)

set_socketRetryLimit(limit)

set_socketTimeout(s)

set_status(on[, switch, nowait])

Set status of the device to 'on' or 'off'.

set_value(index, value[, nowait])

Set int value of any index.

set_version(version)

subdev_query([nowait])

Query for a list of sub-devices and their status

updatedps([index, nowait])

Request device to update index.

Attributes

BULB_FEATURE_BRIGHTNESS

BULB_FEATURE_COLOUR

BULB_FEATURE_COLOURTEMP

BULB_FEATURE_MODE

BULB_FEATURE_MUSIC

BULB_FEATURE_SCENE

BULB_FEATURE_SCENE_DATA

BULB_FEATURE_TIMER

DEFAULT_DPSET

DPS

DPS_INDEX_BRIGHTNESS

DPS_INDEX_COLOUR

DPS_INDEX_COLOURTEMP

DPS_INDEX_MODE

DPS_INDEX_MUSIC

DPS_INDEX_ON

DPS_INDEX_SCENE

DPS_INDEX_SETS

DPS_INDEX_TIMER

DPS_MODE_COLOUR

DPS_MODE_MUSIC

DPS_MODE_SCENE

DPS_MODE_SCENE_1

DPS_MODE_SCENE_2

DPS_MODE_SCENE_3

DPS_MODE_SCENE_4

DPS_MODE_WHITE

MUSIC_TRANSITION_FADE

MUSIC_TRANSITION_JUMP

__init__(*args, **kwargs)

Represents a Tuya device.

Parameters:

dev_id (str) – The device id.

Keyword Arguments:
  • address (str, optional) – The network address.

  • local_key (str, optional) – The encryption key. Defaults to None.

  • cid (str, optional) – Optional sub-device id. Default to None.

  • node_id (str, optional) – alias for cid

  • parent (XenonDevice, optional) – gateway device this device is a child of

port

The port to connect to.

Type:

int

Set Default Bulb Types

add_dps_to_request(dp_indicies)

Add a datapoint (DP) to be included in requests.

brightness(state=None, nowait=False)

Return brightness value

bulb_has_capability(feature, nowait=False)
cache_clear()
cached_status(historic=False, nowait=False)

Return device last status if a persistent connection is open.

Parameters:

nowait (bool) – If cached status is is not available, either call status() (when nowait=False) or immediately return None (when nowait=True)

Response:
json if cache is available, else

json from status() if nowait=False, or None if nowait=True

close()
colour_hsv(state=None, nowait=False)

Return colour as HSV value

colour_rgb(state=None, nowait=False)

Return colour as RGB value

colourtemp(state=None, nowait=False)

Return colour temperature

detect_available_dps()

Return which datapoints are supported by the device.

detect_bulb(response=None, nowait=False)
Attempt to determine BulbDevice Type A, B or C based on:

Type A has keys 1-9 Type B has keys 20-28 Type C is basic (non-CCT) and only has 1-2 (i.e Feit type bulbs from Costco)

Example status data:
Sylvania BR30 [v3.3, RGB+CCT]:

{‘20’: True, ‘21’: ‘colour’, ‘22’: 750, ‘23’: 278, ‘24’: ‘00f003e803e8’, ‘25’: ‘000e0d0000000000000000c803e8’, ‘26’: 0}

Geeni BW229 Smart Filament Bulb [v3.3, CCT only]:

{‘1’: True, ‘2’: 25, ‘3’: 0} 1: switch, 2: brightness, 3: colour temperature

No-name RGB+CCT (LED BULB W5K) [v3.5, RGB+CCT]:

{‘20’: True, ‘21’: ‘white’, ‘22’: 10, ‘23’: 0, ‘24’: ‘000003e803e8’, ‘25’: ‘000e0d0000000000000000c80000’, ‘26’: 0, ‘34’: False}

Feit soft white Filament [v3.5, 2700K only]:

{‘20’: True, ‘21’: ‘white’, ‘22’: 60, ‘25’: ‘000e0d0000000000000000c803e8’, ‘26’: 0, ‘34’: False, ‘41’: True} (No CCT (23) or colour (24), but does support scenes (25) and music mode (28))

Feit dimmer switch [v3.3, not a bulb]:

{‘1’: True, ‘2’: 10, ‘3’: 10, ‘4’: ‘incandescent’} Note: after a power cycle, only DP 2 is returned! The rest are not returned until after they are set 1: switch, 2: brightness, 3: minimum dim %, 4: installed bulb type (LED/incandescent)

static find(did)

Mainly here for backwards compatibility. Calling tinytuya.find_device() directly is recommended.

Parameters:

for (did = The specific Device ID you are looking)

Response:

(ip, version)

generate_payload(command, data=None, gwId=None, devId=None, uid=None, rawData=None, reqType=None)

Generate the payload to send.

Parameters:
  • command (str) – The type of command. This is one of the entries from payload_dict

  • data (dict, optional) – The data to send. This is what will be passed via the ‘dps’ entry

  • gwId (str, optional) – Will be used for gwId

  • devId (str, optional) – Will be used for devId

  • uid (str, optional) – Will be used for uid

get_brightness_percentage(state=None, nowait=False)
get_colourtemp_percentage(state=None, nowait=False)
get_mode(state=None, nowait=False)

Return current working mode

get_value(feature, state=None, nowait=False)
heartbeat(nowait=True)

Send a keep-alive HEART_BEAT command to keep the TCP connection open.

Devices only send an empty-payload response, so no need to wait for it.

Parameters:

nowait (bool) – True to send without waiting for response.

static hexvalue_to_hsv(hexvalue, hexformat=None)

Converts the hexvalue used by Tuya for colour representation into an HSV value.

Parameters:
  • hexvalue (string) – The hex representation generated by BulbDevice.rgb_to_hexvalue()

  • hexformat (str or None) – “rgb8”: The hex is in rrggbb0hhhssvv format “hsv16”: The hex is in hhhhssssvvvv format None: Try to auto-detect the format

static hexvalue_to_rgb(hexvalue, hexformat=None)

Converts the hexvalue used by Tuya for colour representation into an RGB value.

Parameters:
  • hexvalue (string) – The hex representation generated by BulbDevice.rgb_to_hexvalue()

  • hexformat (str or None) – “rgb8”: The hex is in rrggbb0hhhssvv format “hsv16”: The hex is in hhhhssssvvvv format None: Try to auto-detect the format

static hsv_to_hexvalue(h, s, v, hexformat)

Convert an HSV value to the hex representation expected by Tuya Bulb.

Parameters:
  • h (float) – colour Hue as float from 0-1

  • s (float) – colour Saturation as float from 0-1

  • v (float) – colour Value as float from 0-1

  • hexformat (str) – Selects the return format “rgb8”: rrggbb0hhhssvv “hsv16”: hhhhssssvvvv

product()

Request AP_CONFIG Product Info from device. [BETA]

receive()

Poll device to read any payload in the buffer. Timeout results in None returned.

static rgb_to_hexvalue(r, g, b, hexformat)

Convert an RGB value to the hex representation expected by Tuya Bulb.

While r, g and b are just hexadecimal values of the corresponding Red, Green and Blue values, the h, s and v values (which are values between 0 and 1) are scaled: hexformat=”rgb8”: 360 (h) and 255 (s and v) hexformat=”hsv16”: 360 (h) and 1000 (s and v)

Parameters:
  • r (int) – Value for the colour red as int from 0-255.

  • g (int) – Value for the colour green as int from 0-255.

  • b (int) – Value for the colour blue as int from 0-255.

  • hexformat (str) – Selects the return format “rgb8”: rrggbb0hhhssvv “hsv16”: hhhhssssvvvv

send(payload)

Send single buffer payload.

Parameters:

payload (bytes) – Data to send.

set_brightness(brightness, nowait=False)

DEPRECATED Set the brightness value of an rgb bulb.

Parameters:
  • brightness (int) – Value for the brightness (25-255).

  • nowait (bool) – True to send without waiting for response.

set_brightness_percentage(brightness=100, nowait=False)

Set the brightness value of an rgb bulb.

Parameters:
  • brightness (int) – Value for the brightness in percent (0-100)

  • nowait (bool) – True to send without waiting for response.

set_bulb_capabilities(mapping)
set_bulb_type(bulb_type=None, mapping=None)
set_colour(r, g, b, nowait=False)

Set colour of an rgb bulb.

Parameters:
  • r (float) – Value for the colour Red from 0.0-255.0.

  • g (float) – Value for the colour Green from 0.0-255.0.

  • b (float) – Value for the colour Blue from 0.0-255.0.

  • nowait (bool) – True to send without waiting for response.

set_colourtemp(colourtemp, nowait=False)

DEPRECATED Set the colour temperature of an rgb bulb.

Parameters:
  • colourtemp (int) – Value for the colour temperature (0-255).

  • nowait (bool) – True to send without waiting for response.

set_colourtemp_percentage(colourtemp=100, nowait=False)

Set the colour temperature of an rgb bulb.

Parameters:
  • colourtemp (int) – Value for the colour temperature in percentage (0-100).

  • nowait (bool) – True to send without waiting for response.

set_dpsUsed(dps_to_request)
set_hsv(h, s, v, nowait=False)

Set colour of an rgb bulb using h, s, v.

Parameters:
  • h (float) – colour Hue as float from 0-1

  • s (float) – colour Saturation as float from 0-1

  • v (float) – colour Value as float from 0-1

  • nowait (bool) – True to send without waiting for response.

set_mode(mode='white', nowait=False)

Set bulb mode

Parameters:
  • mode (string) – white,colour,scene,music

  • nowait (bool) – True to send without waiting for response.

set_multiple_values(data, nowait=False)

Set multiple indexes at the same time

Parameters:
  • data (dict) – array of index/value pairs to set

  • nowait (bool) – True to send without waiting for response.

set_music_colour(transition, red, green, blue, brightness=None, colourtemp=None, nowait=False)

Set a colour while in music mode

Parameters:
  • red (float) – red value, 0.0 - 255.0

  • green (float) – green value, 0.0 - 255.0

  • blue (float) – blue value, 0.0 - 255.0

  • brightness (float) – optional white light brightness

  • colourtemp (float) – optional white light colourtemp

  • transition (int) – optional transition. will use transition provided in set_musicmode() if not provided

set_retry(retry)
set_scene(scene, scene_data=None, nowait=False)

Set to scene mode

Parameters:
  • scene (int) – Value for the scene as int from 1-4 (Type A bulbs) or 1-N (Type B bulbs).

  • nowait (bool) – True to send without waiting for response.

set_sendWait(s)
set_socketNODELAY(nodelay)
set_socketPersistent(persist)
set_socketRetryDelay(delay)
set_socketRetryLimit(limit)
set_socketTimeout(s)
set_status(on, switch=1, nowait=False)

Set status of the device to ‘on’ or ‘off’.

Parameters:
  • on (bool) – True for ‘on’, False for ‘off’.

  • switch (int) – The switch to set

  • nowait (bool) – True to send without waiting for response.

set_timer(num_secs, dps_id=0, nowait=False)

Set the timer

Parameters:
  • num_secs – data to send to bulb

  • dps_id – do not use, kept for compatibility with Device.set_timer()

set_value(index, value, nowait=False)

Set int value of any index.

Parameters:
  • index (int) – index to set

  • value (int) – new value for the index

  • nowait (bool) – True to send without waiting for response.

set_version(version)
set_white(brightness=-1, colourtemp=-1, nowait=False)

DEPRECATED Set white coloured theme of an rgb bulb.

Parameters:
  • brightness (int) – Value for the brightness (A:25-255 or B:10-1000)

  • colourtemp (int) – Value for the colour temperature (A:0-255, B:0-1000).

  • nowait (bool) – True to send without waiting for response.

  • Default – Max Brightness and Min Colourtemp

Note: unlike set_colourtemp(), the colour temp will be silently ignored if the bulb does not support it

set_white_percentage(brightness=100, colourtemp=0, nowait=False)

Set white coloured theme of an rgb bulb.

Parameters:
  • brightness (int) – Value for the brightness in percent (0-100)

  • colourtemp (int) – Value for the colour temperature in percent (0-100)

  • nowait (bool) – True to send without waiting for response.

Note: unlike set_colourtemp(), the colour temp will be silently ignored if the bulb does not support it

state(nowait=False)

Return state of Bulb

status(nowait=False)

Return device status.

subdev_query(nowait=False)

Query for a list of sub-devices and their status

turn_off(switch=0, nowait=False)

Turn the device off

turn_on(switch=0, nowait=False)

Turn the device on

turn_onoff(on, switch=0, nowait=False)

Turn the device on or off

updatedps(index=None, nowait=False)

Request device to update index.

Parameters:
  • index (array) – list of dps to update (ex. [4, 5, 6, 18, 19, 20])

  • nowait (bool) – True to send without waiting for response.

white_percentage(state=None, nowait=False)
BULB_FEATURE_BRIGHTNESS = 'brightness'
BULB_FEATURE_COLOUR = 'colour'
BULB_FEATURE_COLOURTEMP = 'colourtemp'
BULB_FEATURE_MODE = 'mode'
BULB_FEATURE_MUSIC = 'music'
BULB_FEATURE_SCENE = 'scene'
BULB_FEATURE_SCENE_DATA = 'scene_data'
BULB_FEATURE_TIMER = 'timer'
DEFAULT_DPSET = {   'A': {   'brightness': 3,              'colour': 5,              'colourtemp': 4,              'mode': 2,              'music': 8,              'scene': 6,              'scene_data': None,              'switch': 1,              'timer': 7,              'value_hexformat': 'rgb8',              'value_max': 255,              'value_min': 25},     'B': {   'brightness': 22,              'colour': 24,              'colourtemp': 23,              'mode': 21,              'music': 28,              'scene': 25,              'scene_data': 25,              'switch': 20,              'timer': 26,              'value_hexformat': 'hsv16',              'value_max': 1000,              'value_min': 10},     'C': {   'brightness': 2,              'colour': None,              'colourtemp': 3,              'mode': None,              'music': None,              'scene': None,              'scene_data': None,              'switch': 1,              'timer': None,              'value_hexformat': 'rgb8',              'value_max': 255,              'value_min': 25},     'None': {   'brightness': None,                 'colour': None,                 'colourtemp': None,                 'mode': None,                 'music': None,                 'scene': None,                 'scene_data': None,                 'switch': 1,                 'timer': None,                 'value_hexformat': 'rgb8',                 'value_max': 255,                 'value_min': 0}}
DPS = 'dps'
DPS_INDEX_BRIGHTNESS = {'A': '3', 'B': '22', 'C': '2'}
DPS_INDEX_COLOUR = {'A': '5', 'B': '24', 'C': None}
DPS_INDEX_COLOURTEMP = {'A': '4', 'B': '23', 'C': None}
DPS_INDEX_MODE = {'A': '2', 'B': '21', 'C': '1'}
DPS_INDEX_MUSIC = {'A': None, 'B': '27', 'C': None}
DPS_INDEX_ON = {'A': '1', 'B': '20', 'C': '1'}
DPS_INDEX_SCENE = {'A': '2', 'B': '25', 'C': None}
DPS_INDEX_SETS = [20, 1]
DPS_INDEX_TIMER = {'A': None, 'B': '26', 'C': None}
DPS_MODE_COLOUR = 'colour'
DPS_MODE_MUSIC = 'music'
DPS_MODE_SCENE = 'scene'
DPS_MODE_SCENE_1 = 'scene_1'
DPS_MODE_SCENE_2 = 'scene_2'
DPS_MODE_SCENE_3 = 'scene_3'
DPS_MODE_SCENE_4 = 'scene_4'
DPS_MODE_WHITE = 'white'
MUSIC_TRANSITION_FADE = 1
MUSIC_TRANSITION_JUMP = 0