feat(sensor): ignore inactive sensor by status

- add data attribution to account and temp sensors
- add active device temp sensor attribute
- rename total device temp sensor attribute
This commit is contained in:
Brian Berg 2020-06-17 01:00:44 +00:00
parent 8456fa4c34
commit da7e12c75d
2 changed files with 20 additions and 9 deletions

View File

@ -51,3 +51,4 @@ CURRENCY_EUR = "EUR"
BALANCE_TYPE_AVAILABLE = "available" BALANCE_TYPE_AVAILABLE = "available"
BALANCE_TYPE_PENDING = "pending" BALANCE_TYPE_PENDING = "pending"
BALANCE_TYPE_TOTAL = "total" BALANCE_TYPE_TOTAL = "total"
DEVICE_STATUS_INACTIVE = "INACTIVE"

View File

@ -7,6 +7,7 @@ import os
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import Config, HomeAssistant from homeassistant.core import Config, HomeAssistant
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle from homeassistant.util import Throttle
@ -19,6 +20,7 @@ from .const import (
CURRENCY_EUR, CURRENCY_EUR,
CURRENCY_USD, CURRENCY_USD,
DEFAULT_NAME, DEFAULT_NAME,
DEVICE_STATUS_INACTIVE,
DOMAIN, DOMAIN,
ICON_CURRENCY_BTC, ICON_CURRENCY_BTC,
ICON_CURRENCY_EUR, ICON_CURRENCY_EUR,
@ -27,6 +29,7 @@ from .const import (
) )
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
ATTRIBUTION = "Data provided by NiceHash"
FORMAT_DATETIME = "%d-%m-%Y %H:%M" FORMAT_DATETIME = "%d-%m-%Y %H:%M"
SCAN_INTERVAL_RIGS = timedelta(minutes=1) SCAN_INTERVAL_RIGS = timedelta(minutes=1)
SCAN_INTERVAL_ACCOUNTS = timedelta(minutes=60) SCAN_INTERVAL_ACCOUNTS = timedelta(minutes=60)
@ -191,6 +194,7 @@ class NiceHashBalanceSensor(Entity):
def device_state_attributes(self): def device_state_attributes(self):
"""Sensor device state attributes""" """Sensor device state attributes"""
return { return {
ATTR_ATTRIBUTION: ATTRIBUTION,
"total": self._total_balance, "total": self._total_balance,
"available": self._available, "available": self._available,
"pending": self._pending, "pending": self._pending,
@ -218,6 +222,7 @@ class NiceHashRigTemperatureSensor(Entity):
self._name = rig["name"] self._name = rig["name"]
self._temps = [] self._temps = []
self._num_devices = 0 self._num_devices = 0
self._num_active_devices = 0
_LOGGER.debug(f"Mining Rig Temperature Sensor: {self._name} ({self._rig_id})") _LOGGER.debug(f"Mining Rig Temperature Sensor: {self._name} ({self._rig_id})")
@property @property
@ -248,20 +253,22 @@ class NiceHashRigTemperatureSensor(Entity):
rig_data = mining_rigs.get(self._rig_id) rig_data = mining_rigs.get(self._rig_id)
devices = rig_data.get("devices") devices = rig_data.get("devices")
highest_temp = 0 highest_temp = 0
num_devices = len(devices)
self._num_devices = num_devices
if num_devices > 0:
_LOGGER.debug(f"{self._name}: Found {num_devices} devices")
self._temps = [] self._temps = []
self._num_devices = len(devices)
if self._num_devices > 0:
_LOGGER.debug(f"{self._name}: Found {self._num_devices} devices")
for device in devices: for device in devices:
temp = int(device.get("temperature")) status = device.get("status").get("enumName")
if temp < 0:
# Ignore inactive devices # Ignore inactive devices
if status == DEVICE_STATUS_INACTIVE:
continue continue
temp = int(device.get("temperature"))
self._temps.append(temp) self._temps.append(temp)
if temp > highest_temp: if temp > highest_temp:
highest_temp = temp highest_temp = temp
self._num_active_devices = len(self._temps)
return highest_temp return highest_temp
else: else:
_LOGGER.debug(f"{self._name}: No devices found") _LOGGER.debug(f"{self._name}: No devices found")
@ -279,14 +286,17 @@ class NiceHashRigTemperatureSensor(Entity):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Sensor unit of measurement""" """Sensor unit of measurement"""
# Not Celsius because then HA might convert to Fahrenheit
return "C" return "C"
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Sensor device state attributes""" """Sensor device state attributes"""
return { return {
ATTR_ATTRIBUTION: ATTRIBUTION,
"temperatures": self._temps, "temperatures": self._temps,
"num_devices": self._num_devices, "active_devices": self._num_active_devices,
"total_devices": self._num_devices,
} }
async def async_added_to_hass(self): async def async_added_to_hass(self):