Skip to content

Commit

Permalink
bug: fix some issues with unit of measurement on temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
UpstreamData committed Jan 7, 2025
1 parent 81c3aa5 commit ed05c3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
52 changes: 23 additions & 29 deletions custom_components/miner/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from __future__ import annotations

import logging
from collections.abc import Callable
from dataclasses import dataclass

from homeassistant.components.sensor import EntityCategory
from homeassistant.components.sensor import SensorDeviceClass
Expand All @@ -28,72 +26,68 @@
_LOGGER = logging.getLogger(__name__)


@dataclass
class MinerSensorEntityDescription(SensorEntityDescription):
"""Class describing ASIC Miner sensor entities."""

value: Callable = None


ENTITY_DESCRIPTION_KEY_MAP: dict[str, MinerSensorEntityDescription] = {
"temperature": MinerSensorEntityDescription(
ENTITY_DESCRIPTION_KEY_MAP: dict[str, SensorEntityDescription] = {
"temperature": SensorEntityDescription(
key="Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
suggested_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
),
"board_temperature": MinerSensorEntityDescription(
"board_temperature": SensorEntityDescription(
key="Board Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
suggested_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
),
"chip_temperature": MinerSensorEntityDescription(
"chip_temperature": SensorEntityDescription(
key="Chip Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
suggested_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
),
"hashrate": MinerSensorEntityDescription(
"hashrate": SensorEntityDescription(
key="Hashrate",
native_unit_of_measurement=TERA_HASH_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"ideal_hashrate": MinerSensorEntityDescription(
"ideal_hashrate": SensorEntityDescription(
key="Ideal Hashrate",
native_unit_of_measurement=TERA_HASH_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"board_hashrate": MinerSensorEntityDescription(
"board_hashrate": SensorEntityDescription(
key="Board Hashrate",
native_unit_of_measurement=TERA_HASH_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"power_limit": MinerSensorEntityDescription(
"power_limit": SensorEntityDescription(
key="Power Limit",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"miner_consumption": MinerSensorEntityDescription(
"miner_consumption": SensorEntityDescription(
key="Miner Consumption",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"efficiency": MinerSensorEntityDescription(
"efficiency": SensorEntityDescription(
key="Efficiency",
native_unit_of_measurement=JOULES_PER_TERA_HASH,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"fan_speed": MinerSensorEntityDescription(
"fan_speed": SensorEntityDescription(
key="Fan Speed",
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
state_class=SensorStateClass.MEASUREMENT,
Expand All @@ -113,7 +107,7 @@ async def async_setup_entry(
def _create_miner_entity(sensor: str) -> MinerSensor:
"""Create a miner sensor entity."""
description = ENTITY_DESCRIPTION_KEY_MAP.get(
sensor, MinerSensorEntityDescription("base_sensor")
sensor, SensorEntityDescription(key="base_sensor")
)
return MinerSensor(
coordinator=coordinator,
Expand All @@ -124,7 +118,7 @@ def _create_miner_entity(sensor: str) -> MinerSensor:
def _create_board_entity(board_num: int, sensor: str) -> MinerBoardSensor:
"""Create a board sensor entity."""
description = ENTITY_DESCRIPTION_KEY_MAP.get(
sensor, MinerSensorEntityDescription("base_sensor")
sensor, SensorEntityDescription(key="base_sensor")
)
return MinerBoardSensor(
coordinator=coordinator,
Expand All @@ -136,7 +130,7 @@ def _create_board_entity(board_num: int, sensor: str) -> MinerBoardSensor:
def _create_fan_entity(fan_num: int, sensor: str) -> MinerFanSensor:
"""Create a fan sensor entity."""
description = ENTITY_DESCRIPTION_KEY_MAP.get(
sensor, MinerSensorEntityDescription("base_sensor")
sensor, SensorEntityDescription(key="base_sensor")
)
return MinerFanSensor(
coordinator=coordinator,
Expand All @@ -162,13 +156,13 @@ def _create_fan_entity(fan_num: int, sensor: str) -> MinerFanSensor:
class MinerSensor(CoordinatorEntity[MinerCoordinator], SensorEntity):
"""Defines a Miner Sensor."""

entity_description: MinerSensorEntityDescription
entity_description: SensorEntityDescription

def __init__(
self,
coordinator: MinerCoordinator,
sensor: str,
entity_description: MinerSensorEntityDescription,
entity_description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator)
Expand Down Expand Up @@ -214,14 +208,14 @@ def available(self) -> bool:
class MinerBoardSensor(CoordinatorEntity[MinerCoordinator], SensorEntity):
"""Defines a Miner Board Sensor."""

entity_description: MinerSensorEntityDescription
entity_description: SensorEntityDescription

def __init__(
self,
coordinator: MinerCoordinator,
board_num: int,
sensor: str,
entity_description: MinerSensorEntityDescription,
entity_description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator)
Expand Down Expand Up @@ -268,14 +262,14 @@ def available(self) -> bool:
class MinerFanSensor(CoordinatorEntity[MinerCoordinator], SensorEntity):
"""Defines a Miner Fan Sensor."""

entity_description: MinerSensorEntityDescription
entity_description: SensorEntityDescription

def __init__(
self,
coordinator: MinerCoordinator,
fan_num: int,
sensor: str,
entity_description: MinerSensorEntityDescription,
entity_description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator)
Expand Down
10 changes: 0 additions & 10 deletions custom_components/miner/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from __future__ import annotations

import logging
from collections.abc import Callable
from dataclasses import dataclass

from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
Expand All @@ -20,13 +17,6 @@
_LOGGER = logging.getLogger(__name__)


@dataclass
class MinerSensorEntityDescription(SensorEntityDescription):
"""Class describing Miner sensor entities."""

value: Callable = None


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
Expand Down

0 comments on commit ed05c3b

Please sign in to comment.