1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 08:26:41 +01:00
Files
core/homeassistant/components/motion_blinds/sensor.py
epenet be3d65538d Use runtime_data in motion_blinds integration (#166849)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 12:32:27 +02:00

123 lines
4.2 KiB
Python

"""Support for Motionblinds sensors."""
from typing import Any
from motionblinds import DEVICE_TYPES_WIFI
from motionblinds.motion_blinds import DEVICE_TYPE_TDBU
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import (
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .coordinator import MotionBlindsConfigEntry
from .entity import MotionCoordinatorEntity
ATTR_BATTERY_VOLTAGE = "battery_voltage"
async def async_setup_entry(
hass: HomeAssistant,
config_entry: MotionBlindsConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Perform the setup for Motionblinds."""
entities: list[SensorEntity] = []
coordinator = config_entry.runtime_data
motion_gateway = coordinator.gateway
for blind in motion_gateway.device_list.values():
entities.append(MotionSignalStrengthSensor(coordinator, blind))
if blind.device_type == DEVICE_TYPE_TDBU:
entities.append(MotionTDBUBatterySensor(coordinator, blind, "Bottom"))
entities.append(MotionTDBUBatterySensor(coordinator, blind, "Top"))
elif blind.battery_voltage is not None and blind.battery_voltage > 0:
# Only add battery powered blinds
entities.append(MotionBatterySensor(coordinator, blind))
# Do not add signal sensor twice for direct WiFi blinds
if motion_gateway.device_type not in DEVICE_TYPES_WIFI:
entities.append(MotionSignalStrengthSensor(coordinator, motion_gateway))
async_add_entities(entities)
class MotionBatterySensor(MotionCoordinatorEntity, SensorEntity):
"""Representation of a Motion Battery Sensor."""
_attr_device_class = SensorDeviceClass.BATTERY
_attr_native_unit_of_measurement = PERCENTAGE
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(self, coordinator, blind):
"""Initialize the Motion Battery Sensor."""
super().__init__(coordinator, blind)
self._attr_unique_id = f"{blind.mac}-battery"
@property
def native_value(self):
"""Return the state of the sensor."""
return self._blind.battery_level
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return device specific state attributes."""
return {ATTR_BATTERY_VOLTAGE: self._blind.battery_voltage}
class MotionTDBUBatterySensor(MotionBatterySensor):
"""Representation of a Motion Battery Sensor for a Top Down Bottom Up blind."""
def __init__(self, coordinator, blind, motor):
"""Initialize the Motion Battery Sensor."""
super().__init__(coordinator, blind)
self._motor = motor
self._attr_unique_id = f"{blind.mac}-{motor}-battery"
self._attr_translation_key = f"{motor.lower()}_battery"
@property
def native_value(self):
"""Return the state of the sensor."""
if self._blind.battery_level is None:
return None
return self._blind.battery_level[self._motor[0]]
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return device specific state attributes."""
attributes = {}
if self._blind.battery_voltage is not None:
attributes[ATTR_BATTERY_VOLTAGE] = self._blind.battery_voltage[
self._motor[0]
]
return attributes
class MotionSignalStrengthSensor(MotionCoordinatorEntity, SensorEntity):
"""Representation of a Motion Signal Strength Sensor."""
_attr_device_class = SensorDeviceClass.SIGNAL_STRENGTH
_attr_entity_registry_enabled_default = False
_attr_native_unit_of_measurement = SIGNAL_STRENGTH_DECIBELS_MILLIWATT
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(self, coordinator, blind):
"""Initialize the Motion Signal Strength Sensor."""
super().__init__(coordinator, blind)
self._attr_unique_id = f"{blind.mac}-RSSI"
@property
def native_value(self):
"""Return the state of the sensor."""
return self._blind.RSSI