RobertD502 / home-assistant-flair

Custom component for Home Assistant Core for Flair pucks, vents, rooms, structures, and minisplits
MIT License
90 stars 12 forks source link

Implement power option / cool / heat #5

Closed prestonr83 closed 2 years ago

prestonr83 commented 3 years ago

Currently you cannot set cool/heat or power for puck. I know you mentioned the API doesn't expose that but perhaps you could implement with rest calls. I haven't coded much in python in a long time and I'm not familiar with the HA scaffolding for integrations.

I do know the rest calls work as I have hacked a switch together so I can power off my AC with automation but would be great to have it seamless with this integration.

Example

import requests
import json

CLIENT_ID = ''
CLIENT_SECRET = ''
ROOM_ID = ''

def get_token():
    r = requests.post('https://api.flair.co/oauth/token?client_id={}&client_secret={}&scope=thermostats.view+structures.view+structures.edit+pucks.view&grant_type=client_credentials'.format(CLIENT_ID, CLIENT_SECRET))
    token = r.json() 
    return token['access_token']

HEADERS = { 'Authorization': 'Bearer {}'.format(get_token())}

def set_power(room_id, set_power):
    data = {
        "data": {
        "type": "rooms",
        "attributes": {
            "active": set_power
        },
        "relationships": {}
        }
    }
    r = requests.patch('https://api.flair.co/api/rooms/{}'.format(ROOM_ID), headers=HEADERS, data=json.dumps(data))

set_power(ROOM_ID, false) #Power off
set_power(ROOM_ID, true) #Power on
RobertD502 commented 3 years ago

By setting cool/heat are you referring to the "Mode" in the Flair app? If so, this is done by sending a PATCH request to a given "Structure" at the /api/structures/structure_id endpoint with: {"data":{"type":"structures","attributes":{"structure-heat-cool-mode":"heat"}}} ---> this can be heat, cool, auto, or float (float corresponds to off).

Just a heads up, switching the mode would only work if within the app you set the Set Point Controller to "Flair App" ( This can be found under Home Settings --> System Settings --> Set Point Controller).

Regarding the power - your code is setting the room to inactive/active and not the puck. If there is an option to specifically set the puck on/off in the Flair app then that endpoint can be obtained, but I don't see that as an available option.

I will add your request as a milestone for the future. As of now I'm planning to rewrite the base library for it to be asynchronous followed by a rewrite of the Flair Home Assistant integration. I will add this functionality during the rewrite, but recommend using your method in the meantime.

RobertD502 commented 2 years ago

@prestonr83 The ability to change structure mode has been introduced in 0.0.5.0. That should take care of what you're trying to do.