2018-03-15 01:48:21 +00:00
|
|
|
"""Collection of useful functions for the HomeKit component."""
|
2019-02-14 15:01:46 +00:00
|
|
|
from collections import OrderedDict, namedtuple
|
2018-03-15 01:48:21 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-04-09 21:13:48 +00:00
|
|
|
from homeassistant.components import fan, media_player, sensor
|
2018-03-15 01:48:21 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CODE,
|
|
|
|
ATTR_SUPPORTED_FEATURES,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_TYPE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2019-02-05 15:11:19 +00:00
|
|
|
from homeassistant.core import split_entity_id
|
2018-03-15 01:48:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-03-27 09:31:18 +00:00
|
|
|
import homeassistant.util.temperature as temp_util
|
2019-02-05 15:11:19 +00:00
|
|
|
|
2018-05-25 09:37:20 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_FEATURE,
|
|
|
|
CONF_FEATURE_LIST,
|
|
|
|
CONF_LINKED_BATTERY_SENSOR,
|
|
|
|
CONF_LOW_BATTERY_THRESHOLD,
|
|
|
|
DEFAULT_LOW_BATTERY_THRESHOLD,
|
|
|
|
FEATURE_ON_OFF,
|
|
|
|
FEATURE_PLAY_PAUSE,
|
|
|
|
FEATURE_PLAY_STOP,
|
|
|
|
FEATURE_TOGGLE_MUTE,
|
|
|
|
HOMEKIT_NOTIFY_ID,
|
|
|
|
TYPE_FAUCET,
|
|
|
|
TYPE_OUTLET,
|
|
|
|
TYPE_SHOWER,
|
|
|
|
TYPE_SPRINKLER,
|
|
|
|
TYPE_SWITCH,
|
|
|
|
TYPE_VALVE,
|
|
|
|
)
|
2018-03-15 01:48:21 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-05-28 14:26:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
BASIC_INFO_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_LINKED_BATTERY_SENSOR): cv.entity_domain(sensor.DOMAIN),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LOW_BATTERY_THRESHOLD, default=DEFAULT_LOW_BATTERY_THRESHOLD
|
|
|
|
): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
FEATURE_SCHEMA = BASIC_INFO_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_FEATURE_LIST, default=None): cv.ensure_list}
|
|
|
|
)
|
|
|
|
|
|
|
|
CODE_SCHEMA = BASIC_INFO_SCHEMA.extend(
|
|
|
|
{vol.Optional(ATTR_CODE, default=None): vol.Any(None, cv.string)}
|
|
|
|
)
|
|
|
|
|
|
|
|
MEDIA_PLAYER_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_FEATURE): vol.All(
|
|
|
|
cv.string,
|
|
|
|
vol.In(
|
|
|
|
(
|
|
|
|
FEATURE_ON_OFF,
|
|
|
|
FEATURE_PLAY_PAUSE,
|
|
|
|
FEATURE_PLAY_STOP,
|
|
|
|
FEATURE_TOGGLE_MUTE,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
SWITCH_TYPE_SCHEMA = BASIC_INFO_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_TYPE, default=TYPE_SWITCH): vol.All(
|
|
|
|
cv.string,
|
|
|
|
vol.In(
|
|
|
|
(
|
|
|
|
TYPE_FAUCET,
|
|
|
|
TYPE_OUTLET,
|
|
|
|
TYPE_SHOWER,
|
|
|
|
TYPE_SPRINKLER,
|
|
|
|
TYPE_SWITCH,
|
|
|
|
TYPE_VALVE,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-06-01 16:04:54 +00:00
|
|
|
|
2018-03-15 01:48:21 +00:00
|
|
|
|
|
|
|
def validate_entity_config(values):
|
|
|
|
"""Validate config entry for CONF_ENTITY."""
|
2018-10-04 18:37:04 +00:00
|
|
|
if not isinstance(values, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid("expected a dictionary")
|
2018-10-04 18:37:04 +00:00
|
|
|
|
2018-03-15 01:48:21 +00:00
|
|
|
entities = {}
|
2018-05-11 12:22:45 +00:00
|
|
|
for entity_id, config in values.items():
|
|
|
|
entity = cv.entity_id(entity_id)
|
2018-05-28 14:26:33 +00:00
|
|
|
domain, _ = split_entity_id(entity)
|
|
|
|
|
2018-03-15 01:48:21 +00:00
|
|
|
if not isinstance(config, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid(
|
|
|
|
"The configuration for {} must be " " a dictionary.".format(entity)
|
|
|
|
)
|
2018-03-15 01:48:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if domain in ("alarm_control_panel", "lock"):
|
2018-05-28 14:26:33 +00:00
|
|
|
config = CODE_SCHEMA(config)
|
|
|
|
|
2019-02-08 22:18:18 +00:00
|
|
|
elif domain == media_player.const.DOMAIN:
|
2018-05-28 14:26:33 +00:00
|
|
|
config = FEATURE_SCHEMA(config)
|
|
|
|
feature_list = {}
|
|
|
|
for feature in config[CONF_FEATURE_LIST]:
|
|
|
|
params = MEDIA_PLAYER_SCHEMA(feature)
|
|
|
|
key = params.pop(CONF_FEATURE)
|
|
|
|
if key in feature_list:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise vol.Invalid(
|
|
|
|
"A feature can be added only once for {}".format(entity)
|
|
|
|
)
|
2018-05-28 14:26:33 +00:00
|
|
|
feature_list[key] = params
|
|
|
|
config[CONF_FEATURE_LIST] = feature_list
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
elif domain == "switch":
|
2018-06-01 16:04:54 +00:00
|
|
|
config = SWITCH_TYPE_SCHEMA(config)
|
|
|
|
|
2018-05-28 14:26:33 +00:00
|
|
|
else:
|
|
|
|
config = BASIC_INFO_SCHEMA(config)
|
|
|
|
|
|
|
|
entities[entity] = config
|
2018-03-15 01:48:21 +00:00
|
|
|
return entities
|
|
|
|
|
|
|
|
|
2018-05-28 14:26:33 +00:00
|
|
|
def validate_media_player_features(state, feature_list):
|
|
|
|
"""Validate features for media players."""
|
2018-05-25 09:37:20 +00:00
|
|
|
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
|
|
|
|
|
|
|
|
supported_modes = []
|
2019-07-31 19:25:30 +00:00
|
|
|
if features & (
|
|
|
|
media_player.const.SUPPORT_TURN_ON | media_player.const.SUPPORT_TURN_OFF
|
|
|
|
):
|
2018-05-28 14:26:33 +00:00
|
|
|
supported_modes.append(FEATURE_ON_OFF)
|
2019-07-31 19:25:30 +00:00
|
|
|
if features & (media_player.const.SUPPORT_PLAY | media_player.const.SUPPORT_PAUSE):
|
2018-05-28 14:26:33 +00:00
|
|
|
supported_modes.append(FEATURE_PLAY_PAUSE)
|
2019-07-31 19:25:30 +00:00
|
|
|
if features & (media_player.const.SUPPORT_PLAY | media_player.const.SUPPORT_STOP):
|
2018-05-28 14:26:33 +00:00
|
|
|
supported_modes.append(FEATURE_PLAY_STOP)
|
2019-02-08 22:18:18 +00:00
|
|
|
if features & media_player.const.SUPPORT_VOLUME_MUTE:
|
2018-05-28 14:26:33 +00:00
|
|
|
supported_modes.append(FEATURE_TOGGLE_MUTE)
|
|
|
|
|
|
|
|
error_list = []
|
|
|
|
for feature in feature_list:
|
|
|
|
if feature not in supported_modes:
|
|
|
|
error_list.append(feature)
|
|
|
|
|
|
|
|
if error_list:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("%s does not support features: %s", state.entity_id, error_list)
|
2018-05-28 14:26:33 +00:00
|
|
|
return False
|
|
|
|
return True
|
2018-05-25 09:37:20 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SpeedRange = namedtuple("SpeedRange", ("start", "target"))
|
2019-02-05 15:11:19 +00:00
|
|
|
SpeedRange.__doc__ += """ Maps Home Assistant speed \
|
|
|
|
values to percentage based HomeKit speeds.
|
|
|
|
start: Start of the range (inclusive).
|
|
|
|
target: Percentage to use to determine HomeKit percentages \
|
|
|
|
from HomeAssistant speed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class HomeKitSpeedMapping:
|
|
|
|
"""Supports conversion between Home Assistant and HomeKit fan speeds."""
|
|
|
|
|
|
|
|
def __init__(self, speed_list):
|
|
|
|
"""Initialize a new SpeedMapping object."""
|
|
|
|
if speed_list[0] != fan.SPEED_OFF:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"%s does not contain the speed setting "
|
|
|
|
"%s as its first element. "
|
|
|
|
"Assuming that %s is equivalent to 'off'.",
|
|
|
|
speed_list,
|
|
|
|
fan.SPEED_OFF,
|
|
|
|
speed_list[0],
|
|
|
|
)
|
2019-02-05 15:11:19 +00:00
|
|
|
self.speed_ranges = OrderedDict()
|
|
|
|
list_size = len(speed_list)
|
|
|
|
for index, speed in enumerate(speed_list):
|
|
|
|
# By dividing by list_size -1 the following
|
|
|
|
# desired attributes hold true:
|
|
|
|
# * index = 0 => 0%, equal to "off"
|
|
|
|
# * index = len(speed_list) - 1 => 100 %
|
|
|
|
# * all other indices are equally distributed
|
|
|
|
target = index * 100 / (list_size - 1)
|
|
|
|
start = index * 100 / list_size
|
|
|
|
self.speed_ranges[speed] = SpeedRange(start, target)
|
|
|
|
|
|
|
|
def speed_to_homekit(self, speed):
|
|
|
|
"""Map Home Assistant speed state to HomeKit speed."""
|
2019-04-10 06:35:17 +00:00
|
|
|
if speed is None:
|
|
|
|
return None
|
2019-02-05 15:11:19 +00:00
|
|
|
speed_range = self.speed_ranges[speed]
|
|
|
|
return speed_range.target
|
|
|
|
|
|
|
|
def speed_to_states(self, speed):
|
|
|
|
"""Map HomeKit speed to Home Assistant speed state."""
|
|
|
|
for state, speed_range in reversed(self.speed_ranges.items()):
|
|
|
|
if speed_range.start <= speed:
|
|
|
|
return state
|
|
|
|
return list(self.speed_ranges.keys())[0]
|
|
|
|
|
|
|
|
|
2018-05-18 14:32:57 +00:00
|
|
|
def show_setup_message(hass, pincode):
|
2018-03-15 01:48:21 +00:00
|
|
|
"""Display persistent notification with setup information."""
|
2018-05-18 14:32:57 +00:00
|
|
|
pin = pincode.decode()
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info("Pincode: %s", pin)
|
|
|
|
message = (
|
|
|
|
"To set up Home Assistant in the Home App, enter the "
|
|
|
|
"following code:\n### {}".format(pin)
|
|
|
|
)
|
2018-03-15 01:48:21 +00:00
|
|
|
hass.components.persistent_notification.create(
|
2019-07-31 19:25:30 +00:00
|
|
|
message, "HomeKit Setup", HOMEKIT_NOTIFY_ID
|
|
|
|
)
|
2018-03-15 01:48:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dismiss_setup_message(hass):
|
|
|
|
"""Dismiss persistent notification and remove QR code."""
|
|
|
|
hass.components.persistent_notification.dismiss(HOMEKIT_NOTIFY_ID)
|
2018-03-27 09:31:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def convert_to_float(state):
|
|
|
|
"""Return float of state, catch errors."""
|
|
|
|
try:
|
|
|
|
return float(state)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def temperature_to_homekit(temperature, unit):
|
|
|
|
"""Convert temperature to Celsius for HomeKit."""
|
2018-11-04 21:04:51 +00:00
|
|
|
return round(temp_util.convert(temperature, unit, TEMP_CELSIUS) * 2) / 2
|
2018-03-27 09:31:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def temperature_to_states(temperature, unit):
|
|
|
|
"""Convert temperature back from Celsius to Home Assistant unit."""
|
2018-11-04 21:04:51 +00:00
|
|
|
return round(temp_util.convert(temperature, TEMP_CELSIUS, unit) * 2) / 2
|
2018-04-12 13:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def density_to_air_quality(density):
|
|
|
|
"""Map PM2.5 density to HomeKit AirQuality level."""
|
|
|
|
if density <= 35:
|
|
|
|
return 1
|
2018-07-23 08:16:05 +00:00
|
|
|
if density <= 75:
|
2018-04-12 13:01:41 +00:00
|
|
|
return 2
|
2018-07-23 08:16:05 +00:00
|
|
|
if density <= 115:
|
2018-04-12 13:01:41 +00:00
|
|
|
return 3
|
2018-07-23 08:16:05 +00:00
|
|
|
if density <= 150:
|
2018-04-12 13:01:41 +00:00
|
|
|
return 4
|
|
|
|
return 5
|