2018-03-15 01:48:21 +00:00
|
|
|
"""Collection of useful functions for the HomeKit component."""
|
2020-04-21 22:38:43 +00:00
|
|
|
import io
|
2020-05-05 00:03:46 +00:00
|
|
|
import ipaddress
|
2018-03-15 01:48:21 +00:00
|
|
|
import logging
|
2020-05-01 04:05:06 +00:00
|
|
|
import os
|
2020-05-17 20:51:51 +00:00
|
|
|
import re
|
2020-04-21 22:38:43 +00:00
|
|
|
import secrets
|
2020-05-01 04:05:06 +00:00
|
|
|
import socket
|
2018-03-15 01:48:21 +00:00
|
|
|
|
2020-04-21 22:38:43 +00:00
|
|
|
import pyqrcode
|
2018-03-15 01:48:21 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-01-28 11:38:18 +00:00
|
|
|
from homeassistant.components import binary_sensor, media_player, sensor
|
2021-02-24 00:22:23 +00:00
|
|
|
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
|
2021-03-31 16:22:30 +00:00
|
|
|
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
2021-02-24 00:22:23 +00:00
|
|
|
from homeassistant.components.media_player import (
|
|
|
|
DEVICE_CLASS_TV,
|
|
|
|
DOMAIN as MEDIA_PLAYER_DOMAIN,
|
|
|
|
)
|
2021-03-22 04:55:20 +00:00
|
|
|
from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN, SUPPORT_ACTIVITY
|
2018-03-15 01:48:21 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CODE,
|
2021-02-24 00:22:23 +00:00
|
|
|
ATTR_DEVICE_CLASS,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_SUPPORTED_FEATURES,
|
|
|
|
CONF_NAME,
|
2021-02-02 13:30:38 +00:00
|
|
|
CONF_PORT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TYPE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2020-05-01 04:05:06 +00:00
|
|
|
from homeassistant.core import HomeAssistant, split_entity_id
|
2018-03-15 01:48:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-05-01 04:05:06 +00:00
|
|
|
from homeassistant.helpers.storage import STORAGE_DIR
|
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 (
|
2020-05-07 14:55:09 +00:00
|
|
|
AUDIO_CODEC_COPY,
|
|
|
|
AUDIO_CODEC_OPUS,
|
|
|
|
CONF_AUDIO_CODEC,
|
2020-05-05 00:03:46 +00:00
|
|
|
CONF_AUDIO_MAP,
|
|
|
|
CONF_AUDIO_PACKET_SIZE,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_FEATURE,
|
|
|
|
CONF_FEATURE_LIST,
|
2020-05-25 23:05:38 +00:00
|
|
|
CONF_LINKED_BATTERY_CHARGING_SENSOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LINKED_BATTERY_SENSOR,
|
2020-07-31 19:41:36 +00:00
|
|
|
CONF_LINKED_DOORBELL_SENSOR,
|
2020-07-02 17:53:11 +00:00
|
|
|
CONF_LINKED_HUMIDITY_SENSOR,
|
2020-05-25 23:05:38 +00:00
|
|
|
CONF_LINKED_MOTION_SENSOR,
|
2020-10-23 17:18:02 +00:00
|
|
|
CONF_LINKED_OBSTRUCTION_SENSOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LOW_BATTERY_THRESHOLD,
|
2020-05-05 00:03:46 +00:00
|
|
|
CONF_MAX_FPS,
|
|
|
|
CONF_MAX_HEIGHT,
|
|
|
|
CONF_MAX_WIDTH,
|
|
|
|
CONF_STREAM_ADDRESS,
|
2020-07-25 17:12:14 +00:00
|
|
|
CONF_STREAM_COUNT,
|
2020-05-05 00:03:46 +00:00
|
|
|
CONF_STREAM_SOURCE,
|
|
|
|
CONF_SUPPORT_AUDIO,
|
2020-05-07 14:55:09 +00:00
|
|
|
CONF_VIDEO_CODEC,
|
2020-05-05 00:03:46 +00:00
|
|
|
CONF_VIDEO_MAP,
|
|
|
|
CONF_VIDEO_PACKET_SIZE,
|
2020-05-07 14:55:09 +00:00
|
|
|
DEFAULT_AUDIO_CODEC,
|
2020-05-05 00:03:46 +00:00
|
|
|
DEFAULT_AUDIO_MAP,
|
|
|
|
DEFAULT_AUDIO_PACKET_SIZE,
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_LOW_BATTERY_THRESHOLD,
|
2020-05-05 00:03:46 +00:00
|
|
|
DEFAULT_MAX_FPS,
|
|
|
|
DEFAULT_MAX_HEIGHT,
|
|
|
|
DEFAULT_MAX_WIDTH,
|
2020-07-25 17:12:14 +00:00
|
|
|
DEFAULT_STREAM_COUNT,
|
2020-05-11 14:09:48 +00:00
|
|
|
DEFAULT_SUPPORT_AUDIO,
|
2020-05-07 14:55:09 +00:00
|
|
|
DEFAULT_VIDEO_CODEC,
|
2020-05-05 00:03:46 +00:00
|
|
|
DEFAULT_VIDEO_MAP,
|
|
|
|
DEFAULT_VIDEO_PACKET_SIZE,
|
2020-05-01 04:05:06 +00:00
|
|
|
DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
FEATURE_ON_OFF,
|
|
|
|
FEATURE_PLAY_PAUSE,
|
|
|
|
FEATURE_PLAY_STOP,
|
|
|
|
FEATURE_TOGGLE_MUTE,
|
2020-04-21 22:38:43 +00:00
|
|
|
HOMEKIT_PAIRING_QR,
|
|
|
|
HOMEKIT_PAIRING_QR_SECRET,
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_FAUCET,
|
|
|
|
TYPE_OUTLET,
|
|
|
|
TYPE_SHOWER,
|
|
|
|
TYPE_SPRINKLER,
|
|
|
|
TYPE_SWITCH,
|
|
|
|
TYPE_VALVE,
|
2020-05-07 14:55:09 +00:00
|
|
|
VIDEO_CODEC_COPY,
|
|
|
|
VIDEO_CODEC_H264_OMX,
|
|
|
|
VIDEO_CODEC_LIBX264,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-15 01:48:21 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-05-01 04:05:06 +00:00
|
|
|
MAX_PORT = 65535
|
2020-05-07 14:55:09 +00:00
|
|
|
VALID_VIDEO_CODECS = [VIDEO_CODEC_LIBX264, VIDEO_CODEC_H264_OMX, AUDIO_CODEC_COPY]
|
|
|
|
VALID_AUDIO_CODECS = [AUDIO_CODEC_OPUS, VIDEO_CODEC_COPY]
|
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),
|
2020-05-25 23:05:38 +00:00
|
|
|
vol.Optional(CONF_LINKED_BATTERY_CHARGING_SENSOR): cv.entity_domain(
|
|
|
|
binary_sensor.DOMAIN
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
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}
|
|
|
|
)
|
|
|
|
|
2020-05-05 00:03:46 +00:00
|
|
|
CAMERA_SCHEMA = BASIC_INFO_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_STREAM_ADDRESS): vol.All(ipaddress.ip_address, cv.string),
|
|
|
|
vol.Optional(CONF_STREAM_SOURCE): cv.string,
|
2020-05-07 14:55:09 +00:00
|
|
|
vol.Optional(CONF_AUDIO_CODEC, default=DEFAULT_AUDIO_CODEC): vol.In(
|
|
|
|
VALID_AUDIO_CODECS
|
|
|
|
),
|
2020-05-11 14:09:48 +00:00
|
|
|
vol.Optional(CONF_SUPPORT_AUDIO, default=DEFAULT_SUPPORT_AUDIO): cv.boolean,
|
2020-05-05 00:03:46 +00:00
|
|
|
vol.Optional(CONF_MAX_WIDTH, default=DEFAULT_MAX_WIDTH): cv.positive_int,
|
|
|
|
vol.Optional(CONF_MAX_HEIGHT, default=DEFAULT_MAX_HEIGHT): cv.positive_int,
|
|
|
|
vol.Optional(CONF_MAX_FPS, default=DEFAULT_MAX_FPS): cv.positive_int,
|
|
|
|
vol.Optional(CONF_AUDIO_MAP, default=DEFAULT_AUDIO_MAP): cv.string,
|
|
|
|
vol.Optional(CONF_VIDEO_MAP, default=DEFAULT_VIDEO_MAP): cv.string,
|
2020-07-25 17:12:14 +00:00
|
|
|
vol.Optional(CONF_STREAM_COUNT, default=DEFAULT_STREAM_COUNT): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=1, max=10)
|
|
|
|
),
|
2020-05-07 14:55:09 +00:00
|
|
|
vol.Optional(CONF_VIDEO_CODEC, default=DEFAULT_VIDEO_CODEC): vol.In(
|
|
|
|
VALID_VIDEO_CODECS
|
|
|
|
),
|
2020-05-05 00:03:46 +00:00
|
|
|
vol.Optional(
|
|
|
|
CONF_AUDIO_PACKET_SIZE, default=DEFAULT_AUDIO_PACKET_SIZE
|
|
|
|
): cv.positive_int,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_VIDEO_PACKET_SIZE, default=DEFAULT_VIDEO_PACKET_SIZE
|
|
|
|
): cv.positive_int,
|
2020-05-25 23:05:38 +00:00
|
|
|
vol.Optional(CONF_LINKED_MOTION_SENSOR): cv.entity_domain(binary_sensor.DOMAIN),
|
2020-07-31 19:41:36 +00:00
|
|
|
vol.Optional(CONF_LINKED_DOORBELL_SENSOR): cv.entity_domain(
|
|
|
|
binary_sensor.DOMAIN
|
|
|
|
),
|
2020-05-05 00:03:46 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-07-02 17:53:11 +00:00
|
|
|
HUMIDIFIER_SCHEMA = BASIC_INFO_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_LINKED_HUMIDITY_SENSOR): cv.entity_domain(sensor.DOMAIN)}
|
|
|
|
)
|
|
|
|
|
2020-10-23 17:18:02 +00:00
|
|
|
|
|
|
|
COVER_SCHEMA = BASIC_INFO_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_LINKED_OBSTRUCTION_SENSOR): cv.entity_domain(
|
|
|
|
binary_sensor.DOMAIN
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
|
|
|
2020-05-02 21:15:44 +00:00
|
|
|
HOMEKIT_CHAR_TRANSLATIONS = {
|
|
|
|
0: " ", # nul
|
|
|
|
10: " ", # nl
|
|
|
|
13: " ", # cr
|
|
|
|
33: "-", # !
|
|
|
|
34: " ", # "
|
|
|
|
36: "-", # $
|
|
|
|
37: "-", # %
|
|
|
|
40: "-", # (
|
|
|
|
41: "-", # )
|
|
|
|
42: "-", # *
|
|
|
|
43: "-", # +
|
|
|
|
47: "-", # /
|
|
|
|
58: "-", # :
|
|
|
|
59: "-", # ;
|
|
|
|
60: "-", # <
|
|
|
|
61: "-", # =
|
|
|
|
62: "-", # >
|
|
|
|
63: "-", # ?
|
|
|
|
64: "-", # @
|
|
|
|
91: "-", # [
|
|
|
|
92: "-", # \
|
|
|
|
93: "-", # ]
|
|
|
|
94: "-", # ^
|
|
|
|
95: " ", # _
|
|
|
|
96: "-", # `
|
|
|
|
123: "-", # {
|
|
|
|
124: "-", # |
|
|
|
|
125: "-", # }
|
|
|
|
126: "-", # ~
|
|
|
|
127: "-", # del
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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):
|
2020-02-28 11:39:29 +00:00
|
|
|
raise vol.Invalid(f"The configuration for {entity} must be a dictionary.")
|
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-09-03 15:27:14 +00:00
|
|
|
raise vol.Invalid(f"A feature can be added only once for {entity}")
|
2018-05-28 14:26:33 +00:00
|
|
|
feature_list[key] = params
|
|
|
|
config[CONF_FEATURE_LIST] = feature_list
|
|
|
|
|
2020-05-05 00:03:46 +00:00
|
|
|
elif domain == "camera":
|
|
|
|
config = CAMERA_SCHEMA(config)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
elif domain == "switch":
|
2018-06-01 16:04:54 +00:00
|
|
|
config = SWITCH_TYPE_SCHEMA(config)
|
|
|
|
|
2020-07-02 17:53:11 +00:00
|
|
|
elif domain == "humidifier":
|
|
|
|
config = HUMIDIFIER_SCHEMA(config)
|
|
|
|
|
2020-10-23 17:18:02 +00:00
|
|
|
elif domain == "cover":
|
|
|
|
config = COVER_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
|
|
|
|
|
|
|
|
|
2020-05-02 21:15:44 +00:00
|
|
|
def get_media_player_features(state):
|
|
|
|
"""Determine 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)
|
2020-05-02 21:15:44 +00:00
|
|
|
return supported_modes
|
|
|
|
|
|
|
|
|
|
|
|
def validate_media_player_features(state, feature_list):
|
|
|
|
"""Validate features for media players."""
|
|
|
|
supported_modes = get_media_player_features(state)
|
|
|
|
|
|
|
|
if not supported_modes:
|
|
|
|
_LOGGER.error("%s does not support any media_player features", state.entity_id)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not feature_list:
|
|
|
|
# Auto detected
|
|
|
|
return True
|
2018-05-28 14:26:33 +00:00
|
|
|
|
|
|
|
error_list = []
|
|
|
|
for feature in feature_list:
|
|
|
|
if feature not in supported_modes:
|
|
|
|
error_list.append(feature)
|
|
|
|
|
|
|
|
if error_list:
|
2020-05-02 21:15:44 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"%s does not support media_player 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
|
|
|
|
|
|
|
|
2020-05-01 04:05:06 +00:00
|
|
|
def show_setup_message(hass, entry_id, bridge_name, pincode, uri):
|
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)
|
2020-04-21 22:38:43 +00:00
|
|
|
|
|
|
|
buffer = io.BytesIO()
|
|
|
|
url = pyqrcode.create(uri)
|
2020-08-07 03:26:43 +00:00
|
|
|
url.svg(buffer, scale=5, module_color="#000", background="#FFF")
|
2020-04-21 22:38:43 +00:00
|
|
|
pairing_secret = secrets.token_hex(32)
|
|
|
|
|
2020-05-01 04:05:06 +00:00
|
|
|
hass.data[DOMAIN][entry_id][HOMEKIT_PAIRING_QR] = buffer.getvalue()
|
|
|
|
hass.data[DOMAIN][entry_id][HOMEKIT_PAIRING_QR_SECRET] = pairing_secret
|
2020-04-21 22:38:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
message = (
|
2020-05-01 04:05:06 +00:00
|
|
|
f"To set up {bridge_name} in the Home App, "
|
2020-04-21 22:38:43 +00:00
|
|
|
f"scan the QR code or enter the following code:\n"
|
|
|
|
f"### {pin}\n"
|
2020-05-01 04:05:06 +00:00
|
|
|
f"![image](/api/homekit/pairingqr?{entry_id}-{pairing_secret})"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-02-24 00:22:23 +00:00
|
|
|
hass.components.persistent_notification.create(message, "HomeKit Pairing", entry_id)
|
2018-03-15 01:48:21 +00:00
|
|
|
|
|
|
|
|
2020-05-01 04:05:06 +00:00
|
|
|
def dismiss_setup_message(hass, entry_id):
|
2018-03-15 01:48:21 +00:00
|
|
|
"""Dismiss persistent notification and remove QR code."""
|
2020-05-01 04:05:06 +00:00
|
|
|
hass.components.persistent_notification.dismiss(entry_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
|
|
|
|
|
|
|
|
|
2020-05-02 21:15:44 +00:00
|
|
|
def cleanup_name_for_homekit(name):
|
|
|
|
"""Ensure the name of the device will not crash homekit."""
|
|
|
|
#
|
|
|
|
# This is not a security measure.
|
|
|
|
#
|
|
|
|
# UNICODE_EMOJI is also not allowed but that
|
|
|
|
# likely isn't a problem
|
|
|
|
return name.translate(HOMEKIT_CHAR_TRANSLATIONS)
|
|
|
|
|
|
|
|
|
2018-03-27 09:31:18 +00:00
|
|
|
def temperature_to_homekit(temperature, unit):
|
|
|
|
"""Convert temperature to Celsius for HomeKit."""
|
2019-10-04 00:44:07 +00:00
|
|
|
return round(temp_util.convert(temperature, unit, TEMP_CELSIUS), 1)
|
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
|
2020-05-01 04:05:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_persist_filename_for_entry_id(entry_id: str):
|
|
|
|
"""Determine the filename of the homekit state file."""
|
|
|
|
return f"{DOMAIN}.{entry_id}.state"
|
|
|
|
|
|
|
|
|
|
|
|
def get_aid_storage_filename_for_entry_id(entry_id: str):
|
|
|
|
"""Determine the ilename of homekit aid storage file."""
|
|
|
|
return f"{DOMAIN}.{entry_id}.aids"
|
|
|
|
|
|
|
|
|
|
|
|
def get_persist_fullpath_for_entry_id(hass: HomeAssistant, entry_id: str):
|
|
|
|
"""Determine the path to the homekit state file."""
|
|
|
|
return hass.config.path(STORAGE_DIR, get_persist_filename_for_entry_id(entry_id))
|
|
|
|
|
|
|
|
|
|
|
|
def get_aid_storage_fullpath_for_entry_id(hass: HomeAssistant, entry_id: str):
|
|
|
|
"""Determine the path to the homekit aid storage file."""
|
|
|
|
return hass.config.path(
|
|
|
|
STORAGE_DIR, get_aid_storage_filename_for_entry_id(entry_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-17 20:51:51 +00:00
|
|
|
def format_sw_version(version):
|
|
|
|
"""Extract the version string in a format homekit can consume."""
|
|
|
|
match = re.search(r"([0-9]+)(\.[0-9]+)?(\.[0-9]+)?", str(version).replace("-", "."))
|
|
|
|
if match:
|
|
|
|
return match.group(0)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-05-01 04:05:06 +00:00
|
|
|
def remove_state_files_for_entry_id(hass: HomeAssistant, entry_id: str):
|
|
|
|
"""Remove the state files from disk."""
|
|
|
|
persist_file_path = get_persist_fullpath_for_entry_id(hass, entry_id)
|
|
|
|
aid_storage_path = get_aid_storage_fullpath_for_entry_id(hass, entry_id)
|
|
|
|
os.unlink(persist_file_path)
|
|
|
|
if os.path.exists(aid_storage_path):
|
|
|
|
os.unlink(aid_storage_path)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _get_test_socket():
|
|
|
|
"""Create a socket to test binding ports."""
|
|
|
|
test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
test_socket.setblocking(False)
|
|
|
|
test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
return test_socket
|
|
|
|
|
|
|
|
|
2021-02-02 13:30:38 +00:00
|
|
|
def port_is_available(port: int) -> bool:
|
2020-05-01 04:05:06 +00:00
|
|
|
"""Check to see if a port is available."""
|
|
|
|
test_socket = _get_test_socket()
|
|
|
|
try:
|
|
|
|
test_socket.bind(("", port))
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-02-02 13:30:38 +00:00
|
|
|
async def async_find_next_available_port(hass: HomeAssistant, start_port: int) -> int:
|
|
|
|
"""Find the next available port not assigned to a config entry."""
|
2021-03-21 03:08:49 +00:00
|
|
|
exclude_ports = {
|
|
|
|
entry.data[CONF_PORT]
|
|
|
|
for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
if CONF_PORT in entry.data
|
|
|
|
}
|
2021-02-02 13:30:38 +00:00
|
|
|
|
|
|
|
return await hass.async_add_executor_job(
|
|
|
|
_find_next_available_port, start_port, exclude_ports
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _find_next_available_port(start_port: int, exclude_ports: set) -> int:
|
2020-05-01 04:05:06 +00:00
|
|
|
"""Find the next available port starting with the given port."""
|
|
|
|
test_socket = _get_test_socket()
|
|
|
|
for port in range(start_port, MAX_PORT):
|
2021-02-02 13:30:38 +00:00
|
|
|
if port in exclude_ports:
|
|
|
|
continue
|
2020-05-01 04:05:06 +00:00
|
|
|
try:
|
|
|
|
test_socket.bind(("", port))
|
|
|
|
return port
|
|
|
|
except OSError:
|
|
|
|
if port == MAX_PORT:
|
|
|
|
raise
|
|
|
|
continue
|
2020-05-11 13:17:10 +00:00
|
|
|
|
|
|
|
|
2021-02-02 13:30:38 +00:00
|
|
|
def pid_is_alive(pid) -> bool:
|
2020-05-11 13:17:10 +00:00
|
|
|
"""Check to see if a process is alive."""
|
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return False
|
2021-02-24 00:22:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def accessory_friendly_name(hass_name, accessory):
|
|
|
|
"""Return the combined name for the accessory.
|
|
|
|
|
|
|
|
The mDNS name and the Home Assistant config entry
|
|
|
|
name are usually different which means they need to
|
|
|
|
see both to identify the accessory.
|
|
|
|
"""
|
|
|
|
accessory_mdns_name = accessory.display_name
|
2021-03-12 06:05:03 +00:00
|
|
|
if hass_name.casefold().startswith(accessory_mdns_name.casefold()):
|
2021-02-24 00:22:23 +00:00
|
|
|
return hass_name
|
2021-03-12 06:05:03 +00:00
|
|
|
if accessory_mdns_name.casefold().startswith(hass_name.casefold()):
|
|
|
|
return accessory_mdns_name
|
2021-02-24 00:22:23 +00:00
|
|
|
return f"{hass_name} ({accessory_mdns_name})"
|
|
|
|
|
|
|
|
|
|
|
|
def state_needs_accessory_mode(state):
|
|
|
|
"""Return if the entity represented by the state must be paired in accessory mode."""
|
2021-07-22 05:22:06 +00:00
|
|
|
if state.domain in (CAMERA_DOMAIN, LOCK_DOMAIN):
|
2021-02-24 00:22:23 +00:00
|
|
|
return True
|
|
|
|
|
2021-03-21 03:08:49 +00:00
|
|
|
return (
|
2021-07-22 05:22:06 +00:00
|
|
|
state.domain == MEDIA_PLAYER_DOMAIN
|
2021-02-24 00:22:23 +00:00
|
|
|
and state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TV
|
2021-03-22 04:55:20 +00:00
|
|
|
or state.domain == REMOTE_DOMAIN
|
2021-04-13 10:44:07 +00:00
|
|
|
and state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) & SUPPORT_ACTIVITY
|
2021-03-21 03:08:49 +00:00
|
|
|
)
|