2019-02-13 21:21:14 +01:00
|
|
|
"""Support for IKEA Tradfri sensors."""
|
2021-09-18 21:49:47 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 16:19:06 +02:00
|
|
|
from collections.abc import Callable
|
2022-01-27 11:12:52 +01:00
|
|
|
from typing import Any
|
2021-09-18 21:49:47 +02:00
|
|
|
|
|
|
|
from pytradfri.command import Command
|
2019-09-22 23:01:32 +02:00
|
|
|
|
2021-12-16 07:07:33 -05:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
2021-09-18 21:49:47 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-16 07:07:33 -05:00
|
|
|
from homeassistant.const import PERCENTAGE
|
2021-09-18 21:49:47 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-01 06:23:39 +01:00
|
|
|
|
2022-01-27 11:12:52 +01:00
|
|
|
from .base_class import TradfriBaseEntity
|
|
|
|
from .const import CONF_GATEWAY_ID, COORDINATOR, COORDINATOR_LIST, DOMAIN, KEY_API
|
|
|
|
from .coordinator import TradfriDeviceDataUpdateCoordinator
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-10-05 17:05:38 +01:00
|
|
|
|
2021-09-18 21:49:47 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-09-19 21:21:43 +02:00
|
|
|
"""Set up a Tradfri config entry."""
|
2019-10-06 19:24:56 +02:00
|
|
|
gateway_id = config_entry.data[CONF_GATEWAY_ID]
|
2022-01-27 11:12:52 +01:00
|
|
|
coordinator_data = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
|
|
|
|
api = coordinator_data[KEY_API]
|
2018-09-19 21:21:43 +02:00
|
|
|
|
2021-11-09 15:18:13 +01:00
|
|
|
async_add_entities(
|
2022-01-27 17:47:47 +01:00
|
|
|
TradfriBatterySensor(
|
2022-01-27 11:12:52 +01:00
|
|
|
device_coordinator,
|
|
|
|
api,
|
|
|
|
gateway_id,
|
|
|
|
)
|
|
|
|
for device_coordinator in coordinator_data[COORDINATOR_LIST]
|
2021-11-09 15:18:13 +01:00
|
|
|
if (
|
2022-01-27 11:12:52 +01:00
|
|
|
not device_coordinator.device.has_light_control
|
|
|
|
and not device_coordinator.device.has_socket_control
|
|
|
|
and not device_coordinator.device.has_signal_repeater_control
|
|
|
|
and not device_coordinator.device.has_air_purifier_control
|
2021-11-09 15:18:13 +01:00
|
|
|
)
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2017-10-05 17:05:38 +01:00
|
|
|
|
|
|
|
|
2022-01-27 17:47:47 +01:00
|
|
|
class TradfriBatterySensor(TradfriBaseEntity, SensorEntity):
|
2017-10-05 17:05:38 +01:00
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
2021-12-16 07:07:33 -05:00
|
|
|
_attr_device_class = SensorDeviceClass.BATTERY
|
2021-08-11 18:57:50 +02:00
|
|
|
_attr_native_unit_of_measurement = PERCENTAGE
|
2021-05-31 10:50:11 +02:00
|
|
|
|
2021-09-18 21:49:47 +02:00
|
|
|
def __init__(
|
2021-09-20 14:33:50 +02:00
|
|
|
self,
|
2022-01-27 11:12:52 +01:00
|
|
|
device_coordinator: TradfriDeviceDataUpdateCoordinator,
|
2021-09-20 14:33:50 +02:00
|
|
|
api: Callable[[Command | list[Command]], Any],
|
|
|
|
gateway_id: str,
|
2021-09-18 21:49:47 +02:00
|
|
|
) -> None:
|
2022-01-27 11:12:52 +01:00
|
|
|
"""Initialize a switch."""
|
|
|
|
super().__init__(
|
|
|
|
device_coordinator=device_coordinator,
|
|
|
|
api=api,
|
|
|
|
gateway_id=gateway_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
self._refresh() # Set initial state
|
2017-10-05 17:05:38 +01:00
|
|
|
|
2022-01-27 11:12:52 +01:00
|
|
|
def _refresh(self) -> None:
|
|
|
|
"""Refresh the device."""
|
|
|
|
self._attr_native_value = self.coordinator.data.device_info.battery_level
|