Skip to content

Instantly share code, notes, and snippets.

@plenarius
Created January 7, 2018 19:55
Show Gist options
  • Save plenarius/29f1998a4c24d1e39d89ef731f6b16e9 to your computer and use it in GitHub Desktop.
Save plenarius/29f1998a4c24d1e39d89ef731f6b16e9 to your computer and use it in GitHub Desktop.
homeassistant component for cointracking.info API
"""
Balance of your account at cointracking.info
"""
from datetime import timedelta
import requests
import voluptuous as vol
from hashlib import sha512
import hmac
from urllib.parse import urlencode
import time
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_ATTRIBUTION, CONF_NAME)
from homeassistant.helpers.entity import Entity
CONF_API_KEY = 'api_key'
CONF_API_SECRET = 'api_secret'
CONF_ATTRIBUTION = "Data provided by cointrackinginfo"
DEFAULT_NAME = 'cointracking.info Balance'
ICON = 'mdi:currency-usd'
SCAN_INTERVAL = timedelta(minutes=15)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_API_SECRET): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the cointracking.info sensor."""
api_key = config.get(CONF_API_KEY)
api_secret = config.get(CONF_API_SECRET)
name = config.get(CONF_NAME)
add_devices([CoinTrackingInfoSensor(name, api_key, api_secret)], True)
class CoinTrackingInfoSensor(Entity):
"""Representation of a cointracking.info sensor."""
def __init__(self, name, api_key, api_secret):
"""Initialize the sensor."""
self._name = name
self.api_key = api_key
self.api_secret = api_secret
self._state = None
self._unit_of_measurement = 'USD'
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
}
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return ICON
def update(self):
"""Get the latest data and updates the states."""
url = "https://cointracking.info/api/v1/"
secret = bytes(self.api_secret, 'UTF-8')
now = time.time()
params = {'method' : 'getBalance', 'nonce' : now}
encoded_params = urlencode(params)
message = bytes(encoded_params, 'utf-8')
hashed = hmac.new(secret,message,sha512)
signature = hashed.hexdigest()
headers = {'Key': self.api_key, 'Sign': signature}
response = requests.post(url, data=params, headers=headers)
value = float(response.json()['summary']['value_fiat'])
self._state = "{:0.2f}".format(value)
@Nik31192
Copy link

Hi! I would also love to use this one :)
I tried to set it up via "custom_components" and adding my API and secret - but i dont know the steps afterwards. When trying to add something in configuration.yaml file i only get errors.

Would really appreciate it if you could reach out with a short explanation!
Thanks for your help.
Niko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment