Improve stability of homekit media players (#35080)

This commit is contained in:
J. Nick Koston 2020-05-02 16:15:44 -05:00 committed by GitHub
parent ab08c1bef8
commit 4de30ca2ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 35 deletions

View file

@ -101,6 +101,40 @@ SWITCH_TYPE_SCHEMA = BASIC_INFO_SCHEMA.extend(
)
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
}
def validate_entity_config(values):
"""Validate config entry for CONF_ENTITY."""
if not isinstance(values, dict):
@ -138,8 +172,8 @@ def validate_entity_config(values):
return entities
def validate_media_player_features(state, feature_list):
"""Validate features for media players."""
def get_media_player_features(state):
"""Determine features for media players."""
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
supported_modes = []
@ -153,6 +187,20 @@ def validate_media_player_features(state, feature_list):
supported_modes.append(FEATURE_PLAY_STOP)
if features & media_player.const.SUPPORT_VOLUME_MUTE:
supported_modes.append(FEATURE_TOGGLE_MUTE)
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
error_list = []
for feature in feature_list:
@ -160,7 +208,9 @@ def validate_media_player_features(state, feature_list):
error_list.append(feature)
if error_list:
_LOGGER.error("%s does not support features: %s", state.entity_id, error_list)
_LOGGER.error(
"%s does not support media_player features: %s", state.entity_id, error_list
)
return False
return True
@ -252,6 +302,16 @@ def convert_to_float(state):
return None
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)
def temperature_to_homekit(temperature, unit):
"""Convert temperature to Celsius for HomeKit."""
return round(temp_util.convert(temperature, unit, TEMP_CELSIUS), 1)