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): async def async_update(self):
"""Update entity""" """Update entity"""
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
mining_rigs = self.coordinator.data.get("miningRigs")
try: try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id)) rig = MiningRig(mining_rigs.get(self._rig_id))
if rig: device = rig.devices.get(self._device_id)
device = rig.devices.get(self._device_id) self._status = device.status
if device: self._load = device.load
self._status = device.status self._rpm = device.rpm
self._load = device.load self._temperature = device.temperature
self._rpm = device.rpm algorithms = device.speeds
self._temperature = device.temperature if len(algorithms) > 0:
algorithms = device.speeds algorithm = algorithms[0]
if len(algorithms) > 0: self._algorithm = algorithm.get("title")
algorithm = algorithms[0] self._speed = float(algorithm.get("speed"))
self._algorithm = algorithm.get("title") self._speed_title = algorithm.get("title")
self._speed = float(algorithm.get("speed")) self._speed_unit = algorithm.get("displaySuffix")
self._speed_title = algorithm.get("title") else:
self._speed_unit = algorithm.get("displaySuffix") self._speed_title = "Unknown"
else: self._speed_unit = "MH"
self._speed_title = "Unknown" self._algorithm = None
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._status = DEVICE_STATUS_UNKNOWN

View File

@ -61,14 +61,13 @@ class RigTemperatureSensor(Entity):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs")
self._highest_temp = 0 self._highest_temp = 0
try: try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id)) rig = MiningRig(mining_rigs.get(self._rig_id))
if rig: self._temps = rig.temperatures
self._temps = rig.temperatures self._num_devices = rig.num_devices
self._num_devices = rig.num_devices self._highest_temp = max(rig.temperatures)
self._highest_temp = max(rig.temperatures)
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) temperature\n{e}") _LOGGER.error(f"Unable to get mining rig ({self._rig_id}) temperature\n{e}")
@ -143,13 +142,12 @@ class RigStatusSensor(Entity):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs")
status = DEVICE_STATUS_UNKNOWN status = DEVICE_STATUS_UNKNOWN
try: try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id)) rig = MiningRig(mining_rigs.get(self._rig_id))
if rig: status = rig.status
status = rig.status self._status_time = datetime.fromtimestamp(rig.status_time / 1000.0)
self._status_time = datetime.fromtimestamp(rig.status_time / 1000.0)
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}") _LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
self._status_time = None self._status_time = None
@ -231,12 +229,11 @@ class RigProfitabilitySensor(Entity):
@property @property
def state(self): def state(self):
"""Sensor state""" """Sensor state"""
mining_rigs = self.coordinator.data.get("miningRigs")
try: try:
mining_rigs = self.coordinator.data.get("miningRigs")
rig = MiningRig(mining_rigs.get(self._rig_id)) rig = MiningRig(mining_rigs.get(self._rig_id))
if rig: self._profitability = rig.profitability
self._profitability = rig.profitability self._unpaid_amount = rig.unpaid_amount
self._unpaid_amount = rig.unpaid_amount
except Exception as e: except Exception as e:
_LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}") _LOGGER.error(f"Unable to get mining rig ({self._rig_id}) status\n{e}")
self._profitability = 0 self._profitability = 0