2020-09-27 16:52:56 +02:00
|
|
|
|
# WARNING: ce code s'appuie sur une API découverte par rétro-ingénérie.
|
|
|
|
|
# Il peut cesser de fonctionner sans préavis.
|
2016-05-19 15:15:55 +02:00
|
|
|
|
|
2016-05-21 16:59:51 +02:00
|
|
|
|
# TODO:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
##
|
2016-05-21 16:59:51 +02:00
|
|
|
|
|
2018-02-22 21:29:53 +01:00
|
|
|
|
from urllib import request
|
|
|
|
|
from http.cookiejar import CookieJar
|
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
|
|
2016-05-19 15:15:55 +02:00
|
|
|
|
authors = (
|
|
|
|
|
'Gilles "Almtesh" Émilien MOREL',
|
2018-02-22 21:29:53 +01:00
|
|
|
|
)
|
2020-09-27 16:52:56 +02:00
|
|
|
|
name = 'homesfr pour Python 3'
|
|
|
|
|
version = '1.3'
|
2016-05-19 15:15:55 +02:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
# Modes utilisables
|
2016-05-19 15:15:55 +02:00
|
|
|
|
MODE_OFF = 0
|
|
|
|
|
MODE_CUSTOM = 1
|
|
|
|
|
MODE_ON = 2
|
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
# Types de capteurs
|
|
|
|
|
PRESENCE_DETECTOR = 'PIR_DETECTOR' # https://boutique.home.sfr.fr/detecteur-de-mouvement
|
|
|
|
|
MAGNETIC_OPENNING_DETECTOR = 'MAGNETIC' # https://boutique.home.sfr.fr/detecteur-d-ouverture-de-porte-ou-fenetre
|
|
|
|
|
SMOKE_DETECTOR = 'SMOKE' # https://boutique.home.sfr.fr/detecteur-de-fumee
|
|
|
|
|
SIREN = 'SIREN' # https://boutique.home.sfr.fr/sirene-interieure (et peut-être https://boutique.home.sfr.fr/sirene-exterieure)
|
|
|
|
|
REMOTE_CONTROLER = 'REMOTE' # https://boutique.home.sfr.fr/telecommande
|
|
|
|
|
KEYPAD_CONTROLER = 'KEYPAD' # https://boutique.home.sfr.fr/clavier-de-commande
|
|
|
|
|
PRESENCE_CAMERA_DETECTOR = 'PIR_CAMERA' # https://boutique.home.sfr.fr/camera
|
|
|
|
|
|
|
|
|
|
base_url = 'https://home.sfr.fr'
|
|
|
|
|
|
|
|
|
|
# Authentification
|
|
|
|
|
auth_path = '/mysensors'
|
|
|
|
|
auth_ok_url = 'https://home.sfr.fr/logged'
|
|
|
|
|
auth_post_url = 'https://boutique.home.sfr.fr/authentification'
|
|
|
|
|
auth_referer = 'https://boutique.home.sfr.fr/authentification?back=service'
|
|
|
|
|
auth_user_field = 'email'
|
|
|
|
|
auth_pass_field = 'passwd'
|
|
|
|
|
auth_extra_fields = {'back': 'service', 'token_sso': '', 'error_sso': '', 'SubmitLogin': 'OK'}
|
|
|
|
|
auth_logout_path = '/deconnexion'
|
|
|
|
|
|
|
|
|
|
# Chemin pour la liste des capteurs
|
|
|
|
|
sensors_list = '/mysensors'
|
|
|
|
|
|
|
|
|
|
# Chemin pour les alertes
|
|
|
|
|
alerts_path = '/listalert'
|
|
|
|
|
|
|
|
|
|
# Détection
|
|
|
|
|
mode_get_path = '/mysensors'
|
|
|
|
|
mode_get_label = 'alarm_mode'
|
|
|
|
|
mode_set_path = '/alarmmode'
|
|
|
|
|
mode_set_field = 'action' # Name for GET field
|
|
|
|
|
mode_off = 'OFF' # Value for off
|
|
|
|
|
mode_custom = 'CUSTOM' # Value for custom
|
|
|
|
|
mode_on = 'ON' # Value for on
|
|
|
|
|
|
|
|
|
|
# Cémera
|
|
|
|
|
cameras_list = '/homescope/mycams'
|
|
|
|
|
camera_snapshot = '/homescope/snapshot?size=4'
|
|
|
|
|
camera_snapshot_mac = 'mac'
|
|
|
|
|
camera_video = '/homescope/flv'
|
|
|
|
|
camera_video_mac = 'mac'
|
|
|
|
|
camera_recordings_list = '/listenr'
|
|
|
|
|
camera_recordings_delete = '/delenr'
|
|
|
|
|
camera_recordings_start = '/homescope/record'
|
|
|
|
|
camera_recordings_stop = '/homescope/stoprecord'
|
|
|
|
|
camera_recordings_mac = 'mac'
|
|
|
|
|
camera_get_config_path = '/homescope/camsettings'
|
|
|
|
|
camera_get_config_mac = 'mac'
|
|
|
|
|
camera_get_config_flip = 'FLIP'
|
|
|
|
|
camera_get_config_leds = 'LEDMODE'
|
|
|
|
|
camera_get_config_petmode = 'pet_mode'
|
|
|
|
|
camera_get_config_recording = 'rec24'
|
|
|
|
|
camera_get_config_privacy = 'privacy'
|
|
|
|
|
camera_get_config_name = 'NAME'
|
|
|
|
|
camera_set_config_path = '/homescope/camsettings'
|
|
|
|
|
camera_set_config_mac = 'mac'
|
|
|
|
|
camera_set_config_flip = 'flip'
|
|
|
|
|
camera_set_config_leds = 'led_mode'
|
|
|
|
|
camera_set_config_petmode = 'pet_mode'
|
|
|
|
|
camera_set_config_recording = 'rec24'
|
|
|
|
|
# Le paramètre privacy se gère avec le bouton derrière la caméra.
|
|
|
|
|
camera_set_config_name = 'name'
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
# Capteurs
|
|
|
|
|
sensors_list = '/mysensors'
|
|
|
|
|
sensors_label = 'Sensor'
|
|
|
|
|
sensors_label_id = 'id'
|
|
|
|
|
sensors_mac_field = 'deviceMac'
|
|
|
|
|
sensors_type_field = 'deviceType'
|
|
|
|
|
sensors_model_field = 'deviceModel'
|
|
|
|
|
sensors_version_field = 'deviceVersion'
|
|
|
|
|
sensors_name_field = 'name'
|
|
|
|
|
sensors_longname_field = 'long_name'
|
|
|
|
|
sensors_namegender_field = 'name_gender'
|
|
|
|
|
sensors_batterylevel_field = 'batteryLevel'
|
|
|
|
|
sensors_signal_field = 'signalLevel'
|
|
|
|
|
sensors_status_field = 'status'
|
|
|
|
|
sensors_status_value_ok = 'OK'
|
2019-01-15 23:02:35 +01:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
# Fil d'événements
|
|
|
|
|
logs_path = '/getlog?page=1&nbparpage=10000' # je pense qu'on récupère tous les événements avec cette valeur
|
|
|
|
|
logs_labels = 'LOG'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bytes2file (b):
|
2016-05-21 16:59:51 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne une classe semblable à un fichier contant b
|
2016-05-21 16:59:51 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
from io import BytesIO
|
|
|
|
|
r = BytesIO ()
|
|
|
|
|
r.write (b)
|
|
|
|
|
r.seek (0)
|
|
|
|
|
return (r)
|
2016-05-25 17:19:16 +02:00
|
|
|
|
|
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
def bytes2image (b):
|
|
|
|
|
'''
|
|
|
|
|
Retourne une Image PIL contenant l'image donnée en b
|
|
|
|
|
'''
|
|
|
|
|
from PIL import Image
|
|
|
|
|
f = bytes2file (b)
|
|
|
|
|
r = Image.open (f)
|
|
|
|
|
return (r)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_xml_tree (fp):
|
|
|
|
|
'''
|
|
|
|
|
Retourne une variable itérable contenant les données d'un arbre XML
|
|
|
|
|
'''
|
|
|
|
|
def build_tree (element):
|
|
|
|
|
if tuple (element) == ():
|
|
|
|
|
return (element.tag, dict (element.items ()), element.text)
|
2016-05-26 16:57:48 +02:00
|
|
|
|
else:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
sub = []
|
|
|
|
|
for i in element:
|
|
|
|
|
sub.append (build_tree (i))
|
|
|
|
|
return (element.tag, dict (element.items ()), sub)
|
|
|
|
|
from xml.etree import ElementTree as ET
|
|
|
|
|
fp.seek (0)
|
|
|
|
|
root = ET.parse (fp).getroot ()
|
|
|
|
|
return (build_tree (root))
|
2016-05-25 17:19:16 +02:00
|
|
|
|
|
2019-01-15 23:02:35 +01:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
class HomeSFR ():
|
2016-05-25 17:19:16 +02:00
|
|
|
|
def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Instancie la classe avec un identifiant et un mot de passe, ou des cookies
|
|
|
|
|
On peut définir les identifiants et les cookies, la classe utilisera les cookies par défaut et les identifiants si on n'est pas connecté
|
|
|
|
|
debug fourni des informations supplémentaires sur la sortie standard, s'il est à False, cette classe n'envoie rien sur la sortie standard
|
2016-05-25 17:19:16 +02:00
|
|
|
|
'''
|
|
|
|
|
self.DEBUG = debug
|
|
|
|
|
if self.DEBUG:
|
|
|
|
|
print (name + ' ' + version)
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Auteurs:')
|
2016-05-25 17:19:16 +02:00
|
|
|
|
for i in authors:
|
|
|
|
|
print (' - ' + i)
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if username is not None:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('initalisé avec l\'identifiant ' + username)
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if cookies is not None:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('initialisé avec des cookies')
|
2016-05-25 17:19:16 +02:00
|
|
|
|
print ('debug = ' + str (debug))
|
|
|
|
|
print ('autologin = ' + str (autologin))
|
|
|
|
|
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if (username is None or password is None) and cookies is None:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
raise TypeError ('Vous devez définir des identifiant ou des cookies !')
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.username = username
|
|
|
|
|
self.password = password
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if self.username is not None and self.password is not None:
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.autologin = autologin
|
|
|
|
|
else:
|
|
|
|
|
self.autologin = False
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if cookies is None:
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.cookies = CookieJar ()
|
2016-12-01 11:27:01 +01:00
|
|
|
|
elif type (cookies) == CookieJar:
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.cookies = cookies
|
2016-12-01 11:27:01 +01:00
|
|
|
|
else:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
raise TypeError ('Les cookies doivent être de type CookieJar !')
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.opener = request.build_opener (request.HTTPCookieProcessor (self.cookies))
|
2016-05-19 15:15:55 +02:00
|
|
|
|
|
|
|
|
|
def __str__ (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Returne des informations sur l'état de l'instance
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if self.username is not None:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (name + ' ' + version + '\nUtilisateur : ' + self.username + '\nDebug : ' + str (self.DEBUG))
|
2016-05-23 09:23:26 +02:00
|
|
|
|
else:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (name + ' ' + version + '\nUtilisateur : Inconnu, authentifié avec des cookies.\nDebug : ' + str (self.DEBUG))
|
|
|
|
|
|
2016-05-19 15:15:55 +02:00
|
|
|
|
def test_login (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne l'état de l'authentification
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
|
|
|
|
try:
|
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Test de l\'authentification')
|
|
|
|
|
self.opener.open (base_url + auth_path)
|
2016-05-19 15:15:55 +02:00
|
|
|
|
except HTTPError:
|
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Non connecté')
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (False)
|
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Connecté')
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (True)
|
|
|
|
|
|
|
|
|
|
def login (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
S'authentifier auprès du service
|
|
|
|
|
Retourne True si c'est réussi, False sinon
|
|
|
|
|
Si seuls les cookies sont définis, la méthode retournera False, même si nous sommes authentifiés, pour savoir si on est authentifiés, utiliser test_login ()
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
2018-02-22 21:29:53 +01:00
|
|
|
|
if self.username is not None and self.password is not None:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
self.opener.open (auth_referer)
|
|
|
|
|
data = auth_extra_fields
|
|
|
|
|
data [auth_user_field] = self.username
|
|
|
|
|
data [auth_pass_field] = self.password
|
2018-01-27 16:22:13 +01:00
|
|
|
|
data = bytes (urlencode (data), 'UTF8')
|
2016-05-23 09:23:26 +02:00
|
|
|
|
if self.DEBUG:
|
2018-02-22 21:29:53 +01:00
|
|
|
|
print ('Cookies ' + str (len (self.cookies)))
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Envoi de ' + str (data))
|
|
|
|
|
a = self.opener.open (auth_post_url, data = data)
|
2016-05-23 09:23:26 +02:00
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Authentification redirigée vers ' + a.geturl ())
|
|
|
|
|
return (a.geturl () == auth_ok_url)
|
2016-05-23 09:23:26 +02:00
|
|
|
|
else:
|
|
|
|
|
return (False)
|
2016-05-19 15:15:55 +02:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
def get_or_autologin (self, url, data = None):
|
2016-05-26 16:57:48 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Fais une requête, si une erreur 403 est retourné, tente une authentification automatiquement (si réglé dans le paramètre autologin), sinon lève une exception
|
2016-05-26 16:57:48 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
try:
|
|
|
|
|
return (self.opener.open (url, data = data))
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
if '403' in str (e) and self.autologin:
|
|
|
|
|
self.login ()
|
|
|
|
|
return (self.opener.open (url, data = data))
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2016-05-26 16:57:48 +02:00
|
|
|
|
|
2016-05-23 15:11:43 +02:00
|
|
|
|
def logout (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Se déconnecter
|
|
|
|
|
L'instance devrait être supprimée après l'appel de cette fonction
|
2016-05-23 15:11:43 +02:00
|
|
|
|
'''
|
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Demande de déconnexion')
|
|
|
|
|
self.opener.open (base_url + auth_logout_path)
|
2016-05-23 15:11:43 +02:00
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Destruction des cookies')
|
2016-05-23 15:11:43 +02:00
|
|
|
|
del self.cookies
|
|
|
|
|
self.cookies = None
|
|
|
|
|
return (None)
|
|
|
|
|
|
|
|
|
|
def get_cookies (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Récupérer les cookies
|
|
|
|
|
Il est recommandé de supprimer l'instance après cette fonction
|
2016-05-23 15:11:43 +02:00
|
|
|
|
'''
|
|
|
|
|
return (self.cookies)
|
|
|
|
|
|
2016-05-19 15:15:55 +02:00
|
|
|
|
def set_mode (self, mode):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Modifie le mode de détection
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
|
|
|
|
if mode == MODE_OFF:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
m = mode_off
|
2016-05-19 15:15:55 +02:00
|
|
|
|
elif mode == MODE_CUSTOM:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
m = mode_custom
|
2016-05-19 15:15:55 +02:00
|
|
|
|
elif mode == MODE_ON:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
m = mode_on
|
2016-05-19 15:15:55 +02:00
|
|
|
|
else:
|
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Vous devriez utiliser les constantes MODE_OFF, MODE_ON et MODE_CUSTOM.')
|
2016-05-19 15:15:55 +02:00
|
|
|
|
raise ValueError
|
2020-09-27 16:52:56 +02:00
|
|
|
|
r = base_url + mode_set_path + '?' + mode_set_field + '=' + m
|
2016-05-19 15:15:55 +02:00
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Demande ' + r)
|
|
|
|
|
self.get_or_autologin (r)
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (True)
|
|
|
|
|
|
|
|
|
|
def get_mode (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le mode de détection
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
r = base_url + mode_get_path
|
2016-05-19 15:15:55 +02:00
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Demande ' + r)
|
|
|
|
|
a = bytes2file (self.get_or_autologin (r).read ())
|
|
|
|
|
b = get_xml_tree (a) [1]
|
|
|
|
|
c = b [mode_get_label]
|
2016-05-19 15:15:55 +02:00
|
|
|
|
if self.DEBUG:
|
2020-09-27 16:52:56 +02:00
|
|
|
|
print ('Mode de détection ' + c)
|
|
|
|
|
if (c == mode_off):
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (MODE_OFF)
|
2020-09-27 16:52:56 +02:00
|
|
|
|
if (c == mode_custom):
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (MODE_CUSTOM)
|
2020-09-27 16:52:56 +02:00
|
|
|
|
if (c == mode_on):
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (MODE_ON)
|
|
|
|
|
return (None)
|
|
|
|
|
|
|
|
|
|
def list_sensors (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne une liste des IDs des capteurs
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
r = base_url + sensors_list
|
|
|
|
|
a = bytes2file (self.get_or_autologin (r).read ())
|
|
|
|
|
b = get_xml_tree (a)
|
2016-05-19 15:15:55 +02:00
|
|
|
|
r = []
|
2020-09-27 16:52:56 +02:00
|
|
|
|
for i in b [2]:
|
|
|
|
|
try:
|
|
|
|
|
r.append (i [1] [sensors_label_id])
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass
|
2016-05-19 15:15:55 +02:00
|
|
|
|
return (list (r))
|
|
|
|
|
|
|
|
|
|
def get_sensor (self, id):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne un objet Sensor à partir de l'ID
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
r = Sensor (id, self.get_or_autologin)
|
2016-05-25 17:19:16 +02:00
|
|
|
|
r.refresh ()
|
|
|
|
|
return (r)
|
2016-05-19 15:15:55 +02:00
|
|
|
|
|
|
|
|
|
def get_all_sensors (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne un tuple d'objet Sensor contenant tous les capteurs
|
2016-05-19 15:15:55 +02:00
|
|
|
|
'''
|
|
|
|
|
r = []
|
|
|
|
|
for i in self.list_sensors ():
|
|
|
|
|
r.append (self.get_sensor (i))
|
2016-05-23 08:41:07 +02:00
|
|
|
|
return (tuple (r))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
2019-01-15 23:02:35 +01:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
class Sensor ():
|
|
|
|
|
def __init__ (self, id, get_or_autologin):
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.id = id
|
|
|
|
|
self.sensor_dict = None
|
2020-09-27 16:52:56 +02:00
|
|
|
|
self.get_or_autologin = get_or_autologin
|
2016-05-25 17:19:16 +02:00
|
|
|
|
|
|
|
|
|
def refresh (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Mets à jour les données du capteur
|
2016-05-25 17:19:16 +02:00
|
|
|
|
'''
|
2016-05-26 16:57:48 +02:00
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
r = base_url + sensors_list
|
2016-05-25 17:19:16 +02:00
|
|
|
|
self.sensor_dict = None
|
2020-09-27 16:52:56 +02:00
|
|
|
|
for i in get_xml_tree (bytes2file (self.get_or_autologin (r).read ())) [2]:
|
|
|
|
|
if i [0] == sensors_label and i [1] [sensors_label_id] == self.id:
|
|
|
|
|
self.sensor_dict = i [2]
|
|
|
|
|
break
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_raw (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne les données brutes du capteur
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
|
|
|
|
return (self.sensor_dict)
|
|
|
|
|
|
2020-09-27 16:52:56 +02:00
|
|
|
|
def get_value (self, lst, key):
|
|
|
|
|
for i in lst:
|
|
|
|
|
if i [0] == key:
|
|
|
|
|
return (i [2])
|
|
|
|
|
raise KeyError ()
|
|
|
|
|
|
2016-05-25 17:19:16 +02:00
|
|
|
|
def get_mac (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne l'adresse matérielle du capteur, s'il en a une
|
2016-05-25 17:19:16 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_mac_field))
|
2016-05-25 17:19:16 +02:00
|
|
|
|
|
2016-05-22 22:57:39 +02:00
|
|
|
|
def get_type (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le type du capteur
|
|
|
|
|
Les types sont ceux définis dans les constantes
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_type_field))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_model (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le modèle du capteur
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_model_field))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_version (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne la version du capteur
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_version_field))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_name (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le nom du capteur
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_name_field))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_longname (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne un nom long du capteur composé de son type en français et de son nom
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_longname_field))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_namegender (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le genre du nom du type de capteur en français
|
|
|
|
|
M pour masculin et F pour féminin
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_namegender_field))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_batterylevel (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le niveau de batterie sur 10
|
|
|
|
|
Toute autre valeur doit être considérée comme venant d'un capteur n'ayant pas de batterie
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (int (self.get_value (self.sensor_dict, sensors_batterylevel_field)))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_signal (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne le niveau de signal sur 10
|
|
|
|
|
Tout autre valeur est pour un capteur connecté par câble
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (int (self.get_value (self.sensor_dict, sensors_signal_field)))
|
2016-05-22 22:57:39 +02:00
|
|
|
|
|
|
|
|
|
def get_status (self):
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne True si le capteur est considéré comme opérationnel par le système
|
2016-05-22 22:57:39 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.get_value (self.sensor_dict, sensors_status_field) == sensors_status_value_ok)
|
2016-05-25 17:19:16 +02:00
|
|
|
|
|
2020-09-26 07:51:52 +02:00
|
|
|
|
def get_camera_snapshot (self):
|
2016-05-25 17:19:16 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
Retourne une capture de la caméra dans un objet PIL.Image
|
2016-05-25 17:19:16 +02:00
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
r = base_url + camera_snapshot + '&' + camera_snapshot_mac + '=' + self.get_mac ()
|
|
|
|
|
a = bytes2image (self.get_or_autologin (r).read ())
|
2020-09-26 07:51:52 +02:00
|
|
|
|
return (a)
|
|
|
|
|
|
|
|
|
|
def get_camera_petmode (self):
|
|
|
|
|
'''
|
|
|
|
|
Gets the pet mode value
|
|
|
|
|
Pet mode is a setting on movement sensibility, to avoid trigger on pet movements
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.sensor_dict [camera_get_config_petmode] == '1')
|
2020-09-26 07:51:52 +02:00
|
|
|
|
|
|
|
|
|
def get_camera_recording (self):
|
|
|
|
|
'''
|
|
|
|
|
Gets if the camera records or not
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.sensor_dict [camera_get_config_recording] == '1')
|
2020-09-26 07:51:52 +02:00
|
|
|
|
|
|
|
|
|
def get_camera_privacy (self):
|
|
|
|
|
'''
|
|
|
|
|
Gets if the camera records or not
|
|
|
|
|
'''
|
2020-09-27 16:52:56 +02:00
|
|
|
|
return (self.sensor_dict [camera_get_config_privacy] == '1')
|