2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Home Assistant iOS app sensors."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2021-08-10 14:47:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-12 13:43:17 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2023-06-29 02:22:55 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-05 19:09:14 +00:00
|
|
|
from homeassistant.const import PERCENTAGE
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2020-11-14 20:43:49 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2017-10-06 03:55:19 +00:00
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2024-09-11 14:07:42 +00:00
|
|
|
from . import devices
|
|
|
|
from .const import (
|
|
|
|
ATTR_BATTERY,
|
|
|
|
ATTR_BATTERY_LEVEL,
|
|
|
|
ATTR_BATTERY_STATE,
|
|
|
|
ATTR_BATTERY_STATE_FULL,
|
|
|
|
ATTR_BATTERY_STATE_UNKNOWN,
|
|
|
|
ATTR_BATTERY_STATE_UNPLUGGED,
|
|
|
|
ATTR_DEVICE,
|
|
|
|
ATTR_DEVICE_ID,
|
|
|
|
ATTR_DEVICE_NAME,
|
|
|
|
ATTR_DEVICE_PERMANENT_ID,
|
|
|
|
ATTR_DEVICE_SYSTEM_VERSION,
|
|
|
|
ATTR_DEVICE_TYPE,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2020-11-14 20:43:49 +00:00
|
|
|
|
2021-08-10 14:47:52 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="level",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2023-06-29 02:22:55 +00:00
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
2021-08-10 14:47:52 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="state",
|
2023-06-29 02:22:55 +00:00
|
|
|
translation_key="battery_state",
|
2021-08-10 14:47:52 +00:00
|
|
|
),
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_ICON_LEVEL = "mdi:battery"
|
|
|
|
DEFAULT_ICON_STATE = "mdi:power-plug"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:11:50 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the iOS sensor."""
|
2018-09-12 18:17:52 +00:00
|
|
|
# Leave here for if someone accidentally adds platform: ios to config
|
|
|
|
|
|
|
|
|
2022-01-03 18:11:50 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-09-12 18:17:52 +00:00
|
|
|
"""Set up iOS from a config entry."""
|
2024-02-20 20:26:24 +00:00
|
|
|
async_add_entities(
|
2021-08-10 14:47:52 +00:00
|
|
|
IOSSensor(device_name, device, description)
|
2024-09-11 14:07:42 +00:00
|
|
|
for device_name, device in devices(hass).items()
|
2021-08-10 14:47:52 +00:00
|
|
|
for description in SENSOR_TYPES
|
2024-02-20 20:26:24 +00:00
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class IOSSensor(SensorEntity):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Representation of an iOS sensor."""
|
|
|
|
|
2021-08-10 14:47:52 +00:00
|
|
|
_attr_should_poll = False
|
2023-06-29 02:22:55 +00:00
|
|
|
_attr_has_entity_name = True
|
2021-08-10 14:47:52 +00:00
|
|
|
|
2023-02-06 10:37:25 +00:00
|
|
|
def __init__(
|
2024-01-12 13:43:17 +00:00
|
|
|
self,
|
|
|
|
device_name: str,
|
|
|
|
device: dict[str, Any],
|
|
|
|
description: SensorEntityDescription,
|
2023-02-06 10:37:25 +00:00
|
|
|
) -> None:
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-08-10 14:47:52 +00:00
|
|
|
self.entity_description = description
|
2016-10-22 06:20:15 +00:00
|
|
|
self._device = device
|
2021-08-10 14:47:52 +00:00
|
|
|
|
2024-09-11 14:07:42 +00:00
|
|
|
device_id = device[ATTR_DEVICE_ID]
|
2021-08-10 14:47:52 +00:00
|
|
|
self._attr_unique_id = f"{description.key}_{device_id}"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-26 06:56:23 +00:00
|
|
|
@property
|
2021-10-23 18:42:50 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2018-09-26 06:56:23 +00:00
|
|
|
"""Return information about the device."""
|
2021-10-23 18:42:50 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={
|
2019-07-31 19:25:30 +00:00
|
|
|
(
|
2024-09-11 14:07:42 +00:00
|
|
|
DOMAIN,
|
|
|
|
self._device[ATTR_DEVICE][ATTR_DEVICE_PERMANENT_ID],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-26 06:56:23 +00:00
|
|
|
},
|
2021-10-23 18:42:50 +00:00
|
|
|
manufacturer="Apple",
|
2024-09-11 14:07:42 +00:00
|
|
|
model=self._device[ATTR_DEVICE][ATTR_DEVICE_TYPE],
|
|
|
|
name=self._device[ATTR_DEVICE][ATTR_DEVICE_NAME],
|
|
|
|
sw_version=self._device[ATTR_DEVICE][ATTR_DEVICE_SYSTEM_VERSION],
|
2021-10-23 18:42:50 +00:00
|
|
|
)
|
2018-09-26 06:56:23 +00:00
|
|
|
|
2016-10-22 06:20:15 +00:00
|
|
|
@property
|
2024-01-12 13:43:17 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Return the device state attributes."""
|
2024-09-11 14:07:42 +00:00
|
|
|
device = self._device[ATTR_DEVICE]
|
|
|
|
device_battery = self._device[ATTR_BATTERY]
|
2016-10-22 06:20:15 +00:00
|
|
|
return {
|
2024-09-11 14:07:42 +00:00
|
|
|
"Battery State": device_battery[ATTR_BATTERY_STATE],
|
|
|
|
"Battery Level": device_battery[ATTR_BATTERY_LEVEL],
|
|
|
|
"Device Type": device[ATTR_DEVICE_TYPE],
|
|
|
|
"Device Name": device[ATTR_DEVICE_NAME],
|
|
|
|
"Device Version": device[ATTR_DEVICE_SYSTEM_VERSION],
|
2016-10-22 06:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2024-01-12 13:43:17 +00:00
|
|
|
def icon(self) -> str:
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2024-09-11 14:07:42 +00:00
|
|
|
device_battery = self._device[ATTR_BATTERY]
|
|
|
|
battery_state = device_battery[ATTR_BATTERY_STATE]
|
|
|
|
battery_level = device_battery[ATTR_BATTERY_LEVEL]
|
2017-08-19 13:24:13 +00:00
|
|
|
charging = True
|
|
|
|
icon_state = DEFAULT_ICON_STATE
|
2019-07-31 19:25:30 +00:00
|
|
|
if battery_state in (
|
2024-09-11 14:07:42 +00:00
|
|
|
ATTR_BATTERY_STATE_FULL,
|
|
|
|
ATTR_BATTERY_STATE_UNPLUGGED,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2017-08-19 13:24:13 +00:00
|
|
|
charging = False
|
2019-09-03 15:27:14 +00:00
|
|
|
icon_state = f"{DEFAULT_ICON_STATE}-off"
|
2024-09-11 14:07:42 +00:00
|
|
|
elif battery_state == ATTR_BATTERY_STATE_UNKNOWN:
|
2017-08-19 13:24:13 +00:00
|
|
|
battery_level = None
|
|
|
|
charging = False
|
2019-09-03 15:27:14 +00:00
|
|
|
icon_state = f"{DEFAULT_ICON_LEVEL}-unknown"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2021-08-10 14:47:52 +00:00
|
|
|
if self.entity_description.key == "state":
|
2017-08-19 08:59:54 +00:00
|
|
|
return icon_state
|
2019-07-31 19:25:30 +00:00
|
|
|
return icon_for_battery_level(battery_level=battery_level, charging=charging)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2020-11-14 20:43:49 +00:00
|
|
|
@callback
|
2024-01-12 13:43:17 +00:00
|
|
|
def _update(self, device: dict[str, Any]) -> None:
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Get the latest state of the sensor."""
|
2020-11-14 20:43:49 +00:00
|
|
|
self._device = device
|
2024-09-11 14:07:42 +00:00
|
|
|
self._attr_native_value = self._device[ATTR_BATTERY][
|
2021-08-11 19:17:47 +00:00
|
|
|
self.entity_description.key
|
|
|
|
]
|
2020-11-14 20:43:49 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
2023-09-12 20:22:10 +00:00
|
|
|
"""Handle addition to hass: register to dispatch."""
|
2024-09-11 14:07:42 +00:00
|
|
|
self._attr_native_value = self._device[ATTR_BATTERY][
|
2021-08-11 19:17:47 +00:00
|
|
|
self.entity_description.key
|
|
|
|
]
|
2024-09-11 14:07:42 +00:00
|
|
|
device_id = self._device[ATTR_DEVICE_ID]
|
2020-11-14 20:43:49 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(self.hass, f"{DOMAIN}.{device_id}", self._update)
|
|
|
|
)
|