diff --git a/homeassistant/components/kef/__init__.py b/homeassistant/components/kef/__init__.py index a55c8ca32109..b37b914e03ff 100644 --- a/homeassistant/components/kef/__init__.py +++ b/homeassistant/components/kef/__init__.py @@ -1 +1,16 @@ """The KEF Wireless Speakers component.""" + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.typing import ConfigType + +from .const import DOMAIN +from .services import async_setup_services + +CONFIG_SCHEMA = cv.platform_only_config_schema(DOMAIN) + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the KEF Wireless Speakers component.""" + async_setup_services(hass) + return True diff --git a/homeassistant/components/kef/const.py b/homeassistant/components/kef/const.py new file mode 100644 index 000000000000..52ce96fb69a3 --- /dev/null +++ b/homeassistant/components/kef/const.py @@ -0,0 +1,3 @@ +"""Constants for the KEF Wireless Speakers integration.""" + +DOMAIN = "kef" diff --git a/homeassistant/components/kef/media_player.py b/homeassistant/components/kef/media_player.py index ccb4b2b78b94..041089f5765d 100644 --- a/homeassistant/components/kef/media_player.py +++ b/homeassistant/components/kef/media_player.py @@ -7,7 +7,6 @@ import logging from typing import Any, override from aiokef import AsyncKefSpeaker -from aiokef.aiokef import DSP_OPTION_MAPPING from getmac import get_mac_address import probatio @@ -20,11 +19,13 @@ from homeassistant.components.media_player import ( from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_TYPE from homeassistant.core import HomeAssistant from homeassistant.exceptions import PlatformNotReady -from homeassistant.helpers import config_validation as cv, entity_platform +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .const import DOMAIN + _LOGGER = logging.getLogger(__name__) DEFAULT_NAME = "KEF" @@ -34,8 +35,6 @@ DEFAULT_VOLUME_STEP = 0.05 DEFAULT_INVERSE_SPEAKER_MODE = False DEFAULT_SUPPORTS_ON = True -DOMAIN = "kef" - SCAN_INTERVAL = timedelta(seconds=30) SOURCES = {"LSX": ["Wifi", "Bluetooth", "Aux", "Opt"]} @@ -47,15 +46,6 @@ CONF_INVERSE_SPEAKER_MODE = "inverse_speaker_mode" CONF_SUPPORTS_ON = "supports_on" CONF_STANDBY_TIME = "standby_time" -SERVICE_MODE = "set_mode" -SERVICE_DESK_DB = "set_desk_db" -SERVICE_WALL_DB = "set_wall_db" -SERVICE_TREBLE_DB = "set_treble_db" -SERVICE_HIGH_HZ = "set_high_hz" -SERVICE_LOW_HZ = "set_low_hz" -SERVICE_SUB_DB = "set_sub_db" -SERVICE_UPDATE_DSP = "update_dsp" - DSP_SCAN_INTERVAL = timedelta(seconds=3600) PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend( @@ -148,44 +138,6 @@ async def async_setup_platform( hass.data[DOMAIN][host] = media_player async_add_entities([media_player], update_before_add=True) - platform = entity_platform.async_get_current_platform() - - platform.async_register_entity_service( - SERVICE_MODE, - { - probatio.Optional("desk_mode"): cv.boolean, - probatio.Optional("wall_mode"): cv.boolean, - probatio.Optional("phase_correction"): cv.boolean, - probatio.Optional("high_pass"): cv.boolean, - probatio.Optional("sub_polarity"): probatio.In(["-", "+"]), - probatio.Optional("bass_extension"): probatio.In( - ["Less", "Standard", "Extra"] - ), - }, - "set_mode", - ) - platform.async_register_entity_service(SERVICE_UPDATE_DSP, None, "update_dsp") - - def add_service(name, which, option): - options = DSP_OPTION_MAPPING[which] - dtype = type(options[0]) # int or float - platform.async_register_entity_service( - name, - { - probatio.Required(option): probatio.All( - probatio.Coerce(float), probatio.Coerce(dtype), probatio.In(options) - ) - }, - f"set_{which}", - ) - - add_service(SERVICE_DESK_DB, "desk_db", "db_value") - add_service(SERVICE_WALL_DB, "wall_db", "db_value") - add_service(SERVICE_TREBLE_DB, "treble_db", "db_value") - add_service(SERVICE_HIGH_HZ, "high_hz", "hz_value") - add_service(SERVICE_LOW_HZ, "low_hz", "hz_value") - add_service(SERVICE_SUB_DB, "sub_db", "db_value") - class KefMediaPlayer(MediaPlayerEntity): """Kef Player Object.""" diff --git a/homeassistant/components/kef/services.py b/homeassistant/components/kef/services.py new file mode 100644 index 000000000000..06d1b3a41f9d --- /dev/null +++ b/homeassistant/components/kef/services.py @@ -0,0 +1,72 @@ +"""Services for the KEF Wireless Speakers integration.""" + +from aiokef.aiokef import DSP_OPTION_MAPPING +import probatio + +from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv, service + +from .const import DOMAIN + +SERVICE_MODE = "set_mode" +SERVICE_DESK_DB = "set_desk_db" +SERVICE_WALL_DB = "set_wall_db" +SERVICE_TREBLE_DB = "set_treble_db" +SERVICE_HIGH_HZ = "set_high_hz" +SERVICE_LOW_HZ = "set_low_hz" +SERVICE_SUB_DB = "set_sub_db" +SERVICE_UPDATE_DSP = "update_dsp" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the KEF Wireless Speakers integration.""" + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_MODE, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema={ + probatio.Optional("desk_mode"): cv.boolean, + probatio.Optional("wall_mode"): cv.boolean, + probatio.Optional("phase_correction"): cv.boolean, + probatio.Optional("high_pass"): cv.boolean, + probatio.Optional("sub_polarity"): probatio.In(["-", "+"]), + probatio.Optional("bass_extension"): probatio.In( + ["Less", "Standard", "Extra"] + ), + }, + func="set_mode", + ) + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_UPDATE_DSP, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema=None, + func="update_dsp", + ) + + for name, which, option in ( + (SERVICE_DESK_DB, "desk_db", "db_value"), + (SERVICE_WALL_DB, "wall_db", "db_value"), + (SERVICE_TREBLE_DB, "treble_db", "db_value"), + (SERVICE_HIGH_HZ, "high_hz", "hz_value"), + (SERVICE_LOW_HZ, "low_hz", "hz_value"), + (SERVICE_SUB_DB, "sub_db", "db_value"), + ): + options = DSP_OPTION_MAPPING[which] + dtype = type(options[0]) # int or float + service.async_register_platform_entity_service( + hass, + DOMAIN, + name, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema={ + probatio.Required(option): probatio.All( + probatio.Coerce(float), probatio.Coerce(dtype), probatio.In(options) + ) + }, + func=f"set_{which}", + )