1 """Support for Bond lights."""
3 from __future__
import annotations
8 from aiohttp.client_exceptions
import ClientResponseError
9 from bond_async
import Action, DeviceType
10 import voluptuous
as vol
19 from .
import BondConfigEntry
22 SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
23 SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
25 from .entity
import BondEntity
26 from .models
import BondData
27 from .utils
import BondDevice
29 _LOGGER = logging.getLogger(__name__)
31 SERVICE_START_INCREASING_BRIGHTNESS =
"start_increasing_brightness"
32 SERVICE_START_DECREASING_BRIGHTNESS =
"start_decreasing_brightness"
36 SERVICE_START_INCREASING_BRIGHTNESS,
37 SERVICE_START_DECREASING_BRIGHTNESS,
44 entry: BondConfigEntry,
45 async_add_entities: AddEntitiesCallback,
47 """Set up Bond light devices."""
48 data = entry.runtime_data
51 platform = entity_platform.async_get_current_platform()
52 for service
in ENTITY_SERVICES:
53 platform.async_register_entity_service(
59 fan_lights: list[Entity] = [
61 for device
in hub.devices
62 if DeviceType.is_fan(device.type)
63 and device.supports_light()
64 and not (device.supports_up_light()
and device.supports_down_light())
67 fan_up_lights: list[Entity] = [
69 for device
in hub.devices
70 if DeviceType.is_fan(device.type)
and device.supports_up_light()
73 fan_down_lights: list[Entity] = [
75 for device
in hub.devices
76 if DeviceType.is_fan(device.type)
and device.supports_down_light()
79 fireplaces: list[Entity] = [
81 for device
in hub.devices
82 if DeviceType.is_fireplace(device.type)
85 fp_lights: list[Entity] = [
87 for device
in hub.devices
88 if DeviceType.is_fireplace(device.type)
and device.supports_light()
91 lights: list[Entity] = [
93 for device
in hub.devices
94 if DeviceType.is_light(device.type)
97 platform.async_register_entity_service(
98 SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
100 vol.Required(ATTR_BRIGHTNESS): vol.All(
101 vol.Number(scale=0), vol.Range(0, 255)
104 "async_set_brightness_belief",
107 platform.async_register_entity_service(
108 SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
109 {vol.Required(ATTR_POWER_STATE): vol.All(cv.boolean)},
110 "async_set_power_belief",
114 fan_lights + fan_up_lights + fan_down_lights + fireplaces + fp_lights + lights,
119 """Representation of a Bond light."""
121 _attr_color_mode = ColorMode.ONOFF
122 _attr_supported_color_modes = {ColorMode.ONOFF}
125 """Set the belief state of the light."""
126 if not self.
_device_device.supports_set_brightness():
132 await self.
_bond_bond.action(
134 Action.set_brightness_belief(round((brightness * 100) / 255)),
136 except ClientResponseError
as ex:
138 "The bond API returned an error calling set_brightness_belief for"
139 f
" {self.entity_id}. Code: {ex.status} Message: {ex.message}"
143 """Set the belief state of the light."""
145 await self.
_bond_bond.action(
146 self.
_device_id_device_id, Action.set_light_state_belief(power_state)
148 except ClientResponseError
as ex:
150 "The bond API returned an error calling set_light_state_belief for"
151 f
" {self.entity_id}. Code: {ex.status} Message: {ex.message}"
156 """Representation of a Bond light."""
162 sub_device: str |
None =
None,
164 """Create HA entity representing Bond light."""
165 super().
__init__(data, device, sub_device)
166 if device.supports_set_brightness():
171 state = self.
_device_device.state
173 brightness = state.get(
"brightness")
174 self.
_attr_brightness_attr_brightness = round(brightness * 255 / 100)
if brightness
else None
177 """Turn on the light."""
178 if brightness := kwargs.get(ATTR_BRIGHTNESS):
179 await self.
_bond_bond.action(
181 Action.set_brightness(round((brightness * 100) / 255)),
184 await self.
_bond_bond.action(self.
_device_id_device_id, Action.turn_light_on())
187 """Turn off the light."""
188 await self.
_bond_bond.action(self.
_device_id_device_id, Action.turn_light_off())
192 """Raise HomeAssistantError if the device does not support an action."""
193 if not self.
_device_device.has_action(action):
197 """Start increasing the light brightness."""
199 "The bond.start_increasing_brightness service is deprecated and has been"
200 " replaced with a button; Call the button.press service instead"
203 await self.
_bond_bond.action(
204 self.
_device_id_device_id, Action(Action.START_INCREASING_BRIGHTNESS)
208 """Start decreasing the light brightness."""
210 "The bond.start_decreasing_brightness service is deprecated and has been"
211 " replaced with a button; Call the button.press service instead"
214 await self.
_bond_bond.action(
215 self.
_device_id_device_id, Action(Action.START_DECREASING_BRIGHTNESS)
219 """Stop all actions and clear the queue."""
221 "The bond.stop service is deprecated and has been replaced with a button;"
222 " Call the button.press service instead"
225 await self.
_bond_bond.action(self.
_device_id_device_id, Action(Action.STOP))
229 """Representation of a Bond light."""
232 state = self.
_device_device.state
236 """Turn on the light."""
237 await self.
_bond_bond.action(self.
_device_id_device_id, Action(Action.TURN_DOWN_LIGHT_ON))
240 """Turn off the light."""
241 await self.
_bond_bond.action(self.
_device_id_device_id, Action(Action.TURN_DOWN_LIGHT_OFF))
245 """Representation of a Bond light."""
248 state = self.
_device_device.state
252 """Turn on the light."""
253 await self.
_bond_bond.action(self.
_device_id_device_id, Action(Action.TURN_UP_LIGHT_ON))
256 """Turn off the light."""
257 await self.
_bond_bond.action(self.
_device_id_device_id, Action(Action.TURN_UP_LIGHT_OFF))
261 """Representation of a Bond-controlled fireplace."""
263 _attr_color_mode = ColorMode.BRIGHTNESS
264 _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
265 _attr_translation_key =
"fireplace"
268 state = self.
_device_device.state
269 power = state.get(
"power")
270 flame = state.get(
"flame")
275 """Turn the fireplace on."""
276 _LOGGER.debug(
"Fireplace async_turn_on called with: %s", kwargs)
278 if brightness := kwargs.get(ATTR_BRIGHTNESS):
279 flame = round((brightness * 100) / 255)
280 await self.
_bond_bond.action(self.
_device_id_device_id, Action.set_flame(flame))
282 await self.
_bond_bond.action(self.
_device_id_device_id, Action.turn_on())
285 """Turn the fireplace off."""
286 _LOGGER.debug(
"Fireplace async_turn_off called with: %s", kwargs)
288 await self.
_bond_bond.action(self.
_device_id_device_id, Action.turn_off())
291 """Set the belief state of the light."""
292 if not self.
_device_device.supports_set_brightness():
298 await self.
_bond_bond.action(
300 Action.set_brightness_belief(round((brightness * 100) / 255)),
302 except ClientResponseError
as ex:
304 "The bond API returned an error calling set_brightness_belief for"
305 f
" {self.entity_id}. Code: {ex.status} Message: {ex.message}"
309 """Set the belief state of the light."""
311 await self.
_bond_bond.action(
312 self.
_device_id_device_id, Action.set_power_state_belief(power_state)
314 except ClientResponseError
as ex:
316 "The bond API returned an error calling set_power_state_belief for"
317 f
" {self.entity_id}. Code: {ex.status} Message: {ex.message}"
dictionary _attr_supported_color_modes
None async_set_power_belief(self, bool power_state)
None async_set_brightness_belief(self, int brightness)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None async_set_brightness_belief(self, int brightness)
None async_turn_on(self, **Any kwargs)
None async_set_power_belief(self, bool power_state)
None async_turn_off(self, **Any kwargs)
None async_start_decreasing_brightness(self)
None async_start_increasing_brightness(self)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
_attr_supported_color_modes
None _async_has_action_or_raise(self, str action)
None __init__(self, BondData data, BondDevice device, str|None sub_device=None)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, BondConfigEntry entry, AddEntitiesCallback async_add_entities)