2020-07-09 02:05:11 +02:00
|
|
|
"""
|
|
|
|
NiceHash Rig Sensors
|
|
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CURRENCY_BTC,
|
|
|
|
DEVICE_STATUS_UNKNOWN,
|
|
|
|
FORMAT_DATETIME,
|
|
|
|
ICON_CURRENCY_BTC,
|
|
|
|
ICON_PULSE,
|
|
|
|
ICON_THERMOMETER,
|
|
|
|
NICEHASH_ATTRIBUTION,
|
|
|
|
)
|
2020-07-11 20:56:56 +02:00
|
|
|
from .coordinators import MiningRigsDataUpdateCoordinator
|
2020-07-09 04:20:55 +02:00
|
|
|
from .nicehash import MiningRig
|
2020-07-09 02:05:11 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-07-09 02:31:02 +02:00
|
|
|
class RigTemperatureSensor(Entity):
|
2020-07-09 02:05:11 +02:00
|
|
|
"""
|
|
|
|
Displays highest temperature of active mining rig devices
|
|
|
|
"""
|
|
|
|
|
2020-07-11 18:35:27 +02:00
|
|
|
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig_data: dict):
|
2020-07-09 02:05:11 +02:00
|
|
|
"""Initialize the sensor"""
|
|
|
|
self.coordinator = coordinator
|
2020-07-11 18:35:27 +02:00
|
|
|
self._rig_id = rig_data["rigId"]
|
|
|
|
self._rig_name = rig_data["name"]
|
2020-07-09 02:05:11 +02:00
|
|
|
self._temps = []
|
|
|
|
self._num_devices = 0
|
|
|
|
_LOGGER.debug(
|
|
|
|
f"Mining Rig Temperature Sensor: {self._rig_name} ({self._rig_id})"
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Sensor name"""
|
|
|
|
return f"{self._rig_name} Temperature"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Unique entity id"""
|
|
|
|
return f"{self._rig_id}:temperature"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No need to poll, Coordinator notifies entity of updates"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Whether sensor is available"""
|
|
|
|
return self.coordinator.last_update_success
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Sensor state"""
|
|
|
|
self._highest_temp = 0
|
|
|
|
try:
|
2020-07-11 20:41:22 +02:00
|
|
|
mining_rigs = self.coordinator.data.get("miningRigs")
|
2020-07-09 04:20:55 +02:00
|
|
|
rig = MiningRig(mining_rigs.get(self._rig_id))
|
2020-07-11 20:41:22 +02:00
|
|
|
self._temps = rig.temperatures
|
|
|
|
self._num_devices = rig.num_devices
|
|
|
|
self._highest_temp = max(rig.temperatures)
|
2020-07-09 02:05:11 +02:00
|
|
|
except Exception as e:
|
|
|
|
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) temperature\n{e}")
|
|
|
|
|
|
|
|
return self._highest_temp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Sensor icon"""
|
|
|
|
return ICON_THERMOMETER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Sensor unit of measurement"""
|
|
|
|
# Not Celsius because then HA might convert to Fahrenheit
|
|
|
|
return "C"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Sensor device state attributes"""
|
|
|
|
return {
|
|
|
|
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
|
|
|
|
"highest_temperature": self._highest_temp,
|
|
|
|
"temperatures": self._temps,
|
|
|
|
"total_devices": self._num_devices,
|
|
|
|
}
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Connect to dispatcher listening for entity data notifications"""
|
|
|
|
self.async_on_remove(
|
|
|
|
self.coordinator.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update entity"""
|
|
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
|
|
|
|
|
2020-07-09 02:31:02 +02:00
|
|
|
class RigStatusSensor(Entity):
|
2020-07-09 02:05:11 +02:00
|
|
|
"""
|
|
|
|
Displays status of a mining rig
|
|
|
|
"""
|
|
|
|
|
2020-07-11 18:35:27 +02:00
|
|
|
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig_data: dict):
|
2020-07-09 02:05:11 +02:00
|
|
|
"""Initialize the sensor"""
|
|
|
|
self.coordinator = coordinator
|
2020-07-11 18:35:27 +02:00
|
|
|
self._rig_id = rig_data["rigId"]
|
|
|
|
self._rig_name = rig_data["name"]
|
2020-07-09 02:05:11 +02:00
|
|
|
self._status = DEVICE_STATUS_UNKNOWN
|
|
|
|
self._status_time = None
|
|
|
|
_LOGGER.debug(f"Mining Rig Status Sensor: {self._rig_name} ({self._rig_id})")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Sensor name"""
|
|
|
|
return f"{self._rig_name} Status"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Unique entity id"""
|
|
|
|
return f"{self._rig_id}:status"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No need to poll, Coordinator notifies entity of updates"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Whether sensor is available"""
|
|
|
|
return self.coordinator.last_update_success
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Sensor state"""
|
|
|
|
status = DEVICE_STATUS_UNKNOWN
|
|
|
|
try:
|
2020-07-11 20:41:22 +02:00
|
|
|
mining_rigs = self.coordinator.data.get("miningRigs")
|
2020-07-09 04:20:55 +02:00
|
|
|
rig = MiningRig(mining_rigs.get(self._rig_id))
|
2020-07-11 20:41:22 +02:00
|
|
|
status = rig.status
|
|
|
|
self._status_time = datetime.fromtimestamp(rig.status_time / 1000.0)
|
2020-07-09 02:05:11 +02:00
|
|
|
except Exception as e:
|
|
|
|
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
|
|
|
|
self._status_time = None
|
|
|
|
status = DEVICE_STATUS_UNKNOWN
|
|
|
|
|
|
|
|
self._status = status[0].upper() + status.lower()[1:]
|
|
|
|
return self._status
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Sensor icon"""
|
|
|
|
return ICON_PULSE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Sensor unit of measurement"""
|
2020-07-11 20:57:54 +02:00
|
|
|
return None
|
2020-07-09 02:05:11 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Sensor device state attributes"""
|
2020-07-09 04:20:55 +02:00
|
|
|
status_time = None
|
|
|
|
if self._status_time:
|
|
|
|
status_time = self._status_time.strftime(FORMAT_DATETIME)
|
|
|
|
|
2020-07-09 02:05:11 +02:00
|
|
|
return {
|
|
|
|
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
|
|
|
|
"status": self._status,
|
2020-07-09 04:20:55 +02:00
|
|
|
"status_time": status_time,
|
2020-07-09 02:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Connect to dispatcher listening for entity data notifications"""
|
|
|
|
self.async_on_remove(
|
|
|
|
self.coordinator.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update entity"""
|
|
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
|
|
|
|
|
2020-07-09 02:31:02 +02:00
|
|
|
class RigProfitabilitySensor(Entity):
|
2020-07-09 02:05:11 +02:00
|
|
|
"""
|
|
|
|
Displays profitability of a mining rig
|
|
|
|
"""
|
|
|
|
|
2020-07-11 18:35:27 +02:00
|
|
|
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig_data: dict):
|
2020-07-09 02:05:11 +02:00
|
|
|
"""Initialize the sensor"""
|
|
|
|
self.coordinator = coordinator
|
2020-07-11 18:35:27 +02:00
|
|
|
self._rig_id = rig_data["rigId"]
|
|
|
|
self._rig_name = rig_data["name"]
|
2020-07-09 02:05:11 +02:00
|
|
|
self._profitability = 0
|
|
|
|
self._unpaid_amount = 0
|
|
|
|
_LOGGER.debug(
|
|
|
|
f"Mining Rig Profitability Sensor: {self._rig_name} ({self._rig_id})"
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Sensor name"""
|
|
|
|
return f"{self._rig_name} Profitability"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Unique entity id"""
|
|
|
|
return f"{self._rig_id}:profitability"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No need to poll, Coordinator notifies entity of updates"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Whether sensor is available"""
|
|
|
|
return self.coordinator.last_update_success
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Sensor state"""
|
|
|
|
try:
|
2020-07-11 20:41:22 +02:00
|
|
|
mining_rigs = self.coordinator.data.get("miningRigs")
|
2020-07-09 04:20:55 +02:00
|
|
|
rig = MiningRig(mining_rigs.get(self._rig_id))
|
2020-07-11 20:41:22 +02:00
|
|
|
self._profitability = rig.profitability
|
|
|
|
self._unpaid_amount = rig.unpaid_amount
|
2020-07-09 02:05:11 +02:00
|
|
|
except Exception as e:
|
|
|
|
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
|
|
|
|
self._profitability = 0
|
|
|
|
self._unpaid_amount = 0
|
|
|
|
|
|
|
|
return self._profitability
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Sensor icon"""
|
|
|
|
return ICON_CURRENCY_BTC
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Sensor unit of measurement"""
|
|
|
|
return CURRENCY_BTC
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Sensor device state attributes"""
|
|
|
|
return {
|
|
|
|
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
|
|
|
|
"profitability": self._profitability,
|
|
|
|
"unpaid_amount": self._unpaid_amount,
|
|
|
|
}
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Connect to dispatcher listening for entity data notifications"""
|
|
|
|
self.async_on_remove(
|
|
|
|
self.coordinator.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update entity"""
|
|
|
|
await self.coordinator.async_request_refresh()
|