From 39ee3fcfaacc2b9e340b397a1c583ecac6bd1424 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 2 Feb 2026 21:47:30 +0100 Subject: [PATCH] Move bond service registration (#162075) --- homeassistant/components/bond/__init__.py | 11 ++- homeassistant/components/bond/const.py | 7 -- homeassistant/components/bond/fan.py | 9 -- homeassistant/components/bond/light.py | 41 --------- homeassistant/components/bond/services.py | 101 ++++++++++++++++++++++ homeassistant/components/bond/switch.py | 9 -- tests/components/bond/test_fan.py | 6 +- tests/components/bond/test_light.py | 6 +- tests/components/bond/test_switch.py | 4 +- 9 files changed, 117 insertions(+), 77 deletions(-) create mode 100644 homeassistant/components/bond/services.py diff --git a/homeassistant/components/bond/__init__.py b/homeassistant/components/bond/__init__.py index 00b8c8a0e13..8ef67fbf3a5 100644 --- a/homeassistant/components/bond/__init__.py +++ b/homeassistant/components/bond/__init__.py @@ -16,14 +16,17 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.entity import SLOW_UPDATE_WARNING +from homeassistant.helpers.typing import ConfigType from .const import BRIDGE_MAKE, DOMAIN from .models import BondData +from .services import async_setup_services from .utils import BondHub +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) PLATFORMS = [ Platform.BUTTON, Platform.COVER, @@ -38,6 +41,12 @@ _LOGGER = logging.getLogger(__name__) type BondConfigEntry = ConfigEntry[BondData] +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the component.""" + async_setup_services(hass) + return True + + async def async_setup_entry(hass: HomeAssistant, entry: BondConfigEntry) -> bool: """Set up Bond from a config entry.""" host = entry.data[CONF_HOST] diff --git a/homeassistant/components/bond/const.py b/homeassistant/components/bond/const.py index 91197763d23..3031c159b0f 100644 --- a/homeassistant/components/bond/const.py +++ b/homeassistant/components/bond/const.py @@ -5,10 +5,3 @@ BRIDGE_MAKE = "Olibra" DOMAIN = "bond" CONF_BOND_ID: str = "bond_id" - - -SERVICE_SET_FAN_SPEED_TRACKED_STATE = "set_fan_speed_tracked_state" -SERVICE_SET_POWER_TRACKED_STATE = "set_switch_power_tracked_state" -SERVICE_SET_LIGHT_POWER_TRACKED_STATE = "set_light_power_tracked_state" -SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE = "set_light_brightness_tracked_state" -ATTR_POWER_STATE = "power_state" diff --git a/homeassistant/components/bond/fan.py b/homeassistant/components/bond/fan.py index c228c7355dd..c1554caa9a4 100644 --- a/homeassistant/components/bond/fan.py +++ b/homeassistant/components/bond/fan.py @@ -8,7 +8,6 @@ from typing import Any from aiohttp.client_exceptions import ClientResponseError from bond_async import Action, DeviceType, Direction -import voluptuous as vol from homeassistant.components.fan import ( DIRECTION_FORWARD, @@ -18,7 +17,6 @@ from homeassistant.components.fan import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util.percentage import ( percentage_to_ranged_value, @@ -27,7 +25,6 @@ from homeassistant.util.percentage import ( from homeassistant.util.scaling import int_states_in_range from . import BondConfigEntry -from .const import SERVICE_SET_FAN_SPEED_TRACKED_STATE from .entity import BondEntity from .models import BondData from .utils import BondDevice @@ -44,12 +41,6 @@ async def async_setup_entry( ) -> None: """Set up Bond fan devices.""" data = entry.runtime_data - platform = entity_platform.async_get_current_platform() - platform.async_register_entity_service( - SERVICE_SET_FAN_SPEED_TRACKED_STATE, - {vol.Required("speed"): vol.All(vol.Number(scale=0), vol.Range(0, 100))}, - "async_set_speed_belief", - ) async_add_entities( BondFan(data, device) diff --git a/homeassistant/components/bond/light.py b/homeassistant/components/bond/light.py index 9c51165ebdb..35524357c8b 100644 --- a/homeassistant/components/bond/light.py +++ b/homeassistant/components/bond/light.py @@ -7,37 +7,20 @@ from typing import Any from aiohttp.client_exceptions import ClientResponseError from bond_async import Action, DeviceType -import voluptuous as vol from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from . import BondConfigEntry -from .const import ( - ATTR_POWER_STATE, - SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE, - SERVICE_SET_LIGHT_POWER_TRACKED_STATE, -) from .entity import BondEntity from .models import BondData from .utils import BondDevice _LOGGER = logging.getLogger(__name__) -SERVICE_START_INCREASING_BRIGHTNESS = "start_increasing_brightness" -SERVICE_START_DECREASING_BRIGHTNESS = "start_decreasing_brightness" -SERVICE_STOP = "stop" - -ENTITY_SERVICES = [ - SERVICE_START_INCREASING_BRIGHTNESS, - SERVICE_START_DECREASING_BRIGHTNESS, - SERVICE_STOP, -] - async def async_setup_entry( hass: HomeAssistant, @@ -48,14 +31,6 @@ async def async_setup_entry( data = entry.runtime_data hub = data.hub - platform = entity_platform.async_get_current_platform() - for service in ENTITY_SERVICES: - platform.async_register_entity_service( - service, - None, - f"async_{service}", - ) - fan_lights: list[Entity] = [ BondLight(data, device) for device in hub.devices @@ -94,22 +69,6 @@ async def async_setup_entry( if DeviceType.is_light(device.type) ] - platform.async_register_entity_service( - SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE, - { - vol.Required(ATTR_BRIGHTNESS): vol.All( - vol.Number(scale=0), vol.Range(0, 255) - ) - }, - "async_set_brightness_belief", - ) - - platform.async_register_entity_service( - SERVICE_SET_LIGHT_POWER_TRACKED_STATE, - {vol.Required(ATTR_POWER_STATE): vol.All(cv.boolean)}, - "async_set_power_belief", - ) - async_add_entities( fan_lights + fan_up_lights + fan_down_lights + fireplaces + fp_lights + lights, ) diff --git a/homeassistant/components/bond/services.py b/homeassistant/components/bond/services.py new file mode 100644 index 00000000000..7974f6afde5 --- /dev/null +++ b/homeassistant/components/bond/services.py @@ -0,0 +1,101 @@ +"""Support for Bond services.""" + +from __future__ import annotations + +import voluptuous as vol + +from homeassistant.components.fan import DOMAIN as FAN_DOMAIN +from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv, service + +from .const import DOMAIN + +ATTR_POWER_STATE = "power_state" + +# Fan +SERVICE_SET_FAN_SPEED_TRACKED_STATE = "set_fan_speed_tracked_state" + +# Switch +SERVICE_SET_POWER_TRACKED_STATE = "set_switch_power_tracked_state" + +# Light +SERVICE_SET_LIGHT_POWER_TRACKED_STATE = "set_light_power_tracked_state" +SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE = "set_light_brightness_tracked_state" +SERVICE_START_INCREASING_BRIGHTNESS = "start_increasing_brightness" +SERVICE_START_DECREASING_BRIGHTNESS = "start_decreasing_brightness" +SERVICE_STOP = "stop" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Home Assistant services.""" + + # Fan entity services + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_FAN_SPEED_TRACKED_STATE, + entity_domain=FAN_DOMAIN, + schema={vol.Required("speed"): vol.All(vol.Number(scale=0), vol.Range(0, 100))}, + func="async_set_speed_belief", + ) + + # Light entity services + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_START_INCREASING_BRIGHTNESS, + entity_domain=LIGHT_DOMAIN, + schema=None, + func="async_start_increasing_brightness", + ) + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_START_DECREASING_BRIGHTNESS, + entity_domain=LIGHT_DOMAIN, + schema=None, + func="async_start_decreasing_brightness", + ) + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_STOP, + entity_domain=LIGHT_DOMAIN, + schema=None, + func="async_stop", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE, + entity_domain=LIGHT_DOMAIN, + schema={ + vol.Required(ATTR_BRIGHTNESS): vol.All( + vol.Number(scale=0), vol.Range(0, 255) + ) + }, + func="async_set_brightness_belief", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_LIGHT_POWER_TRACKED_STATE, + entity_domain=LIGHT_DOMAIN, + schema={vol.Required(ATTR_POWER_STATE): vol.All(cv.boolean)}, + func="async_set_power_belief", + ) + + # Switch entity services + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_POWER_TRACKED_STATE, + entity_domain=SWITCH_DOMAIN, + schema={vol.Required(ATTR_POWER_STATE): cv.boolean}, + func="async_set_power_belief", + ) diff --git a/homeassistant/components/bond/switch.py b/homeassistant/components/bond/switch.py index fa2ccd2ca93..9274e4724ff 100644 --- a/homeassistant/components/bond/switch.py +++ b/homeassistant/components/bond/switch.py @@ -6,16 +6,13 @@ from typing import Any from aiohttp.client_exceptions import ClientResponseError from bond_async import Action, DeviceType -import voluptuous as vol from homeassistant.components.switch import SwitchEntity from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from . import BondConfigEntry -from .const import ATTR_POWER_STATE, SERVICE_SET_POWER_TRACKED_STATE from .entity import BondEntity @@ -26,12 +23,6 @@ async def async_setup_entry( ) -> None: """Set up Bond generic devices.""" data = entry.runtime_data - platform = entity_platform.async_get_current_platform() - platform.async_register_entity_service( - SERVICE_SET_POWER_TRACKED_STATE, - {vol.Required(ATTR_POWER_STATE): cv.boolean}, - "async_set_power_belief", - ) async_add_entities( BondSwitch(data, device) diff --git a/tests/components/bond/test_fan.py b/tests/components/bond/test_fan.py index ac38a93a386..b0128e01fed 100644 --- a/tests/components/bond/test_fan.py +++ b/tests/components/bond/test_fan.py @@ -10,11 +10,9 @@ import pytest from homeassistant import core from homeassistant.components import fan -from homeassistant.components.bond.const import ( - DOMAIN, - SERVICE_SET_FAN_SPEED_TRACKED_STATE, -) +from homeassistant.components.bond.const import DOMAIN from homeassistant.components.bond.fan import PRESET_MODE_BREEZE +from homeassistant.components.bond.services import SERVICE_SET_FAN_SPEED_TRACKED_STATE from homeassistant.components.fan import ( ATTR_DIRECTION, ATTR_PERCENTAGE, diff --git a/tests/components/bond/test_light.py b/tests/components/bond/test_light.py index ce245c838ba..76690d18b92 100644 --- a/tests/components/bond/test_light.py +++ b/tests/components/bond/test_light.py @@ -5,13 +5,11 @@ from datetime import timedelta from bond_async import Action, DeviceType import pytest -from homeassistant.components.bond.const import ( +from homeassistant.components.bond.const import DOMAIN +from homeassistant.components.bond.services import ( ATTR_POWER_STATE, - DOMAIN, SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE, SERVICE_SET_LIGHT_POWER_TRACKED_STATE, -) -from homeassistant.components.bond.light import ( SERVICE_START_DECREASING_BRIGHTNESS, SERVICE_START_INCREASING_BRIGHTNESS, SERVICE_STOP, diff --git a/tests/components/bond/test_switch.py b/tests/components/bond/test_switch.py index 2389f751843..515bff3b4d3 100644 --- a/tests/components/bond/test_switch.py +++ b/tests/components/bond/test_switch.py @@ -5,9 +5,9 @@ from datetime import timedelta from bond_async import Action, DeviceType import pytest -from homeassistant.components.bond.const import ( +from homeassistant.components.bond.const import DOMAIN +from homeassistant.components.bond.services import ( ATTR_POWER_STATE, - DOMAIN, SERVICE_SET_POWER_TRACKED_STATE, ) from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN