refactor(sensor): add classes

- add mining rig and device classes
This commit is contained in:
Brian Berg 2020-07-09 02:20:55 +00:00
parent 743fd0febf
commit 4438bd3d6a
4 changed files with 61 additions and 40 deletions

View File

@ -19,6 +19,7 @@ from .const import (
ICON_SPEEDOMETER, ICON_SPEEDOMETER,
NICEHASH_ATTRIBUTION, NICEHASH_ATTRIBUTION,
) )
from .nicehash import MiningRig, MiningRigDevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -94,32 +95,27 @@ class DeviceSensor(Entity):
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
mining_rigs = self.coordinator.data.get("miningRigs") mining_rigs = self.coordinator.data.get("miningRigs")
try: try:
rig_data = mining_rigs.get(self._rig_id) rig = MiningRig(mining_rigs.get(self._rig_id))
devices = rig_data.get("devices") if rig:
for device in devices: device = rig.devices.get(self._device_id)
if device.get("id") == self._device_id: if device:
algorithms = device.get("speeds") self._status = device.status
self._load = device.load
self._rpm = device.rpm
self._temperature = device.temperature
algorithms = device.speeds
if len(algorithms) > 0: if len(algorithms) > 0:
status = device.get("status")
self._status = status.get("description")
algorithm = algorithms[0] algorithm = algorithms[0]
self._load = float(device.get("load"))
self._rpm = float(device.get("revolutionsPerMinute"))
self._algorithm = algorithm.get("title") self._algorithm = algorithm.get("title")
self._speed = float(algorithm.get("speed")) self._speed = float(algorithm.get("speed"))
self._speed_title = algorithm.get("title") self._speed_title = algorithm.get("title")
self._speed_unit = algorithm.get("displaySuffix") self._speed_unit = algorithm.get("displaySuffix")
self._temperature = int(device.get("temperature"))
else: else:
self._status = DEVICE_STATUS_UNKNOWN
self._load = 0
self._rpm = 0
self._speed = 0
self._speed_title = "Unknown" self._speed_title = "Unknown"
self._speed_unit = "MH" self._speed_unit = "MH"
self._algorithm = None self._algorithm = None
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining device ({self._device_id}) speed\n{e}") _LOGGER.error(f"Unable to get mining device ({self._device_id})\n{e}")
self._status = DEVICE_STATUS_UNKNOWN self._status = DEVICE_STATUS_UNKNOWN
self._load = 0 self._load = 0
self._rpm = 0 self._rpm = 0

View File

@ -17,6 +17,35 @@ import uuid
from .const import NICEHASH_API_URL from .const import NICEHASH_API_URL
class MiningRigDevice:
def __init__(self, data):
self.device_id = data.get("id")
self.name = data.get("name")
self.status = data.get("status").get("description")
self.temperature = int(data.get("temperature"))
self.load = float(data.get("load"))
self.rpm = float(data.get("revolutionsPerMinute"))
self.speeds = data.get("speeds")
class MiningRig:
def __init__(self, data):
self.rig_id = data.get("rig_id")
self.name = data.get("name")
self.status = data.get("minerStatus")
self.status_time = data.get("statusTime")
self.devices = dict()
self.temperatures = []
self.profitability = data.get("profitability")
self.unpaid_amount = data.get("unpaidAmount")
devices = data.get("devices")
self.num_devices = len(devices)
for raw_device in devices:
device = MiningRigDevice(raw_device)
self.devices[f"{device.device_id}"] = device
self.temperatures.append(device.temperature)
class NiceHashPublicClient: class NiceHashPublicClient:
async def get_exchange_rates(self): async def get_exchange_rates(self):
exchange_data = await self.request("GET", "/main/api/v2/exchangeRate/list") exchange_data = await self.request("GET", "/main/api/v2/exchangeRate/list")

View File

@ -16,6 +16,7 @@ from .const import (
ICON_THERMOMETER, ICON_THERMOMETER,
NICEHASH_ATTRIBUTION, NICEHASH_ATTRIBUTION,
) )
from .nicehash import MiningRig
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -62,19 +63,11 @@ class RigTemperatureSensor(Entity):
mining_rigs = self.coordinator.data.get("miningRigs") mining_rigs = self.coordinator.data.get("miningRigs")
self._highest_temp = 0 self._highest_temp = 0
try: try:
rig_data = mining_rigs.get(self._rig_id) rig = MiningRig(mining_rigs.get(self._rig_id))
devices = rig_data.get("devices") if rig:
self._temps = [] self._temps = rig.temperatures
self._num_devices = len(devices) self._num_devices = rig.num_devices
self._highest_temp = max(rig.temperatures)
if self._num_devices > 0:
for device in devices:
temp = int(device.get("temperature"))
self._temps.append(temp)
if temp > self._highest_temp:
self._highest_temp = temp
else:
self._num_devices = 0
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) temperature\n{e}") _LOGGER.error(f"Unable to get mining rig ({self._rig_id}) temperature\n{e}")
@ -154,12 +147,11 @@ class RigStatusSensor(Entity):
mining_rigs = self.coordinator.data.get("miningRigs") mining_rigs = self.coordinator.data.get("miningRigs")
status = DEVICE_STATUS_UNKNOWN status = DEVICE_STATUS_UNKNOWN
try: try:
rig_data = mining_rigs.get(self._rig_id) rig = MiningRig(mining_rigs.get(self._rig_id))
devices = rig_data.get("devices") if rig:
status = rig_data.get("minerStatus") status = rig.status
status_time_ms = int(rig_data.get("statusTime")) self._num_devices = rig.num_devices
self._num_devices = len(devices) self._status_time = datetime.fromtimestamp(rig.status_time / 1000.0)
self._status_time = datetime.fromtimestamp(status_time_ms / 1000.0)
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}") _LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
self._status_time = None self._status_time = None
@ -181,10 +173,14 @@ class RigStatusSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Sensor device state attributes""" """Sensor device state attributes"""
status_time = None
if self._status_time:
status_time = self._status_time.strftime(FORMAT_DATETIME)
return { return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION, ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"status": self._status, "status": self._status,
"status_time": self._status_time.strftime(FORMAT_DATETIME), "status_time": status_time,
"total_devices": self._num_devices, "total_devices": self._num_devices,
} }
@ -240,9 +236,10 @@ class RigProfitabilitySensor(Entity):
"""Sensor state""" """Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs") mining_rigs = self.coordinator.data.get("miningRigs")
try: try:
rig_data = mining_rigs.get(self._rig_id) rig = MiningRig(mining_rigs.get(self._rig_id))
self._profitability = rig_data.get("profitability") if rig:
self._unpaid_amount = rig_data.get("unpaidAmount") self._profitability = rig.profitability
self._unpaid_amount = rig.unpaid_amount
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}") _LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
self._profitability = 0 self._profitability = 0

View File

@ -144,8 +144,7 @@ def create_device_sensors(mining_rigs, coordinator):
device_sensors = [] device_sensors = []
for rig in mining_rigs: for rig in mining_rigs:
devices = rig.get("devices") devices = rig.get("devices")
for i in range(len(devices)): for device in devices:
device = devices[i]
device_sensors.append(DeviceAlgorithmSensor(coordinator, rig, device)) device_sensors.append(DeviceAlgorithmSensor(coordinator, rig, device))
device_sensors.append(DeviceSpeedSensor(coordinator, rig, device)) device_sensors.append(DeviceSpeedSensor(coordinator, rig, device))
device_sensors.append(DeviceStatusSensor(coordinator, rig, device)) device_sensors.append(DeviceStatusSensor(coordinator, rig, device))