2020-07-12 23:45:47 +00:00
|
|
|
"""Support for Bond lights."""
|
2021-03-17 22:34:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-09-22 02:38:43 +00:00
|
|
|
import logging
|
2021-04-30 18:38:59 +00:00
|
|
|
from typing import Any
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2021-09-09 13:32:32 +00:00
|
|
|
from aiohttp.client_exceptions import ClientResponseError
|
2021-02-09 08:43:38 +00:00
|
|
|
from bond_api import Action, BPUPSubscriptions, DeviceType
|
2021-09-09 13:32:32 +00:00
|
|
|
import voluptuous as vol
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2020-07-15 03:27:03 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS,
|
2022-04-02 08:03:19 +00:00
|
|
|
COLOR_MODE_BRIGHTNESS,
|
|
|
|
COLOR_MODE_ONOFF,
|
2020-07-15 03:27:03 +00:00
|
|
|
LightEntity,
|
|
|
|
)
|
2020-07-12 23:45:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-08-25 20:07:31 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-09-09 13:32:32 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
2020-07-12 23:45:47 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2021-09-09 13:32:32 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_POWER_STATE,
|
|
|
|
BPUP_SUBS,
|
|
|
|
DOMAIN,
|
|
|
|
HUB,
|
2021-09-13 01:46:43 +00:00
|
|
|
SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
|
|
|
|
SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
|
2021-09-09 13:32:32 +00:00
|
|
|
)
|
2020-07-12 23:45:47 +00:00
|
|
|
from .entity import BondEntity
|
2022-01-05 16:28:12 +00:00
|
|
|
from .utils import BondDevice, BondHub
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2020-09-22 02:38:43 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-08-25 20:07:31 +00:00
|
|
|
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,
|
|
|
|
]
|
|
|
|
|
2020-07-12 23:45:47 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
2021-04-30 18:38:59 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-07-12 23:45:47 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up Bond light devices."""
|
2021-02-09 08:43:38 +00:00
|
|
|
data = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
hub: BondHub = data[HUB]
|
|
|
|
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
2021-09-09 13:32:32 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2021-08-25 20:07:31 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
|
|
|
for service in ENTITY_SERVICES:
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
service,
|
|
|
|
{},
|
|
|
|
f"async_{service}",
|
|
|
|
)
|
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
fan_lights: list[Entity] = [
|
2021-02-09 08:43:38 +00:00
|
|
|
BondLight(hub, device, bpup_subs)
|
2020-07-16 15:31:15 +00:00
|
|
|
for device in hub.devices
|
2021-02-20 18:03:40 +00:00
|
|
|
if DeviceType.is_fan(device.type)
|
|
|
|
and device.supports_light()
|
|
|
|
and not (device.supports_up_light() and device.supports_down_light())
|
|
|
|
]
|
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
fan_up_lights: list[Entity] = [
|
2021-02-20 18:03:40 +00:00
|
|
|
BondUpLight(hub, device, bpup_subs, "up_light")
|
|
|
|
for device in hub.devices
|
|
|
|
if DeviceType.is_fan(device.type) and device.supports_up_light()
|
|
|
|
]
|
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
fan_down_lights: list[Entity] = [
|
2021-02-20 18:03:40 +00:00
|
|
|
BondDownLight(hub, device, bpup_subs, "down_light")
|
|
|
|
for device in hub.devices
|
|
|
|
if DeviceType.is_fan(device.type) and device.supports_down_light()
|
2020-07-12 23:45:47 +00:00
|
|
|
]
|
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
fireplaces: list[Entity] = [
|
2021-02-09 08:43:38 +00:00
|
|
|
BondFireplace(hub, device, bpup_subs)
|
2020-07-16 15:31:15 +00:00
|
|
|
for device in hub.devices
|
2020-07-23 01:22:25 +00:00
|
|
|
if DeviceType.is_fireplace(device.type)
|
2020-07-15 03:27:03 +00:00
|
|
|
]
|
2020-07-17 02:25:04 +00:00
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
fp_lights: list[Entity] = [
|
2021-02-09 08:43:38 +00:00
|
|
|
BondLight(hub, device, bpup_subs, "light")
|
2020-10-18 19:11:24 +00:00
|
|
|
for device in hub.devices
|
|
|
|
if DeviceType.is_fireplace(device.type) and device.supports_light()
|
|
|
|
]
|
|
|
|
|
2021-03-17 22:34:25 +00:00
|
|
|
lights: list[Entity] = [
|
2021-02-09 08:43:38 +00:00
|
|
|
BondLight(hub, device, bpup_subs)
|
2021-02-08 23:39:21 +00:00
|
|
|
for device in hub.devices
|
|
|
|
if DeviceType.is_light(device.type)
|
|
|
|
]
|
|
|
|
|
2021-09-09 13:32:32 +00:00
|
|
|
platform.async_register_entity_service(
|
2021-09-13 01:46:43 +00:00
|
|
|
SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
|
2021-09-09 13:32:32 +00:00
|
|
|
{
|
|
|
|
vol.Required(ATTR_BRIGHTNESS): vol.All(
|
|
|
|
vol.Number(scale=0), vol.Range(0, 255)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
"async_set_brightness_belief",
|
|
|
|
)
|
|
|
|
|
|
|
|
platform.async_register_entity_service(
|
2021-09-13 01:46:43 +00:00
|
|
|
SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
|
2021-09-09 13:32:32 +00:00
|
|
|
{vol.Required(ATTR_POWER_STATE): vol.All(cv.boolean)},
|
|
|
|
"async_set_power_belief",
|
|
|
|
)
|
|
|
|
|
2021-02-20 18:03:40 +00:00
|
|
|
async_add_entities(
|
|
|
|
fan_lights + fan_up_lights + fan_down_lights + fireplaces + fp_lights + lights,
|
|
|
|
True,
|
|
|
|
)
|
2020-07-15 03:27:03 +00:00
|
|
|
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2021-02-20 18:03:40 +00:00
|
|
|
class BondBaseLight(BondEntity, LightEntity):
|
2020-07-12 23:45:47 +00:00
|
|
|
"""Representation of a Bond light."""
|
|
|
|
|
2022-04-02 08:03:19 +00:00
|
|
|
_attr_color_mode = COLOR_MODE_ONOFF
|
|
|
|
_attr_supported_color_modes = {COLOR_MODE_ONOFF}
|
2021-02-20 18:03:40 +00:00
|
|
|
|
2021-09-09 13:32:32 +00:00
|
|
|
async def async_set_brightness_belief(self, brightness: int) -> None:
|
|
|
|
"""Set the belief state of the light."""
|
|
|
|
if not self._device.supports_set_brightness():
|
|
|
|
raise HomeAssistantError("This device does not support setting brightness")
|
|
|
|
if brightness == 0:
|
|
|
|
await self.async_set_power_belief(False)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
await self._hub.bond.action(
|
|
|
|
self._device.device_id,
|
|
|
|
Action.set_brightness_belief(round((brightness * 100) / 255)),
|
|
|
|
)
|
|
|
|
except ClientResponseError as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"The bond API returned an error calling set_brightness_belief for {self.entity_id}. Code: {ex.code} Message: {ex.message}"
|
|
|
|
) from ex
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
except ClientResponseError as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"The bond API returned an error calling set_light_state_belief for {self.entity_id}. Code: {ex.code} Message: {ex.message}"
|
|
|
|
) from ex
|
|
|
|
|
2021-02-20 18:03:40 +00:00
|
|
|
|
|
|
|
class BondLight(BondBaseLight, BondEntity, LightEntity):
|
|
|
|
"""Representation of a Bond light."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hub: BondHub,
|
|
|
|
device: BondDevice,
|
|
|
|
bpup_subs: BPUPSubscriptions,
|
2021-03-17 22:34:25 +00:00
|
|
|
sub_device: str | None = None,
|
2021-05-20 15:51:39 +00:00
|
|
|
) -> None:
|
2021-02-20 18:03:40 +00:00
|
|
|
"""Create HA entity representing Bond light."""
|
|
|
|
super().__init__(hub, device, bpup_subs, sub_device)
|
2021-07-16 21:06:18 +00:00
|
|
|
if device.supports_set_brightness():
|
2022-04-02 08:03:19 +00:00
|
|
|
self._attr_color_mode = COLOR_MODE_BRIGHTNESS
|
|
|
|
self._attr_supported_color_modes = {COLOR_MODE_BRIGHTNESS}
|
2021-02-20 18:03:40 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def _apply_state(self, state: dict) -> None:
|
2021-07-16 21:06:18 +00:00
|
|
|
self._attr_is_on = state.get("light") == 1
|
|
|
|
brightness = state.get("brightness")
|
|
|
|
self._attr_brightness = round(brightness * 255 / 100) if brightness else None
|
2020-07-30 01:01:59 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-07-12 23:45:47 +00:00
|
|
|
"""Turn on the light."""
|
2021-10-30 14:33:42 +00:00
|
|
|
if brightness := kwargs.get(ATTR_BRIGHTNESS):
|
2020-07-30 01:01:59 +00:00
|
|
|
await self._hub.bond.action(
|
|
|
|
self._device.device_id,
|
2020-08-01 00:36:02 +00:00
|
|
|
Action.set_brightness(round((brightness * 100) / 255)),
|
2020-07-30 01:01:59 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
await self._hub.bond.action(self._device.device_id, Action.turn_light_on())
|
2020-07-12 23:45:47 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-07-12 23:45:47 +00:00
|
|
|
"""Turn off the light."""
|
2020-07-23 01:22:25 +00:00
|
|
|
await self._hub.bond.action(self._device.device_id, Action.turn_light_off())
|
2020-07-15 03:27:03 +00:00
|
|
|
|
2021-08-25 20:07:31 +00:00
|
|
|
@callback
|
|
|
|
def _async_has_action_or_raise(self, action: str) -> None:
|
|
|
|
"""Raise HomeAssistantError if the device does not support an action."""
|
|
|
|
if not self._device.has_action(action):
|
|
|
|
raise HomeAssistantError(f"{self.entity_id} does not support {action}")
|
|
|
|
|
|
|
|
async def async_start_increasing_brightness(self) -> None:
|
|
|
|
"""Start increasing the light brightness."""
|
2022-01-23 08:24:40 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"The bond.start_increasing_brightness service is deprecated and has been replaced with a button; Call the button.press service instead"
|
|
|
|
)
|
2021-08-25 20:07:31 +00:00
|
|
|
self._async_has_action_or_raise(Action.START_INCREASING_BRIGHTNESS)
|
|
|
|
await self._hub.bond.action(
|
|
|
|
self._device.device_id, Action(Action.START_INCREASING_BRIGHTNESS)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_start_decreasing_brightness(self) -> None:
|
|
|
|
"""Start decreasing the light brightness."""
|
2022-01-23 08:24:40 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"The bond.start_decreasing_brightness service is deprecated and has been replaced with a button; Call the button.press service instead"
|
|
|
|
)
|
2021-08-25 20:07:31 +00:00
|
|
|
self._async_has_action_or_raise(Action.START_DECREASING_BRIGHTNESS)
|
|
|
|
await self._hub.bond.action(
|
|
|
|
self._device.device_id, Action(Action.START_DECREASING_BRIGHTNESS)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_stop(self) -> None:
|
|
|
|
"""Stop all actions and clear the queue."""
|
2022-01-23 08:24:40 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"The bond.stop service is deprecated and has been replaced with a button; Call the button.press service instead"
|
|
|
|
)
|
2021-08-25 20:07:31 +00:00
|
|
|
self._async_has_action_or_raise(Action.STOP)
|
|
|
|
await self._hub.bond.action(self._device.device_id, Action(Action.STOP))
|
|
|
|
|
2020-07-15 03:27:03 +00:00
|
|
|
|
2021-02-20 18:03:40 +00:00
|
|
|
class BondDownLight(BondBaseLight, BondEntity, LightEntity):
|
|
|
|
"""Representation of a Bond light."""
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def _apply_state(self, state: dict) -> None:
|
2021-07-16 21:06:18 +00:00
|
|
|
self._attr_is_on = bool(state.get("down_light") and state.get("light"))
|
2021-02-20 18:03:40 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BondUpLight(BondBaseLight, BondEntity, LightEntity):
|
|
|
|
"""Representation of a Bond light."""
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def _apply_state(self, state: dict) -> None:
|
2021-07-16 21:06:18 +00:00
|
|
|
self._attr_is_on = bool(state.get("up_light") and state.get("light"))
|
2021-02-20 18:03:40 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-15 03:27:03 +00:00
|
|
|
class BondFireplace(BondEntity, LightEntity):
|
|
|
|
"""Representation of a Bond-controlled fireplace."""
|
|
|
|
|
2022-04-02 08:03:19 +00:00
|
|
|
_attr_color_mode = COLOR_MODE_BRIGHTNESS
|
|
|
|
_attr_supported_color_modes = {COLOR_MODE_BRIGHTNESS}
|
2020-07-15 03:27:03 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def _apply_state(self, state: dict) -> None:
|
2021-07-16 21:06:18 +00:00
|
|
|
power = state.get("power")
|
|
|
|
flame = state.get("flame")
|
|
|
|
self._attr_is_on = power == 1
|
|
|
|
self._attr_brightness = round(flame * 255 / 100) if flame else None
|
|
|
|
self._attr_icon = "mdi:fireplace" if power == 1 else "mdi:fireplace-off"
|
2020-07-15 03:27:03 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-07-15 03:27:03 +00:00
|
|
|
"""Turn the fireplace on."""
|
2020-09-24 02:40:38 +00:00
|
|
|
_LOGGER.debug("Fireplace async_turn_on called with: %s", kwargs)
|
2020-09-22 02:38:43 +00:00
|
|
|
|
2021-10-22 09:34:45 +00:00
|
|
|
if brightness := kwargs.get(ATTR_BRIGHTNESS):
|
2020-07-15 03:27:03 +00:00
|
|
|
flame = round((brightness * 100) / 255)
|
2020-07-23 01:22:25 +00:00
|
|
|
await self._hub.bond.action(self._device.device_id, Action.set_flame(flame))
|
|
|
|
else:
|
|
|
|
await self._hub.bond.action(self._device.device_id, Action.turn_on())
|
2020-07-15 03:27:03 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-07-15 03:27:03 +00:00
|
|
|
"""Turn the fireplace off."""
|
2020-09-24 02:40:38 +00:00
|
|
|
_LOGGER.debug("Fireplace async_turn_off called with: %s", kwargs)
|
2020-09-22 02:38:43 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
await self._hub.bond.action(self._device.device_id, Action.turn_off())
|
2021-09-09 13:32:32 +00:00
|
|
|
|
|
|
|
async def async_set_brightness_belief(self, brightness: int) -> None:
|
|
|
|
"""Set the belief state of the light."""
|
|
|
|
if not self._device.supports_set_brightness():
|
|
|
|
raise HomeAssistantError("This device does not support setting brightness")
|
|
|
|
if brightness == 0:
|
|
|
|
await self.async_set_power_belief(False)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
await self._hub.bond.action(
|
|
|
|
self._device.device_id,
|
|
|
|
Action.set_brightness_belief(round((brightness * 100) / 255)),
|
|
|
|
)
|
|
|
|
except ClientResponseError as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"The bond API returned an error calling set_brightness_belief for {self.entity_id}. Code: {ex.code} Message: {ex.message}"
|
|
|
|
) from ex
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
except ClientResponseError as ex:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"The bond API returned an error calling set_power_state_belief for {self.entity_id}. Code: {ex.code} Message: {ex.message}"
|
|
|
|
) from ex
|