diff --git a/homeassistant/components/bond/button.py b/homeassistant/components/bond/button.py index 4e243198e5e..a2d88bc6f6a 100644 --- a/homeassistant/components/bond/button.py +++ b/homeassistant/components/bond/button.py @@ -4,7 +4,7 @@ from __future__ import annotations from dataclasses import dataclass -from bond_async import Action, BPUPSubscriptions +from bond_async import Action from homeassistant.components.button import ButtonEntity, ButtonEntityDescription from homeassistant.core import HomeAssistant @@ -12,7 +12,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import BondConfigEntry from .entity import BondEntity -from .utils import BondDevice, BondHub +from .models import BondData +from .utils import BondDevice # The api requires a step size even though it does not # seem to matter what is is as the underlying device is likely @@ -246,13 +247,11 @@ async def async_setup_entry( ) -> None: """Set up Bond button devices.""" data = entry.runtime_data - hub = data.hub - bpup_subs = data.bpup_subs entities: list[BondButtonEntity] = [] - for device in hub.devices: + for device in data.hub.devices: device_entities = [ - BondButtonEntity(hub, device, bpup_subs, description) + BondButtonEntity(data, device, description) for description in BUTTONS if device.has_action(description.key) and ( @@ -264,9 +263,7 @@ async def async_setup_entry( # Most devices have the stop action available, but # we only add the stop action button if we add actions # since its not so useful if there are no actions to stop - device_entities.append( - BondButtonEntity(hub, device, bpup_subs, STOP_BUTTON) - ) + device_entities.append(BondButtonEntity(data, device, STOP_BUTTON)) entities.extend(device_entities) async_add_entities(entities) @@ -279,26 +276,23 @@ class BondButtonEntity(BondEntity, ButtonEntity): def __init__( self, - hub: BondHub, + data: BondData, device: BondDevice, - bpup_subs: BPUPSubscriptions, description: BondButtonEntityDescription, ) -> None: """Init Bond button.""" self.entity_description = description - super().__init__( - hub, device, bpup_subs, description.name, description.key.lower() - ) + super().__init__(data, device, description.name, description.key.lower()) async def async_press(self) -> None: """Press the button.""" - if self.entity_description.argument: - action = Action( - self.entity_description.key, self.entity_description.argument - ) + description = self.entity_description + key = description.key + if argument := description.argument: + action = Action(key, argument) else: - action = Action(self.entity_description.key) - await self._hub.bond.action(self._device.device_id, action) + action = Action(key) + await self._bond.action(self._device_id, action) def _apply_state(self) -> None: """Apply the state.""" diff --git a/homeassistant/components/bond/cover.py b/homeassistant/components/bond/cover.py index c576972bf26..66344a1913d 100644 --- a/homeassistant/components/bond/cover.py +++ b/homeassistant/components/bond/cover.py @@ -4,7 +4,7 @@ from __future__ import annotations from typing import Any -from bond_async import Action, BPUPSubscriptions, DeviceType +from bond_async import Action, DeviceType from homeassistant.components.cover import ( ATTR_POSITION, @@ -17,7 +17,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import BondConfigEntry from .entity import BondEntity -from .utils import BondDevice, BondHub +from .models import BondData +from .utils import BondDevice def _bond_to_hass_position(bond_position: int) -> int: @@ -37,12 +38,9 @@ async def async_setup_entry( ) -> None: """Set up Bond cover devices.""" data = entry.runtime_data - hub = data.hub - bpup_subs = data.bpup_subs - async_add_entities( - BondCover(hub, device, bpup_subs) - for device in hub.devices + BondCover(data, device) + for device in data.hub.devices if device.type == DeviceType.MOTORIZED_SHADES ) @@ -52,11 +50,9 @@ class BondCover(BondEntity, CoverEntity): _attr_device_class = CoverDeviceClass.SHADE - def __init__( - self, hub: BondHub, device: BondDevice, bpup_subs: BPUPSubscriptions - ) -> None: + def __init__(self, data: BondData, device: BondDevice) -> None: """Create HA entity representing Bond cover.""" - super().__init__(hub, device, bpup_subs) + super().__init__(data, device) supported_features = CoverEntityFeature(0) if self._device.supports_set_position(): supported_features |= CoverEntityFeature.SET_POSITION @@ -84,31 +80,31 @@ class BondCover(BondEntity, CoverEntity): async def async_set_cover_position(self, **kwargs: Any) -> None: """Set the cover position.""" - await self._hub.bond.action( - self._device.device_id, + await self._bond.action( + self._device_id, Action.set_position(_hass_to_bond_position(kwargs[ATTR_POSITION])), ) async def async_open_cover(self, **kwargs: Any) -> None: """Open the cover.""" - await self._hub.bond.action(self._device.device_id, Action.open()) + await self._bond.action(self._device_id, Action.open()) async def async_close_cover(self, **kwargs: Any) -> None: """Close cover.""" - await self._hub.bond.action(self._device.device_id, Action.close()) + await self._bond.action(self._device_id, Action.close()) async def async_stop_cover(self, **kwargs: Any) -> None: """Hold cover.""" - await self._hub.bond.action(self._device.device_id, Action.hold()) + await self._bond.action(self._device_id, Action.hold()) async def async_open_cover_tilt(self, **kwargs: Any) -> None: """Open the cover tilt.""" - await self._hub.bond.action(self._device.device_id, Action.tilt_open()) + await self._bond.action(self._device_id, Action.tilt_open()) async def async_close_cover_tilt(self, **kwargs: Any) -> None: """Close the cover tilt.""" - await self._hub.bond.action(self._device.device_id, Action.tilt_close()) + await self._bond.action(self._device_id, Action.tilt_close()) async def async_stop_cover_tilt(self, **kwargs: Any) -> None: """Stop the cover.""" - await self._hub.bond.action(self._device.device_id, Action.hold()) + await self._bond.action(self._device_id, Action.hold()) diff --git a/homeassistant/components/bond/entity.py b/homeassistant/components/bond/entity.py index 4495e76859d..81f96b1772c 100644 --- a/homeassistant/components/bond/entity.py +++ b/homeassistant/components/bond/entity.py @@ -8,7 +8,6 @@ from datetime import datetime import logging from aiohttp import ClientError -from bond_async import BPUPSubscriptions from homeassistant.const import ( ATTR_HW_VERSION, @@ -24,7 +23,8 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.event import async_call_later from .const import DOMAIN -from .utils import BondDevice, BondHub +from .models import BondData +from .utils import BondDevice _LOGGER = logging.getLogger(__name__) @@ -39,19 +39,20 @@ class BondEntity(Entity): def __init__( self, - hub: BondHub, + data: BondData, device: BondDevice, - bpup_subs: BPUPSubscriptions, sub_device: str | None = None, sub_device_id: str | None = None, ) -> None: """Initialize entity with API and device info.""" + hub = data.hub self._hub = hub + self._bond = hub.bond self._device = device self._device_id = device.device_id self._sub_device = sub_device self._attr_available = True - self._bpup_subs = bpup_subs + self._bpup_subs = data.bpup_subs self._update_lock = Lock() self._initialized = False if sub_device_id: @@ -79,7 +80,7 @@ class BondEntity(Entity): device_info = DeviceInfo( manufacturer=self._hub.make, # type ignore: tuple items should not be Optional - identifiers={(DOMAIN, self._hub.bond_id, self._device.device_id)}, # type: ignore[arg-type] + identifiers={(DOMAIN, self._hub.bond_id, self._device_id)}, # type: ignore[arg-type] configuration_url=f"http://{self._hub.host}", ) if self.name is not None: @@ -141,7 +142,7 @@ class BondEntity(Entity): async def _async_update_from_api(self) -> None: """Fetch via the API.""" try: - state: dict = await self._hub.bond.device_state(self._device_id) + state: dict = await self._bond.device_state(self._device_id) except (ClientError, TimeoutError, OSError) as error: if self.available: _LOGGER.warning( diff --git a/homeassistant/components/bond/fan.py b/homeassistant/components/bond/fan.py index 4ed6f83a980..4bf091d7356 100644 --- a/homeassistant/components/bond/fan.py +++ b/homeassistant/components/bond/fan.py @@ -7,7 +7,7 @@ import math from typing import Any from aiohttp.client_exceptions import ClientResponseError -from bond_async import Action, BPUPSubscriptions, DeviceType, Direction +from bond_async import Action, DeviceType, Direction import voluptuous as vol from homeassistant.components.fan import ( @@ -29,7 +29,8 @@ 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 .utils import BondDevice, BondHub +from .models import BondData +from .utils import BondDevice _LOGGER = logging.getLogger(__name__) @@ -43,8 +44,6 @@ async def async_setup_entry( ) -> None: """Set up Bond fan devices.""" data = entry.runtime_data - hub = data.hub - bpup_subs = data.bpup_subs platform = entity_platform.async_get_current_platform() platform.async_register_entity_service( SERVICE_SET_FAN_SPEED_TRACKED_STATE, @@ -53,8 +52,8 @@ async def async_setup_entry( ) async_add_entities( - BondFan(hub, device, bpup_subs) - for device in hub.devices + BondFan(data, device) + for device in data.hub.devices if DeviceType.is_fan(device.type) ) @@ -62,14 +61,12 @@ async def async_setup_entry( class BondFan(BondEntity, FanEntity): """Representation of a Bond fan.""" - def __init__( - self, hub: BondHub, device: BondDevice, bpup_subs: BPUPSubscriptions - ) -> None: + def __init__(self, data: BondData, device: BondDevice) -> None: """Create HA entity representing Bond fan.""" self._power: bool | None = None self._speed: int | None = None self._direction: int | None = None - super().__init__(hub, device, bpup_subs) + super().__init__(data, device) if self._device.has_action(Action.BREEZE_ON): self._attr_preset_modes = [PRESET_MODE_BREEZE] features = FanEntityFeature(0) @@ -136,15 +133,13 @@ class BondFan(BondEntity, FanEntity): bond_speed, ) - await self._hub.bond.action( - self._device.device_id, Action.set_speed(bond_speed) - ) + await self._bond.action(self._device_id, Action.set_speed(bond_speed)) async def async_set_power_belief(self, power_state: bool) -> None: """Set the believed state to on or off.""" try: - await self._hub.bond.action( - self._device.device_id, Action.set_power_state_belief(power_state) + await self._bond.action( + self._device_id, Action.set_power_state_belief(power_state) ) except ClientResponseError as ex: raise HomeAssistantError( @@ -168,8 +163,8 @@ class BondFan(BondEntity, FanEntity): bond_speed, ) try: - await self._hub.bond.action( - self._device.device_id, Action.set_speed_belief(bond_speed) + await self._bond.action( + self._device_id, Action.set_speed_belief(bond_speed) ) except ClientResponseError as ex: raise HomeAssistantError( @@ -191,25 +186,21 @@ class BondFan(BondEntity, FanEntity): elif percentage is not None: await self.async_set_percentage(percentage) else: - await self._hub.bond.action(self._device.device_id, Action.turn_on()) + await self._bond.action(self._device_id, Action.turn_on()) async def async_set_preset_mode(self, preset_mode: str) -> None: """Set the preset mode of the fan.""" - await self._hub.bond.action(self._device.device_id, Action(Action.BREEZE_ON)) + await self._bond.action(self._device_id, Action(Action.BREEZE_ON)) async def async_turn_off(self, **kwargs: Any) -> None: """Turn the fan off.""" if self.preset_mode == PRESET_MODE_BREEZE: - await self._hub.bond.action( - self._device.device_id, Action(Action.BREEZE_OFF) - ) - await self._hub.bond.action(self._device.device_id, Action.turn_off()) + await self._bond.action(self._device_id, Action(Action.BREEZE_OFF)) + await self._bond.action(self._device_id, Action.turn_off()) async def async_set_direction(self, direction: str) -> None: """Set fan rotation direction.""" bond_direction = ( Direction.REVERSE if direction == DIRECTION_REVERSE else Direction.FORWARD ) - await self._hub.bond.action( - self._device.device_id, Action.set_direction(bond_direction) - ) + await self._bond.action(self._device_id, Action.set_direction(bond_direction)) diff --git a/homeassistant/components/bond/light.py b/homeassistant/components/bond/light.py index 8ad348064d3..3bff7fe754e 100644 --- a/homeassistant/components/bond/light.py +++ b/homeassistant/components/bond/light.py @@ -6,7 +6,7 @@ import logging from typing import Any from aiohttp.client_exceptions import ClientResponseError -from bond_async import Action, BPUPSubscriptions, DeviceType +from bond_async import Action, DeviceType import voluptuous as vol from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity @@ -23,7 +23,8 @@ from .const import ( SERVICE_SET_LIGHT_POWER_TRACKED_STATE, ) from .entity import BondEntity -from .utils import BondDevice, BondHub +from .models import BondData +from .utils import BondDevice _LOGGER = logging.getLogger(__name__) @@ -46,8 +47,6 @@ async def async_setup_entry( """Set up Bond light devices.""" data = entry.runtime_data hub = data.hub - bpup_subs = data.bpup_subs - platform = entity_platform.async_get_current_platform() platform = entity_platform.async_get_current_platform() for service in ENTITY_SERVICES: @@ -58,7 +57,7 @@ async def async_setup_entry( ) fan_lights: list[Entity] = [ - BondLight(hub, device, bpup_subs) + BondLight(data, device) for device in hub.devices if DeviceType.is_fan(device.type) and device.supports_light() @@ -66,31 +65,31 @@ async def async_setup_entry( ] fan_up_lights: list[Entity] = [ - BondUpLight(hub, device, bpup_subs, "up_light") + BondUpLight(data, device, "up_light") for device in hub.devices if DeviceType.is_fan(device.type) and device.supports_up_light() ] fan_down_lights: list[Entity] = [ - BondDownLight(hub, device, bpup_subs, "down_light") + BondDownLight(data, device, "down_light") for device in hub.devices if DeviceType.is_fan(device.type) and device.supports_down_light() ] fireplaces: list[Entity] = [ - BondFireplace(hub, device, bpup_subs) + BondFireplace(data, device) for device in hub.devices if DeviceType.is_fireplace(device.type) ] fp_lights: list[Entity] = [ - BondLight(hub, device, bpup_subs, "light") + BondLight(data, device, "light") for device in hub.devices if DeviceType.is_fireplace(device.type) and device.supports_light() ] lights: list[Entity] = [ - BondLight(hub, device, bpup_subs) + BondLight(data, device) for device in hub.devices if DeviceType.is_light(device.type) ] @@ -130,8 +129,8 @@ class BondBaseLight(BondEntity, LightEntity): await self.async_set_power_belief(False) return try: - await self._hub.bond.action( - self._device.device_id, + await self._bond.action( + self._device_id, Action.set_brightness_belief(round((brightness * 100) / 255)), ) except ClientResponseError as ex: @@ -143,8 +142,8 @@ class BondBaseLight(BondEntity, LightEntity): async def async_set_power_belief(self, power_state: bool) -> None: """Set the belief state of the light.""" try: - await self._hub.bond.action( - self._device.device_id, Action.set_light_state_belief(power_state) + await self._bond.action( + self._device_id, Action.set_light_state_belief(power_state) ) except ClientResponseError as ex: raise HomeAssistantError( @@ -158,13 +157,12 @@ class BondLight(BondBaseLight, BondEntity, LightEntity): def __init__( self, - hub: BondHub, + data: BondData, device: BondDevice, - bpup_subs: BPUPSubscriptions, sub_device: str | None = None, ) -> None: """Create HA entity representing Bond light.""" - super().__init__(hub, device, bpup_subs, sub_device) + super().__init__(data, device, sub_device) if device.supports_set_brightness(): self._attr_color_mode = ColorMode.BRIGHTNESS self._attr_supported_color_modes = {ColorMode.BRIGHTNESS} @@ -178,16 +176,16 @@ class BondLight(BondBaseLight, BondEntity, LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the light.""" if brightness := kwargs.get(ATTR_BRIGHTNESS): - await self._hub.bond.action( - self._device.device_id, + await self._bond.action( + self._device_id, Action.set_brightness(round((brightness * 100) / 255)), ) else: - await self._hub.bond.action(self._device.device_id, Action.turn_light_on()) + await self._bond.action(self._device_id, Action.turn_light_on()) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the light.""" - await self._hub.bond.action(self._device.device_id, Action.turn_light_off()) + await self._bond.action(self._device_id, Action.turn_light_off()) @callback def _async_has_action_or_raise(self, action: str) -> None: @@ -202,8 +200,8 @@ class BondLight(BondBaseLight, BondEntity, LightEntity): " replaced with a button; Call the button.press service instead" ) self._async_has_action_or_raise(Action.START_INCREASING_BRIGHTNESS) - await self._hub.bond.action( - self._device.device_id, Action(Action.START_INCREASING_BRIGHTNESS) + await self._bond.action( + self._device_id, Action(Action.START_INCREASING_BRIGHTNESS) ) async def async_start_decreasing_brightness(self) -> None: @@ -213,8 +211,8 @@ class BondLight(BondBaseLight, BondEntity, LightEntity): " replaced with a button; Call the button.press service instead" ) self._async_has_action_or_raise(Action.START_DECREASING_BRIGHTNESS) - await self._hub.bond.action( - self._device.device_id, Action(Action.START_DECREASING_BRIGHTNESS) + await self._bond.action( + self._device_id, Action(Action.START_DECREASING_BRIGHTNESS) ) async def async_stop(self) -> None: @@ -224,7 +222,7 @@ class BondLight(BondBaseLight, BondEntity, LightEntity): " Call the button.press service instead" ) self._async_has_action_or_raise(Action.STOP) - await self._hub.bond.action(self._device.device_id, Action(Action.STOP)) + await self._bond.action(self._device_id, Action(Action.STOP)) class BondDownLight(BondBaseLight, BondEntity, LightEntity): @@ -236,15 +234,11 @@ class BondDownLight(BondBaseLight, BondEntity, LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the light.""" - await self._hub.bond.action( - self._device.device_id, Action(Action.TURN_DOWN_LIGHT_ON) - ) + await self._bond.action(self._device_id, Action(Action.TURN_DOWN_LIGHT_ON)) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the light.""" - await self._hub.bond.action( - self._device.device_id, Action(Action.TURN_DOWN_LIGHT_OFF) - ) + await self._bond.action(self._device_id, Action(Action.TURN_DOWN_LIGHT_OFF)) class BondUpLight(BondBaseLight, BondEntity, LightEntity): @@ -256,15 +250,11 @@ class BondUpLight(BondBaseLight, BondEntity, LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the light.""" - await self._hub.bond.action( - self._device.device_id, Action(Action.TURN_UP_LIGHT_ON) - ) + await self._bond.action(self._device_id, Action(Action.TURN_UP_LIGHT_ON)) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the light.""" - await self._hub.bond.action( - self._device.device_id, Action(Action.TURN_UP_LIGHT_OFF) - ) + await self._bond.action(self._device_id, Action(Action.TURN_UP_LIGHT_OFF)) class BondFireplace(BondEntity, LightEntity): @@ -287,15 +277,15 @@ class BondFireplace(BondEntity, LightEntity): if brightness := kwargs.get(ATTR_BRIGHTNESS): flame = round((brightness * 100) / 255) - await self._hub.bond.action(self._device.device_id, Action.set_flame(flame)) + await self._bond.action(self._device_id, Action.set_flame(flame)) else: - await self._hub.bond.action(self._device.device_id, Action.turn_on()) + await self._bond.action(self._device_id, Action.turn_on()) async def async_turn_off(self, **kwargs: Any) -> None: """Turn the fireplace off.""" _LOGGER.debug("Fireplace async_turn_off called with: %s", kwargs) - await self._hub.bond.action(self._device.device_id, Action.turn_off()) + await self._bond.action(self._device_id, Action.turn_off()) async def async_set_brightness_belief(self, brightness: int) -> None: """Set the belief state of the light.""" @@ -305,8 +295,8 @@ class BondFireplace(BondEntity, LightEntity): await self.async_set_power_belief(False) return try: - await self._hub.bond.action( - self._device.device_id, + await self._bond.action( + self._device_id, Action.set_brightness_belief(round((brightness * 100) / 255)), ) except ClientResponseError as ex: @@ -318,8 +308,8 @@ class BondFireplace(BondEntity, LightEntity): async def async_set_power_belief(self, power_state: bool) -> None: """Set the belief state of the light.""" try: - await self._hub.bond.action( - self._device.device_id, Action.set_power_state_belief(power_state) + await self._bond.action( + self._device_id, Action.set_power_state_belief(power_state) ) except ClientResponseError as ex: raise HomeAssistantError( diff --git a/homeassistant/components/bond/switch.py b/homeassistant/components/bond/switch.py index b8aaa81cd05..ace6d307e6d 100644 --- a/homeassistant/components/bond/switch.py +++ b/homeassistant/components/bond/switch.py @@ -26,8 +26,6 @@ async def async_setup_entry( ) -> None: """Set up Bond generic devices.""" data = entry.runtime_data - hub = data.hub - bpup_subs = data.bpup_subs platform = entity_platform.async_get_current_platform() platform.async_register_entity_service( SERVICE_SET_POWER_TRACKED_STATE, @@ -36,8 +34,8 @@ async def async_setup_entry( ) async_add_entities( - BondSwitch(hub, device, bpup_subs) - for device in hub.devices + BondSwitch(data, device) + for device in data.hub.devices if DeviceType.is_generic(device.type) ) @@ -50,17 +48,17 @@ class BondSwitch(BondEntity, SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" - await self._hub.bond.action(self._device.device_id, Action.turn_on()) + await self._bond.action(self._device_id, Action.turn_on()) async def async_turn_off(self, **kwargs: Any) -> None: """Turn the device off.""" - await self._hub.bond.action(self._device.device_id, Action.turn_off()) + await self._bond.action(self._device_id, Action.turn_off()) async def async_set_power_belief(self, power_state: bool) -> None: """Set switch power belief.""" try: - await self._hub.bond.action( - self._device.device_id, Action.set_power_state_belief(power_state) + await self._bond.action( + self._device_id, Action.set_power_state_belief(power_state) ) except ClientResponseError as ex: raise HomeAssistantError(