Skip to content

Commit

Permalink
Update some log messages to f strings for consistency, and remove som…
Browse files Browse the repository at this point in the history
…e unused code.
  • Loading branch information
b-rowan committed Jan 23, 2024
1 parent 0a0f232 commit 5083d0d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 47 deletions.
2 changes: 1 addition & 1 deletion custom_components/miner/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# return len(devices) > 0


# config_entry_flow.register_discovery_flow(DOMAIN, "Miner", _async_has_devices)
# config_entry_flow.register_discovery_flow(DOMAIN, "miner", _async_has_devices)


async def validate_ip_input(
Expand Down
1 change: 1 addition & 0 deletions custom_components/miner/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Constants for the Miner integration."""

DOMAIN = "miner"

CONF_IP = "ip"
CONF_TITLE = "title"
CONF_SSH_PASSWORD = "ssh_password"
Expand Down
4 changes: 3 additions & 1 deletion custom_components/miner/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ async def _async_update_data(self):
if self.miner is None:
self.miner = await pyasic.get_miner(miner_ip)

_LOGGER.debug(f"Found miner :{self.miner}")

if self.miner is None:
raise UpdateFailed("Miner Offline")

Expand Down Expand Up @@ -93,7 +95,7 @@ async def _async_update_data(self):
except Exception as err:
raise UpdateFailed("Unknown Error") from err

_LOGGER.debug(miner_data)
_LOGGER.debug(f"Got data: {miner_data}")

data = {
"hostname": miner_data.hostname,
Expand Down
10 changes: 2 additions & 8 deletions custom_components/miner/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ async def async_setup_entry(
) -> None:
"""Add sensors for passed config_entry in HA."""
coordinator: MinerCoordinator = hass.data[DOMAIN][config_entry.entry_id]
created = set()

@callback
def _create_entity(key: str):
"""Create a sensor entity."""
created.add(key)

await coordinator.async_config_entry_first_refresh()
if coordinator.miner.supports_autotuning:
Expand Down Expand Up @@ -109,12 +103,12 @@ async def async_set_native_value(self, value):
miner = self.coordinator.miner

_LOGGER.debug(
"%s: setting power limit to %s.", self.coordinator.entry.title, value
f"{self.coordinator.entry.title}: setting power limit to {value}."
)

if not miner.supports_autotuning:
raise TypeError(
f"{self.coordinator.entry.title} does not support setting power limit."
f"{self.coordinator.entry.title}: Tuning not supported."
)

result = await miner.set_power_limit(int(value))
Expand Down
53 changes: 20 additions & 33 deletions custom_components/miner/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Support for IoTaWatt Energy monitor."""
"""Support for Miner sensors."""
from __future__ import annotations

import logging
Expand Down Expand Up @@ -33,62 +33,54 @@ class MinerSensorEntityDescription(SensorEntityDescription):
value: Callable = None


class MinerNumberEntityDescription(SensorEntityDescription):
"""Class describing ASIC Miner number entities."""

value: Callable = None


ENTITY_DESCRIPTION_KEY_MAP: dict[
str, MinerSensorEntityDescription or MinerNumberEntityDescription
] = {
ENTITY_DESCRIPTION_KEY_MAP: dict[str, MinerSensorEntityDescription] = {
"temperature": MinerSensorEntityDescription(
"Temperature",
key="Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
"board_temperature": MinerSensorEntityDescription(
"Board Temperature",
key="Board Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
"chip_temperature": MinerSensorEntityDescription(
"Chip Temperature",
key="Chip Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
"hashrate": MinerSensorEntityDescription(
"Hashrate",
key="Hashrate",
native_unit_of_measurement=TERA_HASH_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
),
"ideal_hashrate": MinerSensorEntityDescription(
"Ideal Hashrate",
key="Ideal Hashrate",
native_unit_of_measurement=TERA_HASH_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
),
"board_hashrate": MinerSensorEntityDescription(
"Board Hashrate",
key="Board Hashrate",
native_unit_of_measurement=TERA_HASH_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
),
"power_limit": MinerSensorEntityDescription(
"Power Limit",
key="Power Limit",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
),
"miner_consumption": MinerSensorEntityDescription(
"Miner Consumption",
key="Miner Consumption",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
),
"efficiency": MinerSensorEntityDescription(
"Efficiency",
key="Efficiency",
native_unit_of_measurement=JOULES_PER_TERA_HASH,
state_class=SensorStateClass.MEASUREMENT,
),
"fan_speed": MinerSensorEntityDescription(
"Fan Speed",
key="Fan Speed",
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
state_class=SensorStateClass.MEASUREMENT,
),
Expand Down Expand Up @@ -168,12 +160,14 @@ def __init__(
self._attr_unique_id = f"{self.coordinator.data['mac']}-{sensor}"
self._sensor = sensor
self.entity_description = entity_description
self._attr_force_update = True

@property
def _sensor_data(self):
"""Return sensor data."""
return self.coordinator.data["miner_sensors"][self._sensor]
try:
return self.coordinator.data["miner_sensors"][self._sensor]
except LookupError:
return None

@property
def name(self) -> str | None:
Expand Down Expand Up @@ -220,17 +214,13 @@ def __init__(
self._board_num = board_num
self._sensor = sensor
self.entity_description = entity_description
self._attr_force_update = True

@property
def _sensor_data(self):
"""Return sensor data."""
if (
self._board_num in self.coordinator.data["board_sensors"]
and self._sensor in self.coordinator.data["board_sensors"][self._board_num]
):
try:
return self.coordinator.data["board_sensors"][self._board_num][self._sensor]
else:
except LookupError:
return None

@property
Expand Down Expand Up @@ -283,12 +273,9 @@ def __init__(
@property
def _sensor_data(self):
"""Return sensor data."""
if (
self._fan_num in self.coordinator.data["fan_sensors"]
and self._sensor in self.coordinator.data["fan_sensors"][self._fan_num]
):
try:
return self.coordinator.data["fan_sensors"][self._fan_num][self._sensor]
else:
except LookupError:
return None

@property
Expand Down
8 changes: 4 additions & 4 deletions custom_components/miner/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ def device_info(self) -> entity.DeviceInfo:
async def async_turn_on(self) -> None:
"""Turn on miner."""
miner = self.coordinator.miner
_LOGGER.debug("%s: Resume mining.", self.coordinator.entry.title)
_LOGGER.debug(f"{self.coordinator.entry.title}: Resume mining.")
if not miner.supports_shutdown:
raise TypeError(f"{miner} does not support shutdown mode.")
raise TypeError(f"{miner}: Shutdown not supported.")
self._attr_is_on = True
await miner.resume_mining()
self.async_write_ha_state()

async def async_turn_off(self) -> None:
"""Turn off miner."""
miner = self.coordinator.miner
_LOGGER.debug("%s: Stop mining.", self.coordinator.entry.title)
_LOGGER.debug(f"{self.coordinator.entry.title}: Stop mining.")
if not miner.supports_shutdown:
raise TypeError(f"{miner} does not support shutdown mode.")
raise TypeError(f"{miner}: Shutdown not supported.")
self._attr_is_on = False
await miner.stop_mining()
self.async_write_ha_state()
Expand Down

0 comments on commit 5083d0d

Please sign in to comment.