1 """Support for Crownstone devices."""
3 from __future__
import annotations
5 from functools
import partial
6 from typing
import TYPE_CHECKING, Any
8 from crownstone_cloud.cloud_models.crownstones
import Crownstone
9 from crownstone_cloud.const
import DIMMING_ABILITY
10 from crownstone_cloud.exceptions
import CrownstoneAbilityError
11 from crownstone_uart
import CrownstoneUart
21 CROWNSTONE_INCLUDE_TYPES,
24 SIG_CROWNSTONE_STATE_UPDATE,
25 SIG_UART_STATE_CHANGE,
27 from .entity
import CrownstoneEntity
28 from .helpers
import map_from_to
31 from .entry_manager
import CrownstoneEntryManager
36 config_entry: ConfigEntry,
37 async_add_entities: AddEntitiesCallback,
39 """Set up crownstones from a config entry."""
40 manager: CrownstoneEntryManager = hass.data[DOMAIN][config_entry.entry_id]
42 entities: list[CrownstoneLightEntity] = []
45 for sphere
in manager.cloud.cloud_data:
46 for crownstone
in sphere.crownstones:
47 if crownstone.type
in CROWNSTONE_INCLUDE_TYPES:
49 if manager.uart
and sphere.cloud_id == manager.usb_sphere_id:
59 """Crownstone 0..100 to hass 0..255."""
64 """Hass 0..255 to Crownstone 0..100."""
69 """Representation of a crownstone.
71 Light platform is used to support dimming.
75 _attr_translation_key =
"german_power_outlet"
78 self, crownstone_data: Crownstone, usb: CrownstoneUart |
None =
None
80 """Initialize the crownstone."""
88 """Return the brightness if dimming enabled."""
93 """Return if the device is on."""
98 """Return the color mode of the light."""
99 if self.
devicedevice.abilities.get(DIMMING_ABILITY).is_enabled:
100 return ColorMode.BRIGHTNESS
101 return ColorMode.ONOFF
105 """Flag supported color modes."""
109 """Set up a listener when this entity is added to HA."""
124 """Turn on this light via dongle or cloud."""
125 if ATTR_BRIGHTNESS
in kwargs:
126 if self.
usbusb
is not None and self.
usbusb.is_ready():
127 await self.
hasshass.async_add_executor_job(
129 self.
usbusb.dim_crownstone,
130 self.
devicedevice.unique_id,
136 await self.
devicedevice.async_set_brightness(
139 except CrownstoneAbilityError
as ability_error:
146 elif self.
usbusb
is not None and self.
usbusb.is_ready():
147 await self.
hasshass.async_add_executor_job(
148 partial(self.
usbusb.switch_crownstone, self.
devicedevice.unique_id, on=
True)
150 self.
devicedevice.state = 100
155 self.
devicedevice.state = 100
159 """Turn off this device via dongle or cloud."""
160 if self.
usbusb
is not None and self.
usbusb.is_ready():
161 await self.
hasshass.async_add_executor_job(
162 partial(self.
usbusb.switch_crownstone, self.
devicedevice.unique_id, on=
False)
168 self.
devicedevice.state = 0
None async_turn_off(self, **Any kwargs)
None async_added_to_hass(self)
int|None brightness(self)
set[str]|None supported_color_modes(self)
None async_turn_on(self, **Any kwargs)
None __init__(self, Crownstone crownstone_data, CrownstoneUart|None usb=None)
ColorMode|str|None color_mode(self)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
int map_from_to(int val, int in_min, int in_max, int out_min, int out_max)
int hass_to_crownstone_state(int value)
int crownstone_state_to_hass(int value)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)