Skip to content

Commit

Permalink
Merge pull request #1 from xerxes87/master
Browse files Browse the repository at this point in the history
Readiness for Home assistant 0.9.6
  • Loading branch information
Mmodarre committed Aug 16, 2019
2 parents 02a64c9 + 2d23635 commit cf95ab6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 49 deletions.
File renamed without changes.
58 changes: 33 additions & 25 deletions pyfujitsu/api.py → pyfujitseu/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
HEADER_CONTENT_TYPE = "Content-Type"
HEADER_VALUE_CONTENT_TYPE = "application/json"
HEADER_AUTHORIZATION = "Authorization"
SIGNIN_BODY = "{\r\n \"user\": {\r\n \"email\": \"%s\",\r\n \"application\": {\r\n \"app_id\": \"CJIOSP-id\",\r\n \"app_secret\": \"CJIOSP-Vb8MQL_lFiYQ7DKjN0eCFXznKZE\"\r\n },\r\n \"password\": \"%s\"\r\n }\r\n}"
API_BASE_URL = "https://ads-field.aylanetworks.com/apiv1/"
API_GET_ACCESS_TOKEN_URL = "https://user-field.aylanetworks.com/users/sign_in.json"

API_GET_DEVICES_URL = API_BASE_URL + "devices.json"
API_GET_PROPERTIES_URL = API_BASE_URL + "/dsns/{DSN}/properties.json"
API_SET_PROPERTIES_URL = API_BASE_URL + "/properties/{property}/datapoints.json"
ACCESS_TOKEN_FILE = 'token.txt'
#version 0.9.2.7

_LOGGER = logging.getLogger(__name__)

Expand All @@ -29,18 +23,36 @@ def _api_headers(access_token=None):
return headers

class Api:
def __init__(self,username,password):
def __init__(self,username,password,region='us',tokenpath='token.txt'):
self.username = username
self.password = password
#self._authenticate()
self._ACCESS_TOKEN_FILE = 'token.txt'
self.region = region

if region == 'eu':
self._SIGNIN_BODY = '{"user":{"email":"%s","password":"%s","application":{"app_id":"FGLair-eu-id","app_secret":"FGLair-eu-gpFbVBRoiJ8E3QWJ-QRULLL3j3U"}}}'
self._API_GET_ACCESS_TOKEN_URL = "https://user-field-eu.aylanetworks.com/users/sign_in.json"
API_BASE_URL = "https://ads-field-eu.aylanetworks.com/apiv1/"
elif region == 'cn':
self._SIGNIN_BODY = '{"user":{"email":"%s","password":"%s","application":{"app_id":"FGLairField-cn-id","app_secret":"FGLairField-cn-zezg7Y60YpAvy3HPwxvWLnd4Oh4"}}}'
self._API_GET_ACCESS_TOKEN_URL = "https://user-field.ayla.com.cn/users/sign_in.json"
API_BASE_URL = "https://ads-field.ayla.com.cn/apiv1/"
else:
self._SIGNIN_BODY = "{\r\n \"user\": {\r\n \"email\": \"%s\",\r\n \"application\": {\r\n \"app_id\": \"CJIOSP-id\",\r\n \"app_secret\": \"CJIOSP-Vb8MQL_lFiYQ7DKjN0eCFXznKZE\"\r\n },\r\n \"password\": \"%s\"\r\n }\r\n}"
self._API_GET_ACCESS_TOKEN_URL = "https://user-field.aylanetworks.com/users/sign_in.json"
API_BASE_URL = "https://ads-field.aylanetworks.com/apiv1/"

self._API_GET_PROPERTIES_URL = API_BASE_URL + "dsns/{DSN}/properties.json"
self._API_SET_PROPERTIES_URL = API_BASE_URL + "properties/{property}/datapoints.json"
self._API_GET_DEVICES_URL = API_BASE_URL + "devices.json"

self._ACCESS_TOKEN_FILE = tokenpath

