Skip to content

Commit

Permalink
Merge pull request #658 from natekspencer/cleanup
Browse files Browse the repository at this point in the history
Set device class and unit for hdd disk sensors
  • Loading branch information
JurajNyiri authored Oct 28, 2024
2 parents 6ce39e0 + dcfe236 commit 965d785
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions custom_components/tapo_control/sensor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Tapo camera sensors."""

import re

from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
STATE_UNAVAILABLE,
UnitOfInformation,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
Expand Down Expand Up @@ -249,6 +252,15 @@ def updateTapo(self, camData: dict | None) -> None:
for hdd in camData["sdCardData"]:
if hdd["disk_name"] == self._sensor_name:
state = hdd[self._sensor_property]
if "space" in self._sensor_property and (
match := re.search(r"[-+]?\d*\.?\d+", state)
):
value = match.group()
if (unit := state.replace(value, "")) in UnitOfInformation:
self._attr_device_class = SensorDeviceClass.DATA_SIZE
self._attr_native_unit_of_measurement = UnitOfInformation(unit)
self._attr_suggested_unit_of_measurement = UnitOfInformation.GIGABYTES
state = value
self._attr_native_value = state


Expand Down

0 comments on commit 965d785

Please sign in to comment.