Ok, I finally have got it working by using the AppDaemon add-on.
This is my app configuration:
get_watts_vision_status:
module: get_watts_vision_status
class: GetWattsVisionStatus
username: !secret watts_vision_username
password: !secret watts_vision_password
client_id: !secret watts_vision_client_id
url: !secret watts_vision_url
smarthome_id: !secret watts_vision_smarthome_id
And this is the Python script I have made to set the value of the template sensors every two minutes:
import appdaemon.plugins.hass.hassapi as hass
import requests
class GetWattsVisionStatus(hass.Hass):
def initialize(self):
self.run_every(self.fetch_token, "now", 120)
def fetch_token(self, kwargs):
username = self.args['username']
password = self.args['password']
client_id = self.args['client_id']
url = '{}/auth/realms/watts/protocol/openid-connect/token'.format(self.args['url'])
payload = { 'grant_type': 'password', 'username': username, 'password': password, 'client_id': client_id }
request = requests.post(url = url, data = payload)
request_status_code = request.status_code
if request_status_code == 200:
access_token = request.json()['access_token']
self.get_status(access_token = access_token)
else:
self.log('Fetching access token was unsuccessful.')
def get_status(self, access_token):
smarthome_id = self.args['smarthome_id']
url = '{}/api/v0.1/human/smarthome/read'.format(self.args['url'])
headers = { 'Authorization': 'Bearer {}'.format(access_token) }
payload = { 'token': 'true', 'smarthome_id': smarthome_id, 'lang': 'nl_NL' }
request = requests.post(url = url, headers = headers, data = payload)
request_status_code = request.status_code
if request_status_code == 200:
result = request.json()
code = result['code']['code']
if code == '1':
data_zones = result['data']['zones']
self.set_temperature(zone= '0', helper = 'temperature_living_room', data = data_zones)
self.set_temperature(zone= '1', helper = 'temperature_study_room', data = data_zones)
self.set_temperature(zone= '2', helper = 'temperature_guest_room', data = data_zones)
self.set_temperature(zone= '3', helper = 'temperature_bath_room', data = data_zones)
self.set_temperature(zone= '4', helper = 'temperature_bed_room', data = data_zones)
self.set_temperature(zone= '5', helper = 'temperature_attic_room', data = data_zones)
self.set_temperature(zone= '6', helper = 'temperature_laundry_room', data = data_zones)
self.set_heating_status(zone= '0', sensor = 'living_room_heating_up', data = data_zones)
self.set_heating_status(zone= '1', sensor = 'study_room_heating_up', data = data_zones)
self.set_heating_status(zone= '2', sensor = 'guest_room_heating_up', data = data_zones)
self.set_heating_status(zone= '3', sensor = 'bath_room_heating_up', data = data_zones)
self.set_heating_status(zone= '4', sensor = 'bed_room_heating_up', data = data_zones)
self.set_heating_status(zone= '5', sensor = 'attic_room_heating_up', data = data_zones)
self.set_heating_status(zone= '6', sensor = 'laundry_room_heating_up', data = data_zones)
else:
error_message = result['code']['value']
self.log('Reading temperature was unsuccessful: {}'.format(error_message))
else:
self.log('Reading temperature was unsuccessful.')
def set_temperature(self, zone, helper, data):
temperature_in_fahrenheit = data[zone]['devices']['0']['temperature_air']
temperature = self.calculate_fahrenheit_to_celsius_degrees(temperature_in_fahrenheit)
self.set_value('input_number.{}'.format(helper), temperature)
gv_mode = data[zone]['devices']['0']['gv_mode']
if gv_mode == '11':
set_temperature_in_fahrenheit = data[zone]['devices']['0']['consigne_eco']
else:
set_temperature_in_fahrenheit = data[zone]['devices']['0']['consigne_confort']
set_temperature = self.calculate_fahrenheit_to_celsius_degrees(set_temperature_in_fahrenheit)
self.set_value('input_number.set_{}'.format(helper), round(set_temperature * 2) / 2)
def calculate_fahrenheit_to_celsius_degrees(self, temperature_in_fahrenheit):
return round((float (temperature_in_fahrenheit) / 10 - 32) / 1.8, 2)
def set_heating_status(self, zone, sensor, data):
heating_up = data[zone]['devices']['0']['heating_up']
self.set_state('binary_sensor.{}'.format(sensor), state = 'on' if heating_up == '1' else 'off')
I used this document to set it up: AppDaemon Tutorial for HASS Users — AppDaemon 4.1.0 documentation
Please feel free to contact me if you have any questions.