def _get_devices(self,access_token=None):
if not self._check_token_validity(access_token):

## Token invalid requesting authentication
access_token = self._authenticate()
response = self._call_api("get",API_GET_DEVICES_URL,access_token=access_token)
response = self._call_api("get",self._API_GET_DEVICES_URL,access_token=access_token)
return response.json()

def get_devices_dsn(self, access_token=None):
Expand All @@ -49,23 +61,21 @@ def get_devices_dsn(self, access_token=None):
for device in devices:
devices_dsn.append(device['device']['dsn'])
return devices_dsn



def _get_device_properties(self,dsn):
access_token = self._read_token()
if not self._check_token_validity(access_token):
access_token = self._authenticate()

response = self._call_api("get",API_GET_PROPERTIES_URL.format(DSN=dsn),access_token=access_token)
response = self._call_api("get",self._API_GET_PROPERTIES_URL.format(DSN=dsn),access_token=access_token)
return response.json()

def _set_device_property(self,propertyCode,value):
access_token = self._read_token()
if not self._check_token_validity(access_token):
access_token = self._authenticate()

response = self._call_api("post",API_SET_PROPERTIES_URL.format(property=propertyCode),propertyValue=value,access_token=access_token)
response = self._call_api("post",self._API_SET_PROPERTIES_URL.format(property=propertyCode),propertyValue=value,access_token=access_token)

return response

Expand All @@ -74,29 +84,27 @@ def _get_device_property(self,propertyCode):
if not self._check_token_validity(access_token):
access_token = self._authenticate()

response = self._call_api("get",API_SET_PROPERTIES_URL.format(property=propertyCode),access_token=access_token)
response = self._call_api("get",self._API_SET_PROPERTIES_URL.format(property=propertyCode),access_token=access_token)
## Pay Attention the response is a HTTP request response object
# and by doing .json you would get a List
return response


def _check_token_validity(self,access_token=None):
if not access_token:
return False

return False
try:
self._call_api("get",API_GET_DEVICES_URL,access_token=access_token)
self._call_api("get",self._API_GET_DEVICES_URL,access_token=access_token)
except:
return False

return False
return True


def _authenticate(self):

response = self._call_api("POST",
API_GET_ACCESS_TOKEN_URL,
json=SIGNIN_BODY % (self.username,self.password),
self._API_GET_ACCESS_TOKEN_URL,
json=self._SIGNIN_BODY % (self.username,self.password),
headers= _api_headers())

response.json()['time'] = int(time.time())
Expand All @@ -107,7 +115,7 @@ def _authenticate(self):
#expires_in = response.json()['expires_in']


f = open(ACCESS_TOKEN_FILE, "w")
f = open(self._ACCESS_TOKEN_FILE, "w")
f.write(response.text)


Expand Down Expand Up @@ -154,4 +162,4 @@ def _call_api(self, method, url, access_token=None, **kwargs):

response = requests.request(method, url, data=kwargs.get("json"),headers=kwargs.get("headers"))
response.raise_for_status()
return response
return response
76 changes: 53 additions & 23 deletions pyfujitsu/splitAC.py → pyfujitseu/splitAC.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from homeassistant.components.pyfujitsu.api import Api as api
from .api import Api as api

#version 0.9.2.7

class splitAC:
def __init__(self,dsn,api):
Expand Down Expand Up @@ -111,7 +113,36 @@ def get_fan_speed_desc(self):
4 : 'Auto'
}
return FAN_SPEED_DICT[self.fan_speed['value']]


## Fan Swing mode
## 0: 'Horizontal',1: 'Down', 2: 'Unknown', 3: 'Swing'
def changeSwingMode(self, mode):
print(mode)
if mode.upper() == 'HORIZONTAL':
self.af_vertical_direction = 0
return None
if mode.upper() == 'DOWN':
self.af_vertical_direction = 1
return None
if mode.upper() == 'UNKNOWN':
self.af_vertical_direction = 2
return None
if mode.upper() == 'SWING':
self.af_vertical_direction = 3
return None

