fix(sensor): fix device sensors not updating

This commit is contained in:
Brian Berg 2020-07-12 18:02:47 +00:00
parent b100a15b9d
commit ea5816ea20

View File

@ -42,14 +42,6 @@ class DeviceSensor(Entity):
self._rig_name = rig_data.get("name") self._rig_name = rig_data.get("name")
self._device_name = device_data.get("name") self._device_name = device_data.get("name")
self._device_id = device_data.get("id") self._device_id = device_data.get("id")
self._status = DEVICE_STATUS_UNKNOWN
self._load = 0
self._rpm = 0
self._algorithm = None
self._speed = 0
self._speed_title = "Unknown"
self._speed_unit = "MH"
self._temperature = 0
@property @property
def name(self): def name(self):
@ -76,20 +68,6 @@ class DeviceSensor(Entity):
"""Sensor unit of measurement""" """Sensor unit of measurement"""
return None return None
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"status": self._status,
"algorithm": self._algorithm,
"speed": self._speed,
"speed_unit": f"{self._speed_unit}/s",
"temperature": self._temperature,
"load": f"{self._load}%",
"rpm": self._rpm,
}
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications""" """Connect to dispatcher listening for entity data notifications"""
self.async_on_remove( self.async_on_remove(
@ -99,34 +77,14 @@ class DeviceSensor(Entity):
async def async_update(self): async def async_update(self):
"""Update entity""" """Update entity"""
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
def _get_device(self):
try: try:
mining_rigs = self.coordinator.data.get("miningRigs") mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id)) rig = MiningRig(mining_rigs.get(self._rig_id))
device = rig.devices.get(self._device_id) return rig.devices.get(self._device_id)
self._status = device.status
self._load = device.load
self._rpm = device.rpm
self._temperature = device.temperature
algorithms = device.speeds
if len(algorithms) > 0:
algorithm = algorithms[0]
self._algorithm = algorithm.get("title")
self._speed = float(algorithm.get("speed"))
self._speed_title = algorithm.get("title")
self._speed_unit = algorithm.get("displaySuffix")
else:
self._speed_title = "Unknown"
self._speed_unit = "MH"
self._algorithm = None
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining device ({self._device_id})\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
self._speed = 0
self._speed_title = "Unknown"
self._speed_unit = "MH"
self._algorithm = None
class DeviceStatusSensor(DeviceSensor): class DeviceStatusSensor(DeviceSensor):
@ -134,6 +92,8 @@ class DeviceStatusSensor(DeviceSensor):
Displays status of a mining rig device Displays status of a mining rig device
""" """
_status = "Unknown"
@property @property
def name(self): def name(self):
"""Sensor name""" """Sensor name"""
@ -147,6 +107,12 @@ class DeviceStatusSensor(DeviceSensor):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
device = self._get_device()
if device:
self._status = device.status
else:
self._status = "Unknown"
return self._status return self._status
@property @property
@ -154,12 +120,25 @@ class DeviceStatusSensor(DeviceSensor):
"""Sensor icon""" """Sensor icon"""
return ICON_PULSE return ICON_PULSE
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"status": self._status,
"rig": self._rig_name,
}
class DeviceSpeedSensor(DeviceSensor): class DeviceSpeedSensor(DeviceSensor):
""" """
Displays speed of a mining rig device Displays speed of a mining rig device
""" """
_algorithm = None
_speed = 0.00
_speed_unit = "MH"
@property @property
def name(self): def name(self):
"""Sensor name""" """Sensor name"""
@ -173,6 +152,17 @@ class DeviceSpeedSensor(DeviceSensor):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
device = self._get_device()
if device and len(device.speeds) > 0:
algorithm = device.speeds[0]
self._algorithm = algorithm.get("title")
self._speed = algorithm.get("speed")
self._speed_unit = algorithm.get("displaySuffix")
else:
self._algorithm = "Unknown"
self._speed = 0.00
self._speed_unit = "MH"
return self._speed return self._speed
@property @property
@ -185,12 +175,27 @@ class DeviceSpeedSensor(DeviceSensor):
"""Sensor unit of measurement""" """Sensor unit of measurement"""
return f"{self._speed_unit}/s" return f"{self._speed_unit}/s"
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"algorithm": self._algorithm,
"speed": self._speed,
"speed_unit": self._speed_unit,
"rig": self._rig_name,
}
class DeviceAlgorithmSensor(DeviceSensor): class DeviceAlgorithmSensor(DeviceSensor):
""" """
Displays algorithm of a mining rig device Displays algorithm of a mining rig device
""" """
_algorithm = None
_speed = 0.00
_speed_unit = "MH"
@property @property
def name(self): def name(self):
"""Sensor name""" """Sensor name"""
@ -204,6 +209,17 @@ class DeviceAlgorithmSensor(DeviceSensor):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
device = self._get_device()
if device and len(device.speeds) > 0:
algorithm = device.speeds[0]
self._algorithm = algorithm.get("title")
self._speed = algorithm.get("speed")
self._speed_unit = algorithm.get("displaySuffix")
else:
self._algorithm = "Unknown"
self._speed = 0.00
self._speed_unit = "MH"
return self._algorithm return self._algorithm
@property @property
@ -211,12 +227,25 @@ class DeviceAlgorithmSensor(DeviceSensor):
"""Sensor icon""" """Sensor icon"""
return ICON_PICKAXE return ICON_PICKAXE
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"algorithm": self._algorithm,
"speed": self._speed,
"speed_unit": self._speed_unit,
"rig": self._rig_name,
}
class DeviceTemperatureSensor(DeviceSensor): class DeviceTemperatureSensor(DeviceSensor):
""" """
Displays temperature of a mining rig device Displays temperature of a mining rig device
""" """
_temperature = 0
@property @property
def name(self): def name(self):
"""Sensor name""" """Sensor name"""
@ -230,6 +259,12 @@ class DeviceTemperatureSensor(DeviceSensor):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
device = self._get_device()
if device:
self._temperature = device.temperature
else:
self._temperature = 0
return self._temperature return self._temperature
@property @property
@ -243,12 +278,23 @@ class DeviceTemperatureSensor(DeviceSensor):
# Not Celsius because then HA might convert to Fahrenheit # Not Celsius because then HA might convert to Fahrenheit
return "C" return "C"
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"temperature": self._temperature,
"rig": self._rig_name,
}
class DeviceLoadSensor(DeviceSensor): class DeviceLoadSensor(DeviceSensor):
""" """
Displays load of a mining rig device Displays load of a mining rig device
""" """
_load = 0
@property @property
def name(self): def name(self):
"""Sensor name""" """Sensor name"""
@ -262,6 +308,12 @@ class DeviceLoadSensor(DeviceSensor):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
device = self._get_device()
if device:
self._load = device.load
else:
self._load = 0
return self._load return self._load
@property @property
@ -274,6 +326,15 @@ class DeviceLoadSensor(DeviceSensor):
"""Sensor unit of measurement""" """Sensor unit of measurement"""
return "%" return "%"
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"load": self._load,
"rig": self._rig_name,
}
class DeviceRPMSensor(DeviceSensor): class DeviceRPMSensor(DeviceSensor):
""" """
@ -293,6 +354,12 @@ class DeviceRPMSensor(DeviceSensor):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
device = self._get_device()
if device:
self._rpm = device.rpm
else:
self._rpm = 0
return self._rpm return self._rpm
@property @property
@ -305,3 +372,12 @@ class DeviceRPMSensor(DeviceSensor):
"""Sensor unit of measurement""" """Sensor unit of measurement"""
return "RPM" return "RPM"
@property
def device_state_attributes(self):
"""Sensor device state attributes"""
return {
ATTR_ATTRIBUTION: NICEHASH_ATTRIBUTION,
"rpm": self._rpm,
"rig": self._rig_name,
}