Обновить custom_components/motion_recorder/sensor.py

This commit is contained in:
2026-07-05 13:33:27 +03:00
parent 792a185204
commit ea1b6e3916
+66 -69
View File
@@ -1,70 +1,67 @@
"""Sensor platform for Motion Recorder.""" """Sensor platform for Motion Recorder."""
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from .const import DOMAIN, CONF_CAMERA_ENTITY from .const import DOMAIN
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Motion Recorder sensor.""" """Set up Motion Recorder sensor."""
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([MotionRecorderStatusSensor(coordinator, entry)]) async_add_entities([MotionRecorderStatusSensor(coordinator, entry)])
class MotionRecorderStatusSensor(CoordinatorEntity, SensorEntity): class MotionRecorderStatusSensor(CoordinatorEntity, SensorEntity):
"""Representation of a Motion Recorder status sensor.""" """Representation of a Motion Recorder status sensor."""
def __init__(self, coordinator, entry): def __init__(self, coordinator, entry):
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.entry = entry self.entry = entry
self._attr_unique_id = f"{entry.entry_id}_status" self._attr_unique_id = f"{entry.entry_id}_status"
self._attr_name = f"{entry.title} Статус" self._attr_name = f"{entry.title} Статус"
self._attr_translation_key = "motion_recorder_status" self._attr_translation_key = "motion_recorder_status"
# Добавляем информацию об устройстве self._attr_device_info = DeviceInfo(
self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, entry.entry_id)},
identifiers={(DOMAIN, entry.entry_id)}, name=entry.title,
name=entry.title, manufacturer="Motion Recorder",
manufacturer="Motion Recorder", model="Camera Recording Integration",
model="Camera Recording Integration", )
)
self._icon_map = {
# Icon mapping "idle": "mdi:sleep",
self._icon_map = { "detecting": "mdi:motion-sensor",
"idle": "mdi:sleep", "recording": "mdi:record-rec",
"detecting": "mdi:motion-sensor", "delaying": "mdi:timer-sand",
"recording": "mdi:record-rec", "finalizing": "mdi:file-check",
"delaying": "mdi:timer-sand", "error": "mdi:alert-circle",
"finalizing": "mdi:file-check", "disabled": "mdi:video-off",
"completed": "mdi:check-circle", }
"error": "mdi:alert-circle",
"disabled": "mdi:video-off", @property
} def native_value(self):
"""Return the state of the sensor."""
@property return self.coordinator.state
def native_value(self):
"""Return the state of the sensor.""" @property
return self.coordinator.state def extra_state_attributes(self):
"""Return the state attributes."""
@property return self.coordinator.attributes
def extra_state_attributes(self):
"""Return the state attributes.""" @property
return self.coordinator.attributes def icon(self):
"""Return the icon."""
@property return self._icon_map.get(self.coordinator.state, "mdi:camera")
def icon(self):
"""Return the icon.""" def _handle_coordinator_update(self) -> None:
return self._icon_map.get(self.coordinator.state, "mdi:camera") """Handle updated data from the coordinator."""
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.async_write_ha_state() self.async_write_ha_state()