def get_swing_mode_desc(self):
SWING_LIST_DICT = {
0: 'Horizontal',
1: 'Down',
2: 'Unknown',
3: 'Swing'
}
try:
return SWING_LIST_DICT[self.af_vertical_direction['value']]
except TypeError:
return SWING_LIST_DICT[2]


## Direction Settings
## Vertical
Expand Down Expand Up @@ -154,15 +185,15 @@ def changeTemperature(self,newTemperature):
## Fixing temps if not given as multiplies of 10 less than 180
if newTemperature < 180:
newTemperature = newTemperature * 10
if (newTemperature > 180 and newTemperature < 320):
if (newTemperature >= 180 and newTemperature <= 320):
self.adjust_temperature = newTemperature
else:
raise Exception('out of range temperature!!')

## Operation Mode setting
def changeOperationMode(self,operationMode):
if not isinstance(operationMode, int):
operationMode = self._operation_mode_translate(operationMode.upper())
operationMode = self._operation_mode_translate(operationMode)
self.operation_mode = operationMode


Expand All @@ -179,7 +210,7 @@ def _get_prop_from_json(self,propertyName,properties):
def operation_mode(self): return self._operation_mode

@property
def operation_mode_desc(self): return self._operation_mode_translate(self.operation_mode['value']).capitalize()
def operation_mode_desc(self): return self._operation_mode_translate(self.operation_mode['value'])

@operation_mode.setter
def operation_mode(self,properties):
Expand Down Expand Up @@ -247,8 +278,7 @@ def fan_speed(self,properties):
self.refresh_properties()
else:
raise Exception('Wrong usage of the method!!')




@property
def economy_mode(self): return self._economy_mode
Expand Down Expand Up @@ -292,18 +322,18 @@ def af_horizontal_swing(self,properties):
self.refresh_properties()
else:
raise Exception('Wrong usage of the method!!')


@property
def af_vertical_direction(self): return self._af_vertical_direction

@af_vertical_direction.setter
def af_vertical_direction(self,properties):
if isinstance(properties,(list, tuple)):
self._af_vertical_direction = self._get_prop_from_json('af_vertical_direction',properties)
self._af_vertical_direction = self._get_prop_from_json('af_vertical_move_step1',properties)
elif isinstance(properties,int):
self._api._set_device_property(self.af_vertical_direction['key'],properties)
self.vertical_swing_off() ##If direction set then swing will be turned OFF
#self.vertical_swing_off() ##If direction set then swing will be turned OFF
self.refresh_properties()
else:
raise Exception('Wrong usage of the method or direction out of range!!')
Expand Down Expand Up @@ -338,17 +368,17 @@ def _get_device_property_history(self,propertyCode):
##Translate the operation mode to descriptive values and reverse
def _operation_mode_translate(self,operation_mode):
DICT_OPERATION_MODE = {
"OFF": 0,
"AUTO" : 2,
"COOL" : 3,
"DRY" : 4,
"FAN" : 5,
"HEAT" : 6,
0 : "OFF",
2 : "AUTO",
3 : "COOL",
4 : "DRY",
5 : "FAN",
6 : "HEAT"
"off": 0,
"auto" : 2,
"cool" : 3,
"dry" : 4,
"fan_only" : 5,
"heat" : 6,
0 : "off",
2 : "auto",
3 : "cool",
4 : "dry",
5 : "fan_only",
6 : "heat"
}
return DICT_OPERATION_MODE[operation_mode]
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

setuptools.setup(
name="pyfujitsu",
version="0.8.1",
version="0.9.0",
author="Mehdi Modarressi",
author_email="Luckposht@gmail.com",
maintainer="@xerxes87"
description="Python library to control Fujitsu General Airconditioners on AylaNetworks IoT platform",
long_description=long_description,
#long_description_content_type="text/markdown",
Expand Down

0 comments on commit cf95ab6

Please sign in to comment.