Skip to content

Instantly share code, notes, and snippets.

@Gronis
Last active October 5, 2021 23:15
Show Gist options
  • Save Gronis/a2397eeb5aba7ea0da8d0be7577b55e9 to your computer and use it in GitHub Desktop.
Save Gronis/a2397eeb5aba7ea0da8d0be7577b55e9 to your computer and use it in GitHub Desktop.
Home assistant xmrig config
""" 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

Control the number of mining cores on xmrig from home assistant. The script calls the http interface on xmrig and sets the number of cores used for mining with cn-pico algorithm. Feel free to edit to your usecase.

Put __init__.py file in <homeassistant_root>/custom_components/xmrig/__init__.py

Then add the following to your configuration.yaml

xmrig:
  host: <host>
  port: <port>
  token: <token>

You can then set the number of cores with home assisant service call:

service: xmrig.set_core_count
data:
  cores: <n>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment