molobrakos / volvooncall

Retrieve information from the Volvo On Call (VOC) web service
The Unlicense
158 stars 51 forks source link

Great! #1

Closed nikagl closed 7 years ago

nikagl commented 7 years ago

Did you also find how to turn on the heater? In the following thread they do it with a POST of /heater/start, but I couldn't get it to work yet...

https://community.openhab.org/t/help-with-sending-command/7046/9

Regards,

Nika.

molobrakos commented 7 years ago

Interesting, let me know if you get it to work (or submit a patch)!

nikagl commented 7 years ago

I figured it out. In my case it's not a heater but preclimatization and I needed to add some headers for the post to complete.

I don't know how to submit a patch and such, still really bad at github. Btw, I have no clue what I'm doing with Python either, I simply used some bits of your code and... it worked :).

Here's the updated code: `#!/usr/bin/env python

-- coding: utf-8 --

""" Retrieve information from VOC """

import logging from datetime import timedelta import requests try: from urlparse import urljoin except ImportError: from urllib.parse import urljoin

_LOGGER = logging.getLogger(name)

SERVICE_URL = 'https://vocapi.wirelesscar.net/customerapi/rest/v3.0/' HEADERS = {"X-Device-Id": "Device", "X-OS-Type": "Android", "X-Originator-Type": "App", "Content-Type": "application/json", "X-OS-Version": "19"}

TIMEOUT = timedelta(seconds=5)

class Connection(): """Connection to the VOC server."""

def __init__(self, username, password):
    """Initialize."""
    self._session = requests.Session()
    self._session.headers.update(HEADERS)
    self._session.auth = (username,
                          password)
    self._state = None

def getquery(self, ref, rel=SERVICE_URL):
    """Perform a query to the online service."""
    url = urljoin(rel, ref)
    _LOGGER.debug("Request for %s", url)
    res = self._session.get(url, timeout=TIMEOUT.seconds)
    res.raise_for_status()
    _LOGGER.debug("Received %s", res.json())
    return res.json()

def postquery(self, ref, rel=SERVICE_URL):
    """Perform a query to the online service."""
    url = urljoin(rel, ref)
    _LOGGER.debug("Post for %s", url)
    res = self._session.post(url, "{}")
    res.raise_for_status()
    _LOGGER.debug("Received %s", res.json())
    return res.json()

def update(self, reset=False):
    """Update status."""
    try:
        _LOGGER.info("Updating1")
        if not self._state or reset:
            _LOGGER.info("Querying vehicles")
            user = self.getquery("customeraccounts")
            self._state = {}
            for vehicle in user["accountVehicleRelations"]:
                rel = self.getquery(vehicle)
                vehicle = rel["vehicle"] + '/'
                state = self.getquery("attributes", vehicle)
                self._state.update({vehicle: state})
        _LOGGER.debug("Updating2")
        for vehicle in self._state:
            status = self.getquery("status", vehicle)
            position = self.getquery("position", vehicle)
            heater = self.postquery("preclimatization/start", vehicle)
            vehicle = self._state[vehicle]
            vehicle.update(status)
            vehicle.update(position)
            vehicle.update(heater)
        _LOGGER.debug("State: %s", self._state)
        return True, self._state.values()
    except requests.exceptions.RequestException as error:
        _LOGGER.error("Could not query server: %s", error)
        return False, self._state.values()

if name == "main": from os import path from sys import argv from pprint import pprint

logging.basicConfig(level=logging.CRITICAL)

logging.basicConfig(level=logging.ERROR)

logging.basicConfig(level=logging.WARNING)

logging.basicConfig(level=logging.INFO)

logging.basicConfig(level=logging.DEBUG)

logging.basicConfig(level=logging.NOTSET)

def credentials():
    """Return user, pass."""
    if len(argv) == 3:
        return argv[1:]
    try:
        from yaml import safe_load as load_yaml
        with open(path.join(path.dirname(argv[0]),
                            ".credentials.yaml")) as config:
            config = load_yaml(config)
            return config["username"], config["password"]
    except ImportError:
        _LOGGER.error("Incorrect parameters. \nUsage: volvooncall.py <username> <password>")
        exit(-1)

res, value = Connection(*credentials()).update()
if res:
    pprint(list(value))

`

Regards,

Nika.

nikagl commented 7 years ago

Uh, that worked out fine... NOT :). I'll try and see what I can do with my github account, pulling, etc.

nikagl commented 7 years ago

Will close the issue now, you can hopefully use my pull request :)