Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to example to work with 1.2.4 #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/electrasmart/api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
DELAY_BETWEEM_SID_REQUESTS = int(timedelta(minutes=5).total_seconds())
SID_EXPIRATION = int(timedelta(minutes=15).total_seconds())

class electra:
ATTR_STATUS = 'status'
STATUS_SUCCESS =0
ATTR_DATA = 'data'
ATTR_RES = 'res'
ATTR_TOKEN = 'token'

@dataclass
class Attributes:
Expand Down
4 changes: 4 additions & 0 deletions src/electrasmart/device/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class OperationMode:
ON = "ON"
OFF = "OFF"
STANDBY = "STBY"
SWING_BOTH = ""
SWING_HORIZONTAL = ""
SWING_VERTICAL = ""
SWING_OFF = ""


@dataclass
Expand Down
59 changes: 59 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import asyncio
import aiohttp
import sys

from electrasmart.api import ElectraAPI, ElectraApiError
from electrasmart.api.utils import generate_imei
from electrasmart.api.const import electra
from electrasmart.device import ElectraAirConditioner
from electrasmart.device.const import OperationMode


async def main():
session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False), timeout=aiohttp.ClientTimeout(total=10))
api = ElectraAPI(session)

# User phone number
phone_number = "0521234567"
# Generate token
imei = generate_imei()
try:
resp = await api.generate_new_token(phone_number=phone_number, imei=imei)
except ElectraApiError as e:
# handle error
pass

otp = input("Enter the OTP you recieved via SMS")
# more error handling
if resp[electra.ATTR_STATUS] == electra.STATUS_SUCCESS:
if resp[electra.ATTR_DATA][electra.ATTR_RES] != electra.STATUS_SUCCESS:
# Wrong phone number or unregistered user
sys.exit(1)

resp = await api.validate_one_time_password(otp=otp, imei=imei, phone_number=phone_number)
if resp[electra.ATTR_DATA][electra.ATTR_RES] == electra.STATUS_SUCCESS:
token = resp[electra.ATTR_DATA][electra.ATTR_TOKEN]
else:
# wrong OTP
sys.exit(1)

try:
resp = await api.fetch_devices()
ac_devices = api.devices()
except ElectraApiError as e:
sys.exit(1)

for ac in ac_devices:
assert(ac, ElectraAirConditioner)
if ac.name == "Saloon AC":
ac.turn_on()
ac.set_mode(OperationMode.MODE_COOL)
ac.set_temperature(17)
ac.set_fan_speed(OperationMode.FAN_SPEED_HIGH)
# ac.set_vertical_swing(OperationMode.OPER_ON)
api.set_state(ac) # This will send the conf to the AC


loop = asyncio.get_event_loop()
loop.run_until_complete(main())