2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Home Assistant iOS app sensors."""
|
2016-10-22 06:20:15 +00:00
|
|
|
from homeassistant.components import ios
|
2020-02-28 19:46:48 +00:00
|
|
|
from homeassistant.const import UNIT_PERCENTAGE
|
2016-10-22 06:20:15 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-10-06 03:55:19 +00:00
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2020-02-28 19:46:48 +00:00
|
|
|
SENSOR_TYPES = {
|
|
|
|
"level": ["Battery Level", UNIT_PERCENTAGE],
|
|
|
|
"state": ["Battery State", None],
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=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
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up iOS from a config entry."""
|
2020-04-04 21:14:47 +00:00
|
|
|
dev = []
|
2018-09-12 18:17:52 +00:00
|
|
|
for device_name, device in ios.devices(hass).items():
|
2019-07-31 19:25:30 +00:00
|
|
|
for sensor_type in ("level", "state"):
|
2016-10-22 06:20:15 +00:00
|
|
|
dev.append(IOSSensor(sensor_type, device_name, device))
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
async_add_entities(dev, True)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IOSSensor(Entity):
|
|
|
|
"""Representation of an iOS sensor."""
|
|
|
|
|
|
|
|
def __init__(self, sensor_type, device_name, device):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._device_name = device_name
|
2020-02-28 11:39:29 +00:00
|
|
|
self._name = f"{device_name} {SENSOR_TYPES[sensor_type][0]}"
|
2016-10-22 06:20:15 +00:00
|
|
|
self._device = device
|
|
|
|
self.type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
|
2018-09-26 06:56:23 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return information about the device."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"identifiers": {
|
|
|
|
(
|
|
|
|
ios.DOMAIN,
|
|
|
|
self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_PERMANENT_ID],
|
|
|
|
)
|
2018-09-26 06:56:23 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"name": self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_NAME],
|
|
|
|
"manufacturer": "Apple",
|
|
|
|
"model": self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_TYPE],
|
|
|
|
"sw_version": self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_SYSTEM_VERSION],
|
2018-09-26 06:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 06:20:15 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the iOS sensor."""
|
|
|
|
device_name = self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_NAME]
|
2020-02-28 11:39:29 +00:00
|
|
|
return f"{device_name} {SENSOR_TYPES[self.type][0]}"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique ID of this sensor."""
|
2017-04-18 05:58:04 +00:00
|
|
|
device_id = self._device[ios.ATTR_DEVICE_ID]
|
2019-09-03 15:27:14 +00:00
|
|
|
return f"{self.type}_{device_id}"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement this sensor expresses itself in."""
|
2017-04-28 10:26:17 +00:00
|
|
|
return self._unit_of_measurement
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
device = self._device[ios.ATTR_DEVICE]
|
|
|
|
device_battery = self._device[ios.ATTR_BATTERY]
|
|
|
|
return {
|
|
|
|
"Battery State": device_battery[ios.ATTR_BATTERY_STATE],
|
|
|
|
"Battery Level": device_battery[ios.ATTR_BATTERY_LEVEL],
|
|
|
|
"Device Type": device[ios.ATTR_DEVICE_TYPE],
|
|
|
|
"Device Name": device[ios.ATTR_DEVICE_NAME],
|
|
|
|
"Device Version": device[ios.ATTR_DEVICE_SYSTEM_VERSION],
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
device_battery = self._device[ios.ATTR_BATTERY]
|
|
|
|
battery_state = device_battery[ios.ATTR_BATTERY_STATE]
|
|
|
|
battery_level = device_battery[ios.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 (
|
|
|
|
ios.ATTR_BATTERY_STATE_FULL,
|
|
|
|
ios.ATTR_BATTERY_STATE_UNPLUGGED,
|
|
|
|
):
|
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"
|
2016-10-22 06:20:15 +00:00
|
|
|
elif battery_state == ios.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
|
|
|
|
2017-04-22 03:16:59 +00:00
|
|
|
if self.type == "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
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest state of the sensor."""
|
2018-09-12 18:17:52 +00:00
|
|
|
self._device = ios.devices(self.hass).get(self._device_name)
|
2016-10-22 06:20:15 +00:00
|
|
|
self._state = self._device[ios.ATTR_BATTERY][self.type]
|