Coder Social home page Coder Social logo

perl-webservice-tuya-iot-api's Introduction

NAME

WebService::Tuya::IoT::API - Perl library to access the Tuya IoT API

SYNOPSIS

use WebService::Tuya::IoT::API;
my $ws             = WebService::Tuya::IoT::API->new(client_id=>$client_id, client_secret=>$client_secret);
my $access_token   = $ws->access_token;
my $device_status  = $ws->device_status($deviceid);
my $response       = $ws->device_commands($deviceid, {code=>'switch_1', value=>$boolean ? \1 : \0});

DESCRIPTION

Perl library to access the Tuya IoT API to control and read the state of Tuya compatible smart devices.

Tuya compatible smart devices include outlets, switches, lights, window covers, etc.

SETUP

Other projects have documented device setup, so I will not go into details here. The TinyTuya setup documentation is the best that I have found. Please note some setup instructions step through the process of creating an app inside the Tuya IoT project, but I was able to use the Smart Life app for device discovery and pair the app with the API by scanning the QR code.

  • You must configure your devices with the Smart Life (iOS,Android) app.
  • You must create an account and cloud project on the Tuya IoT Platform.
  • You must link the Smart Life app to the cloud project with the QR code from the devices tab.
  • You must configure the correct project data center to see your devices in the project (Note: My devices call the Western America Data Center even though I'm located in Eastern America).
  • You must use the host associated to your data center. The default host is the Americas which is set as openapi.tuyaus.com.

CONSTRUCTORS

new

my $ws = WebService::Tuya::IoT::API->new(
                                         client_id     => $my_client_id,
                                         client_secret => $my_client_secret,
                                         http_hostname => $my_http_hostname, #default openapi.tuyaus.com
                                        );

PROPERTIES

http_hostname

Sets and returns the host name for the API service endpoint.

$ws->http_hostname("openapi.tuyaus.com"); #Americas
$ws->http_hostname("openapi.tuyacn.com"); #China
$ws->http_hostname("openapi.tuyaeu.com"); #Europe
$ws->http_hostname("openapi.tuyain.com"); #India

default: openapi.tuyaus.com

client_id

Sets and returns the Client ID found on https://iot.tuya.com/ project overview page.

client_secret

Sets and returns the Client Secret found on https://iot.tuya.com/ project overview page.

METHODS

api

Calls the Tuya IoT API and returns the parsed JSON data structure. This method automatically handles access token and web request signatures.

my $response = $ws->api(GET  => 'v1.0/token?grant_type=1');                                                             #get access token
my $response = $ws->api(GET  => "v1.0/iot-03/devices/$deviceid/status");                                                #get status of $deviceid
my $response = $ws->api(POST => "v1.0/iot-03/devices/$deviceid/commands", {commands=>[{code=>'switch_1', value=>\0}]}); #set switch_1 off on $deviceid

References:

api_get, api_post, api_put, api_delete

Wrappers around the api method with hard coded HTTP methods.

access_token

Wrapper around api method which calls and caches the token web service for a temporary access token to be used for subsequent web service calls.

my $access_token = $ws->access_token; #requires client_id and client_secret

device_status

Wrapper around api method to access the device status API destination.

my $device_status = $ws->device_status($deviceid);

device_status_code_value

Wrapper around api method to access the device status API destination and return the value for the given switch code.

my $value = $ws->device_status_code_value($deviceid, $code); #isa JSON Boolean

default: code => switch_1

device_information

Wrapper around api method to access the device information API destination.

my $device_information = $ws->device_information($deviceid);

device_freeze_state

Wrapper around api method to access the device freeze-state API destination.

my $device_freeze_state = $ws->device_freeze_state($deviceid);

device_factory_infos

Wrapper around api method to access the device factory-infos API destination.

my $device_factory_infos = $ws->device_factory_infos($deviceid);

device_specification

Wrapper around api method to access the device specification API destination.

my $device_specification = $ws->device_specification($deviceid);

device_protocol

Wrapper around api method to access the device protocol API destination.

my $device_protocol = $ws->device_protocol($deviceid);

device_properties

Wrapper around api method to access the device properties API destination.

my $device_properties = $ws->device_properties($deviceid);

device_commands

Wrapper around api method to access the device commands API destination.

my $switch   = 'switch_1';
my $value    = $boolean ? \1 : \0;
my $response = $ws->device_commands($deviceid, {code=>$switch, value=>$value});

device_command_code_value

Wrapper around device_commands for one command with code and value keys;

my $response = $ws->device_command_code_value($deviceid, $code, $value);

ACCESSORS

ua

Returns an HTTP::Tiny web client user agent

SEE ALSO

AUTHOR

Michael R. Davis

COPYRIGHT AND LICENSE

MIT License

Copyright (c) 2023 Michael R. Davis

perl-webservice-tuya-iot-api's People

Contributors

mrdvt92 avatar

Watchers

 avatar

Forkers

wouldsmina

perl-webservice-tuya-iot-api's Issues

using smartLife account

Hello,

I am using SmartLife application. When I tested your Perl library, I encountered an error indicating a non-existent account. Is the API for SmartLife different from that of Tuya? If so, do you know how to work around this issue without having to create a Tuya account?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.