1 """Fan representation of a Snooz device."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from datetime
import timedelta
9 from pysnooz.api
import UnknownSnoozState
10 from pysnooz.commands
import (
12 SnoozCommandResultStatus,
17 import voluptuous
as vol
32 DEFAULT_TRANSITION_DURATION,
34 SERVICE_TRANSITION_OFF,
35 SERVICE_TRANSITION_ON,
37 from .models
import SnoozConfigurationData
41 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
43 """Set up Snooz device from a config entry."""
45 platform = entity_platform.async_get_current_platform()
46 platform.async_register_entity_service(
47 SERVICE_TRANSITION_ON,
49 vol.Optional(ATTR_VOLUME): vol.All(
50 vol.Coerce(int), vol.Range(min=0, max=100)
52 vol.Optional(ATTR_DURATION, default=DEFAULT_TRANSITION_DURATION): vol.All(
53 vol.Coerce(int), vol.Range(min=1, max=300)
56 "async_transition_on",
58 platform.async_register_entity_service(
59 SERVICE_TRANSITION_OFF,
61 vol.Optional(ATTR_DURATION, default=DEFAULT_TRANSITION_DURATION): vol.All(
62 vol.Coerce(int), vol.Range(min=1, max=300)
65 "async_transition_off",
68 data: SnoozConfigurationData = hass.data[DOMAIN][entry.entry_id]
74 """Fan representation of a Snooz device."""
76 _attr_has_entity_name =
True
78 _attr_supported_features = (
79 FanEntityFeature.SET_SPEED
80 | FanEntityFeature.TURN_OFF
81 | FanEntityFeature.TURN_ON
83 _attr_should_poll =
False
84 _is_on: bool |
None =
None
85 _percentage: int |
None =
None
86 _enable_turn_on_off_backwards_compatibility =
False
88 def __init__(self, data: SnoozConfigurationData) ->
None:
89 """Initialize a Snooz fan entity."""
104 """Restore state and subscribe to device events."""
108 if last_state.state
in (STATE_ON, STATE_OFF):
109 self.
_is_on_is_on = last_state.state == STATE_ON
112 self.
_percentage_percentage = last_state.attributes.get(ATTR_PERCENTAGE)
122 """Volume level of the device."""
127 """Power state of the device."""
132 """Return True if unable to access real state of the entity."""
133 return not self.
_device_device.is_connected
or self.
_device_device.state
is UnknownSnoozState
137 percentage: int |
None =
None,
138 preset_mode: str |
None =
None,
141 """Turn on the device."""
145 """Turn off the device."""
149 """Set the volume of the device. A value of 0 will turn off the device."""
151 set_volume(percentage)
if percentage > 0
else turn_off()
155 """Transition on the device."""
161 """Transition off the device."""
167 result = await self.
_device_device.async_execute_command(command)
169 if result.status == SnoozCommandResultStatus.SUCCESSFUL:
171 elif result.status != SnoozCommandResultStatus.CANCELLED:
173 f
"Command {command} failed with status {result.status.name} after"
174 f
" {result.duration}"
None turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
None _async_execute_command(self, SnoozCommandData command)
None async_transition_off(self, int duration, **Any kwargs)
int|None percentage(self)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
None async_transition_on(self, int duration, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None __init__(self, SnoozConfigurationData data)
None _async_write_state_changed(self)
None async_set_percentage(self, int percentage)
None async_added_to_hass(self)
Callable[[], None] _async_subscribe_to_device_change(self)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None turn_off(self, **Any kwargs)
State|None async_get_last_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)