Skip to content

Commit

Permalink
Add Reolink battery entities (home-assistant#117506)
Browse files Browse the repository at this point in the history
* add battery sensors

* Disable Battery Temperature and State by default

* fix mypy

* Use device class for icon
  • Loading branch information
starkillerOG authored May 16, 2024
1 parent 68b7302 commit f788f88
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
6 changes: 6 additions & 0 deletions homeassistant/components/reolink/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
"ptz_pan_position": {
"default": "mdi:pan"
},
"battery_temperature": {
"default": "mdi:thermometer"
},
"battery_state": {
"default": "mdi:battery-charging"
},
"wifi_signal": {
"default": "mdi:wifi"
},
Expand Down
42 changes: 39 additions & 3 deletions homeassistant/components/reolink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from decimal import Decimal

from reolink_aio.api import Host
from reolink_aio.enums import BatteryEnum

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, EntityCategory
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
Expand All @@ -37,7 +39,7 @@ class ReolinkSensorEntityDescription(
):
"""A class that describes sensor entities for a camera channel."""

value: Callable[[Host, int], int | float]
value: Callable[[Host, int], StateType]


@dataclass(frozen=True, kw_only=True)
Expand All @@ -47,7 +49,7 @@ class ReolinkHostSensorEntityDescription(
):
"""A class that describes host sensor entities."""

value: Callable[[Host], int | None]
value: Callable[[Host], StateType]


SENSORS = (
Expand All @@ -60,6 +62,40 @@ class ReolinkHostSensorEntityDescription(
value=lambda api, ch: api.ptz_pan_position(ch),
supported=lambda api, ch: api.supported(ch, "ptz_position"),
),
ReolinkSensorEntityDescription(
key="battery_percent",
cmd_key="GetBatteryInfo",
translation_key="battery_percent",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
value=lambda api, ch: api.battery_percentage(ch),
supported=lambda api, ch: api.supported(ch, "battery"),
),
ReolinkSensorEntityDescription(
key="battery_temperature",
cmd_key="GetBatteryInfo",
translation_key="battery_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value=lambda api, ch: api.battery_temperature(ch),
supported=lambda api, ch: api.supported(ch, "battery"),
),
ReolinkSensorEntityDescription(
key="battery_state",
cmd_key="GetBatteryInfo",
translation_key="battery_state",
device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
options=[state.name for state in BatteryEnum],
value=lambda api, ch: BatteryEnum(api.battery_status(ch)).name,
supported=lambda api, ch: api.supported(ch, "battery"),
),
)

HOST_SENSORS = (
Expand Down
14 changes: 14 additions & 0 deletions homeassistant/components/reolink/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,20 @@
"ptz_pan_position": {
"name": "PTZ pan position"
},
"battery_percent": {
"name": "Battery percentage"
},
"battery_temperature": {
"name": "Battery temperature"
},
"battery_state": {
"name": "Battery state",
"state": {
"discharging": "Discharging",
"charging": "Charging",
"chargecomplete": "Charge complete"
}
},
"hdd_storage": {
"name": "HDD {hdd_index} storage"
},
Expand Down

0 comments on commit f788f88

Please sign in to comment.