refactor(sensors): create common rig sensor entity

This commit is contained in:
Brian Berg 2020-07-12 20:25:50 +00:00
parent ecea93959c
commit f332fa3bf5
2 changed files with 65 additions and 102 deletions

View File

@ -13,6 +13,7 @@ ISSUE_URL = "https://github.com/brianberg/ha-nicehash/issues"
ICON_CURRENCY_BTC = "mdi:currency-btc"
ICON_CURRENCY_EUR = "mdi:currency-eur"
ICON_CURRENCY_USD = "mdi:currency-usd"
ICON_EXCAVATOR = "mdi:excavator"
ICON_MEMORY = "mdi:memory"
ICON_PICKAXE = "mdi:pickaxe"
ICON_PULSE = "mdi:pulse"

View File

@ -12,6 +12,7 @@ from .const import (
DEVICE_STATUS_UNKNOWN,
FORMAT_DATETIME,
ICON_CURRENCY_BTC,
ICON_EXCAVATOR,
ICON_PULSE,
ICON_THERMOMETER,
NICEHASH_ATTRIBUTION,
@ -22,9 +23,9 @@ from .nicehash import MiningRig
_LOGGER = logging.getLogger(__name__)
class RigTemperatureSensor(Entity):
class RigSensor(Entity):
"""
Displays highest temperature of active mining rig devices
Mining rig sensor
"""
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig: MiningRig):
@ -32,12 +33,54 @@ class RigTemperatureSensor(Entity):
self.coordinator = coordinator
self._rig_id = rig.id
self._rig_name = rig.name
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 self._rig_name
@property
def icon(self):
"""Sensor icon"""
return ICON_EXCAVATOR
@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
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()
def _get_rig(self):
try:
mining_rigs = self.coordinator.data.get("miningRigs")
return MiningRig(mining_rigs.get(self._rig_id))
except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id})\n{e}")
class RigTemperatureSensor(RigSensor):
"""
Displays highest temperature of active mining rig devices
"""
_temps = []
_num_devices = 0
_highest_temp = 0
@property
def name(self):
"""Sensor name"""
@ -48,28 +91,15 @@ class RigTemperatureSensor(Entity):
"""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:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
rig = self._get_rig()
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}")
return self._highest_temp
@ -94,30 +124,14 @@ class RigTemperatureSensor(Entity):
"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()
class RigStatusSensor(Entity):
class RigStatusSensor(RigSensor):
"""
Displays status of a mining rig
"""
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig: MiningRig):
"""Initialize the sensor"""
self.coordinator = coordinator
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})")
_status = DEVICE_STATUS_UNKNOWN
_status_time = None
@property
def name(self):
@ -129,27 +143,15 @@ class RigStatusSensor(Entity):
"""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:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
rig = self._get_rig()
if rig:
status = rig.status
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}")
else:
status = DEVICE_STATUS_UNKNOWN
self._status_time = None
status = DEVICE_STATUS_UNKNOWN
@ -179,32 +181,14 @@ class RigStatusSensor(Entity):
"status_time": status_time,
}
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()
class RigProfitabilitySensor(Entity):
class RigProfitabilitySensor(RigSensor):
"""
Displays profitability of a mining rig
"""
def __init__(self, coordinator: MiningRigsDataUpdateCoordinator, rig: MiningRig):
"""Initialize the sensor"""
self.coordinator = coordinator
self._rig_id = rig.id
self._rig_name = rig.name
self._profitability = 0
self._unpaid_amount = 0
_LOGGER.debug(
f"Mining Rig Profitability Sensor: {self._rig_name} ({self._rig_id})"
)
_profitability = 0
_unpaid_amount = 0
@property
def name(self):
@ -216,26 +200,14 @@ class RigProfitabilitySensor(Entity):
"""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:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
rig = self._get_rig()
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}")
else:
self._profitability = 0
self._unpaid_amount = 0
@ -259,13 +231,3 @@ class RigProfitabilitySensor(Entity):
"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()