mirror of
https://github.com/xiaochao99/fn_nas
synced 2025-11-28 17:19:42 +00:00
将硬盘健康状态改为二元传感器用于警报通知
This commit is contained in:
72
custom_components/fn_nas/binary_sensor.py
Normal file
72
custom_components/fn_nas/binary_sensor.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import logging
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from .const import (
|
||||
DOMAIN, HDD_HEALTH, DEVICE_ID_NAS, DATA_UPDATE_COORDINATOR
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
domain_data = hass.data[DOMAIN][config_entry.entry_id]
|
||||
coordinator = domain_data[DATA_UPDATE_COORDINATOR]
|
||||
|
||||
entities = []
|
||||
existing_ids = set()
|
||||
|
||||
# 添加硬盘健康状态二元传感器
|
||||
for disk in coordinator.data.get("disks", []):
|
||||
health_uid = f"{config_entry.entry_id}_{disk['device']}_health_binary"
|
||||
if health_uid not in existing_ids:
|
||||
entities.append(
|
||||
DiskHealthBinarySensor(
|
||||
coordinator,
|
||||
disk["device"],
|
||||
f"硬盘 {disk.get('model', '未知')} 健康状态",
|
||||
health_uid,
|
||||
disk
|
||||
)
|
||||
)
|
||||
existing_ids.add(health_uid)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class DiskHealthBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||
def __init__(self, coordinator, device_id, name, unique_id, disk_info):
|
||||
super().__init__(coordinator)
|
||||
self.device_id = device_id
|
||||
self._attr_name = name
|
||||
self._attr_unique_id = unique_id
|
||||
self.disk_info = disk_info
|
||||
self._attr_device_info = {
|
||||
"identifiers": {(DOMAIN, f"disk_{device_id}")},
|
||||
"name": disk_info.get("model", "未知硬盘"),
|
||||
"manufacturer": "硬盘设备",
|
||||
"via_device": (DOMAIN, DEVICE_ID_NAS)
|
||||
}
|
||||
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""返回True表示有问题,False表示正常"""
|
||||
for disk in self.coordinator.data.get("disks", []):
|
||||
if disk["device"] == self.device_id:
|
||||
health = disk.get("health", "未知")
|
||||
# 将健康状态映射为二元状态
|
||||
if health in ["正常", "良好", "OK", "ok", "good", "Good"]:
|
||||
return False # 正常状态
|
||||
elif health in ["警告", "异常", "错误", "warning", "Warning", "error", "Error", "bad", "Bad"]:
|
||||
return True # 有问题状态
|
||||
else:
|
||||
# 未知状态也视为有问题
|
||||
return True
|
||||
return True # 默认视为有问题
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""根据状态返回图标"""
|
||||
if self.is_on:
|
||||
return "mdi:alert-circle" # 有问题时显示警告图标
|
||||
else:
|
||||
return "mdi:check-circle" # 正常时显示对勾图标
|
||||
@@ -3,6 +3,7 @@ from homeassistant.const import Platform
|
||||
DOMAIN = "fn_nas"
|
||||
PLATFORMS = [
|
||||
Platform.SENSOR,
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.SWITCH,
|
||||
Platform.BUTTON
|
||||
]
|
||||
|
||||
@@ -3,8 +3,8 @@ from homeassistant.components.sensor import SensorEntity, SensorDeviceClass, Sen
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from homeassistant.const import UnitOfTemperature
|
||||
from .const import (
|
||||
DOMAIN, HDD_TEMP, HDD_HEALTH, HDD_STATUS, SYSTEM_INFO, ICON_DISK,
|
||||
ICON_TEMPERATURE, ICON_HEALTH, ATTR_DISK_MODEL, ATTR_SERIAL_NO,
|
||||
DOMAIN, HDD_TEMP, HDD_STATUS, SYSTEM_INFO, ICON_DISK,
|
||||
ICON_TEMPERATURE, ATTR_DISK_MODEL, ATTR_SERIAL_NO,
|
||||
ATTR_POWER_ON_HOURS, ATTR_TOTAL_CAPACITY, ATTR_HEALTH_STATUS,
|
||||
DEVICE_ID_NAS, DATA_UPDATE_COORDINATOR
|
||||
)
|
||||
@@ -38,22 +38,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
)
|
||||
existing_ids.add(temp_uid)
|
||||
|
||||
# 健康状态传感器
|
||||
health_uid = f"{config_entry.entry_id}_{disk['device']}_health"
|
||||
if health_uid not in existing_ids:
|
||||
entities.append(
|
||||
DiskSensor(
|
||||
coordinator,
|
||||
disk["device"],
|
||||
HDD_HEALTH,
|
||||
f"硬盘 {disk.get('model', '未知')} 健康状态",
|
||||
health_uid,
|
||||
None,
|
||||
ICON_HEALTH,
|
||||
disk
|
||||
)
|
||||
)
|
||||
existing_ids.add(health_uid)
|
||||
|
||||
|
||||
# 硬盘状态传感器
|
||||
status_uid = f"{config_entry.entry_id}_{disk['device']}_status"
|
||||
@@ -314,9 +299,7 @@ class DiskSensor(CoordinatorEntity, SensorEntity):
|
||||
elif isinstance(temp, (int, float)):
|
||||
return temp
|
||||
return None
|
||||
elif self.sensor_type == HDD_HEALTH:
|
||||
health = disk.get("health", "未知")
|
||||
return health if health != "未知" else "未知状态"
|
||||
|
||||
elif self.sensor_type == HDD_STATUS:
|
||||
return disk.get("status", "未知")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user