refactor(sensor): add classes
- add mining rig and device classes
This commit is contained in:
parent
743fd0febf
commit
4438bd3d6a
@ -19,6 +19,7 @@ from .const import (
|
||||
ICON_SPEEDOMETER,
|
||||
NICEHASH_ATTRIBUTION,
|
||||
)
|
||||
from .nicehash import MiningRig, MiningRigDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -94,32 +95,27 @@ class DeviceSensor(Entity):
|
||||
await self.coordinator.async_request_refresh()
|
||||
mining_rigs = self.coordinator.data.get("miningRigs")
|
||||
try:
|
||||
rig_data = mining_rigs.get(self._rig_id)
|
||||
devices = rig_data.get("devices")
|
||||
for device in devices:
|
||||
if device.get("id") == self._device_id:
|
||||
algorithms = device.get("speeds")
|
||||
rig = MiningRig(mining_rigs.get(self._rig_id))
|
||||
if rig:
|
||||
device = rig.devices.get(self._device_id)
|
||||
if device:
|
||||
self._status = device.status
|
||||
self._load = device.load
|
||||
self._rpm = device.rpm
|
||||
self._temperature = device.temperature
|
||||
algorithms = device.speeds
|
||||
if len(algorithms) > 0:
|
||||
status = device.get("status")
|
||||
self._status = status.get("description")
|
||||
algorithm = algorithms[0]
|
||||
self._load = float(device.get("load"))
|
||||
self._rpm = float(device.get("revolutionsPerMinute"))
|
||||
self._algorithm = algorithm.get("title")
|
||||
self._speed = float(algorithm.get("speed"))
|
||||
self._speed_title = algorithm.get("title")
|
||||
self._speed_unit = algorithm.get("displaySuffix")
|
||||
self._temperature = int(device.get("temperature"))
|
||||
else:
|
||||
self._status = DEVICE_STATUS_UNKNOWN
|
||||
self._load = 0
|
||||
self._rpm = 0
|
||||
self._speed = 0
|
||||
self._speed_title = "Unknown"
|
||||
self._speed_unit = "MH"
|
||||
self._algorithm = None
|
||||
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._load = 0
|
||||
self._rpm = 0
|
||||
|
@ -17,6 +17,35 @@ import uuid
|
||||
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:
|
||||
async def get_exchange_rates(self):
|
||||
exchange_data = await self.request("GET", "/main/api/v2/exchangeRate/list")
|
||||
|
@ -16,6 +16,7 @@ from .const import (
|
||||
ICON_THERMOMETER,
|
||||
NICEHASH_ATTRIBUTION,
|
||||
)
|
||||
from .nicehash import MiningRig
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -62,19 +63,11 @@ class RigTemperatureSensor(Entity):
|
||||
mining_rigs = self.coordinator.data.get("miningRigs")
|
||||
self._highest_temp = 0
|
||||
try:
|
||||
rig_data = mining_rigs.get(self._rig_id)
|
||||
devices = rig_data.get("devices")
|
||||
self._temps = []
|
||||
self._num_devices = len(devices)
|
||||
|
||||
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
|
||||
rig = MiningRig(mining_rigs.get(self._rig_id))
|
||||
if rig:
|
||||
self._temps = rig.temperatures
|
||||
self._num_devices = rig.num_devices
|
||||
self._highest_temp = max(rig.temperatures)
|
||||
except Exception as 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")
|
||||
status = DEVICE_STATUS_UNKNOWN
|
||||
try:
|
||||
rig_data = mining_rigs.get(self._rig_id)
|
||||
devices = rig_data.get("devices")
|
||||
status = rig_data.get("minerStatus")
|
||||
status_time_ms = int(rig_data.get("statusTime"))
|
||||
self._num_devices = len(devices)
|
||||
self._status_time = datetime.fromtimestamp(status_time_ms / 1000.0)
|
||||
rig = MiningRig(mining_rigs.get(self._rig_id))
|
||||
if rig:
|
||||
status = rig.status
|
||||
self._num_devices = rig.num_devices
|
||||
self._status_time = datetime.fromtimestamp(rig.status_time / 1000.0)
|
||||
except Exception as e:
|
||||
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
|
||||
self._status_time = None
|
||||
@ -181,10 +173,14 @@ class RigStatusSensor(Entity):
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Sensor device state attributes"""
|
||||
status_time = None
|
||||
if self._status_time:
|
||||
status_time = self._status_time.strftime(FORMAT_DATETIME)
|
||||
|
||||
return {
|
||||
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
|
||||
"status": self._status,
|
||||
"status_time": self._status_time.strftime(FORMAT_DATETIME),
|
||||
"status_time": status_time,
|
||||
"total_devices": self._num_devices,
|
||||
}
|
||||
|
||||
@ -240,9 +236,10 @@ class RigProfitabilitySensor(Entity):
|
||||
"""Sensor state"""
|
||||
mining_rigs = self.coordinator.data.get("miningRigs")
|
||||
try:
|
||||
rig_data = mining_rigs.get(self._rig_id)
|
||||
self._profitability = rig_data.get("profitability")
|
||||
self._unpaid_amount = rig_data.get("unpaidAmount")
|
||||
rig = MiningRig(mining_rigs.get(self._rig_id))
|
||||
if rig:
|
||||
self._profitability = rig.profitability
|
||||
self._unpaid_amount = rig.unpaid_amount
|
||||
except Exception as e:
|
||||
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
|
||||
self._profitability = 0
|
||||
|
@ -144,8 +144,7 @@ def create_device_sensors(mining_rigs, coordinator):
|
||||
device_sensors = []
|
||||
for rig in mining_rigs:
|
||||
devices = rig.get("devices")
|
||||
for i in range(len(devices)):
|
||||
device = devices[i]
|
||||
for device in devices:
|
||||
device_sensors.append(DeviceAlgorithmSensor(coordinator, rig, device))
|
||||
device_sensors.append(DeviceSpeedSensor(coordinator, rig, device))
|
||||
device_sensors.append(DeviceStatusSensor(coordinator, rig, device))
|
||||
|
Loading…
Reference in New Issue
Block a user