feat(sensor): simpler device names

- refactor sensor params
- simpifiy rig/device id attributes
This commit is contained in:
Brian Berg 2020-07-12 20:20:43 +00:00
parent ea5816ea20
commit ecea93959c
4 changed files with 44 additions and 26 deletions

View File

@ -33,15 +33,15 @@ class DeviceSensor(Entity):
def __init__(
self,
coordinator: MiningRigsDataUpdateCoordinator,
rig_data: dict,
device_data: dict,
rig: MiningRig,
device: MiningRigDevice,
):
"""Initialize the sensor"""
self.coordinator = coordinator
self._rig_id = rig_data.get("rigId")
self._rig_name = rig_data.get("name")
self._device_name = device_data.get("name")
self._device_id = device_data.get("id")
self._rig_id = rig.id
self._rig_name = rig.name
self._device_id = device.id
self._device_name = device.name
@property
def name(self):

View File

@ -10,6 +10,7 @@ from hashlib import sha256
import hmac
import httpx
import json
import re
import sys
from time import mktime
import uuid
@ -17,10 +18,21 @@ import uuid
from .const import NICEHASH_API_URL
def parse_device_name(raw_name):
name = re.sub(
r"(\s?\(r\))|(\s?\(tm\))|(\s?cpu)|(\s?graphics)|(\s?@.*ghz)",
"",
raw_name,
flags=re.IGNORECASE,
)
return name
class MiningRigDevice:
def __init__(self, data: dict):
self.device_id = data.get("id")
self.name = data.get("name")
self.id = data.get("id")
self.name = parse_device_name(data.get("name"))
self.status = data.get("status").get("description")
self.temperature = int(data.get("temperature"))
self.load = float(data.get("load"))
@ -30,7 +42,7 @@ class MiningRigDevice:
class MiningRig:
def __init__(self, data: dict):
self.rig_id = data.get("rig_id")
self.id = data.get("rigId")
self.name = data.get("name")
self.status = data.get("minerStatus")
self.status_time = data.get("statusTime")
@ -40,9 +52,9 @@ class MiningRig:
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
for device_data in devices:
device = MiningRigDevice(device_data)
self.devices[f"{device.id}"] = device
self.temperatures.append(device.temperature)

View File

@ -27,11 +27,11 @@ class RigTemperatureSensor(Entity):
Displays highest temperature of active mining rig devices
"""
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig_data: dict):
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig: MiningRig):
"""Initialize the sensor"""
self.coordinator = coordinator
self._rig_id = rig_data["rigId"]
self._rig_name = rig_data["name"]
self._rig_id = rig.id
self._rig_name = rig.name
self._temps = []
self._num_devices = 0
_LOGGER.debug(
@ -110,11 +110,11 @@ class RigStatusSensor(Entity):
Displays status of a mining rig
"""
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig_data: dict):
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig: MiningRig):
"""Initialize the sensor"""
self.coordinator = coordinator
self._rig_id = rig_data["rigId"]
self._rig_name = rig_data["name"]
self._rig_id = rig.id
self._rig_name = rig.name
self._status = DEVICE_STATUS_UNKNOWN
self._status_time = None
_LOGGER.debug(f"Mining Rig Status Sensor: {self._rig_name} ({self._rig_id})")
@ -195,11 +195,11 @@ class RigProfitabilitySensor(Entity):
Displays profitability of a mining rig
"""
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig_data: dict):
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig: MiningRig):
"""Initialize the sensor"""
self.coordinator = coordinator
self._rig_id = rig_data["rigId"]
self._rig_name = rig_data["name"]
self._rig_id = rig.id
self._rig_name = rig.name
self._profitability = 0
self._unpaid_amount = 0
_LOGGER.debug(

View File

@ -21,7 +21,12 @@ from .const import (
DEVICE_SPEED_RATE,
DEVICE_SPEED_ALGORITHM,
)
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
from .nicehash import (
MiningRig,
MiningRigDevice,
NiceHashPrivateClient,
NiceHashPublicClient,
)
from .account_sensors import BalanceSensor
from .payout_sensors import RecentMiningPayoutSensor
from .rig_sensors import (
@ -146,7 +151,8 @@ def create_payout_sensors(organization_id, coordinator):
def create_rig_sensors(mining_rigs, coordinator):
rig_sensors = []
for rig in mining_rigs:
for rig_data in mining_rigs:
rig = MiningRig(rig_data)
rig_sensors.append(RigStatusSensor(coordinator, rig))
rig_sensors.append(RigTemperatureSensor(coordinator, rig))
rig_sensors.append(RigProfitabilitySensor(coordinator, rig))
@ -156,9 +162,9 @@ def create_rig_sensors(mining_rigs, coordinator):
def create_device_sensors(mining_rigs, coordinator):
device_sensors = []
for rig in mining_rigs:
devices = rig.get("devices")
for device in devices:
for rig_data in mining_rigs:
rig = MiningRig(rig_data)
for device in rig.devices.values():
device_sensors.append(DeviceAlgorithmSensor(coordinator, rig, device))
device_sensors.append(DeviceSpeedSensor(coordinator, rig, device))
device_sensors.append(DeviceStatusSensor(coordinator, rig, device))