refactor(sensors): minor simplification

This commit is contained in:
Brian Berg 2020-07-11 18:41:22 +00:00
parent eefd191e51
commit 78fc79e7a7
2 changed files with 27 additions and 32 deletions

View File

@ -99,27 +99,25 @@ class DeviceSensor(Entity):
async def async_update(self):
"""Update entity"""
await self.coordinator.async_request_refresh()
mining_rigs = self.coordinator.data.get("miningRigs")
try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
if rig:
device = rig.devices.get(self._device_id)
if device:
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
device = 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:
_LOGGER.error(f"Unable to get mining device ({self._device_id})\n{e}")
self._status = DEVICE_STATUS_UNKNOWN

View File

@ -61,14 +61,13 @@ class RigTemperatureSensor(Entity):
@property
def state(self):
"""Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs")
self._highest_temp = 0
try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
if rig:
self._temps = rig.temperatures
self._num_devices = rig.num_devices
self._highest_temp = max(rig.temperatures)
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}")
@ -143,13 +142,12 @@ class RigStatusSensor(Entity):
@property
def state(self):
"""Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs")
status = DEVICE_STATUS_UNKNOWN
try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
if rig:
status = rig.status
self._status_time = datetime.fromtimestamp(rig.status_time / 1000.0)
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}")
self._status_time = None
@ -231,12 +229,11 @@ class RigProfitabilitySensor(Entity):
@property
def state(self):
"""Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs")
try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id))
if rig:
self._profitability = rig.profitability
self._unpaid_amount = rig.unpaid_amount
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}")
self._profitability = 0