|
|
|
""" This is docs for setting the number of cores used for crypto mining with xmrig. """ |
|
|
|
import requests |
|
import asyncio |
|
import json |
|
import voluptuous as vol |
|
import logging |
|
|
|
from homeassistant.const import ( |
|
CONF_HOST, CONF_NAME, CONF_PORT, STATE_OFF, STATE_ON |
|
) |
|
from homeassistant.helpers import config_validation as cv |
|
|
|
client = requests |
|
|
|
DOMAIN = 'xmrig' |
|
CONF_TOKEN = "token" |
|
_LOGGER = logging.getLogger(DOMAIN) |
|
|
|
CONFIG_SCHEMA = vol.Schema({ |
|
DOMAIN: vol.Schema({ |
|
vol.Required(CONF_HOST): cv.string, |
|
vol.Required(CONF_PORT): cv.port, |
|
vol.Optional(CONF_TOKEN, default=None): cv.string, |
|
}) |
|
}, extra=vol.ALLOW_EXTRA) |
|
|
|
|
|
def setup(hass, config): |
|
config = config.get(DOMAIN) |
|
host = config.get(CONF_HOST) |
|
port = config.get(CONF_PORT) |
|
token = config.get(CONF_TOKEN) |
|
_LOGGER.info("Setting up xmrig service " + host) |
|
|
|
def set_core_count(call): |
|
"""Set xmrig cores.""" |
|
#host = "192.168.1.102" |
|
#port = 9988 |
|
#name = "asd" |
|
cores = int(call.data.get("cores", "1")) |
|
set_core_count_(host, port, token, cores) |
|
|
|
hass.services.register(DOMAIN, "set_core_count", set_core_count) |
|
return True |
|
|
|
def set_core_count_(host, port, token, cores): |
|
|
|
url = 'http://' + host + ':' + str(port) + '/1/config' |
|
headers = { |
|
"Authorization":"Bearer " + token, |
|
"content-type": "application/json" |
|
} |
|
r = client.get(url, headers=headers) |
|
|
|
if(r.status_code == 200): |
|
config = json.loads(r.text) |
|
config['cpu']['enabled'] = cores > 0 |
|
config['cpu']['cn-pico'] = [[1,-1]] * cores |
|
body = json.dumps(config, indent=4) |
|
r = client.post(url, headers=headers, data=body) |
|
_LOGGER.info("Setting xmrig config") |
|
return r.status_code >= 200 and r.status_code < 300 |
|
|
|
return False |