Skip to content

Commit

Permalink
feat(sensor): select sensor state in automations from list (#54)
Browse files Browse the repository at this point in the history
Turn device class to ` SensorDeviceClass.ENUM` to let HA know all possible states of sensor ans use these states in automatisation helpers. 
Many thanks to @joostlek for contributing.
  • Loading branch information
joostlek authored Aug 20, 2023
1 parent eeb9ed2 commit 4558ba8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
33 changes: 33 additions & 0 deletions custom_components/sleep_as_android/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,36 @@
DEFAULT_NAME = "SleepAsAndroid"
DEFAULT_TOPIC_TEMPLATE = "SleepAsAndroid/%s" % DEVICE_MACRO
DEFAULT_QOS = 0

# available at https://docs.sleep.urbandroid.org/services/automation.html#events
sleep_tracking_states = [
"sleep_tracking_started",
"sleep_tracking_stopped",
"sleep_tracking_paused",
"sleep_tracking_resumed",
"alarm_snooze_clicked",
"alarm_snooze_canceled",
"time_to_bed_alarm_alert",
"alarm_alert_start",
"alarm_alert_dismiss",
"alarm_skip_next",
"show_skip_next_alarm",
"rem",
"smart_period",
"before_smart_period",
"lullaby_start",
"lullaby_stop",
"lullaby_volume_down",
"deep_sleep",
"light_sleep",
"awake",
"not_awake",
"apnea_alarm",
"antisnoring",
"sound_event_snore",
"sound_event_talk",
"sound_event_cough",
"sound_event_baby",
"sound_event_laugh",
"before_alarm",
]
35 changes: 2 additions & 33 deletions custom_components/sleep_as_android/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,11 @@
from homeassistant.core import HomeAssistant
import voluptuous as vol

from .const import DOMAIN
from .const import DOMAIN, sleep_tracking_states

_LOGGER = logging.getLogger(__name__)

# available at https://docs.sleep.urbandroid.org/services/automation.html#events
TRIGGERS = [
"sleep_tracking_started",
"sleep_tracking_stopped",
"sleep_tracking_paused",
"sleep_tracking_resumed",
"alarm_snooze_clicked",
"alarm_snooze_canceled",
"time_to_bed_alarm_alert",
"alarm_alert_start",
"alarm_alert_dismiss",
"alarm_skip_next",
"show_skip_next_alarm",
"rem",
"smart_period",
"before_smart_period",
"lullaby_start",
"lullaby_stop",
"lullaby_volume_down",
"deep_sleep",
"light_sleep",
"awake",
"not_awake",
"apnea_alarm",
"antisnoring",
"sound_event_snore",
"sound_event_talk",
"sound_event_cough",
"sound_event_baby",
"sound_event_laugh",
"before_alarm",
]
TRIGGERS = sleep_tracking_states

TRIGGER_SCHEMA = HA_TRIGGER_BASE_SCHEMA.extend(
{
Expand Down
18 changes: 11 additions & 7 deletions custom_components/sleep_as_android/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import logging
from typing import TYPE_CHECKING

from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity_registry import async_entries_for_config_entry
from homeassistant.helpers.restore_state import RestoreEntity

from .const import DOMAIN
from .const import DOMAIN, sleep_tracking_states
from .device_trigger import TRIGGERS

if TYPE_CHECKING:
Expand Down Expand Up @@ -68,7 +68,11 @@ class SleepAsAndroidSensor(SensorEntity, RestoreEntity):
"""
_attr_icon = "mdi:sleep"
_attr_should_poll = False
_attr_device_class = f"{DOMAIN}__status"
_attr_device_class = SensorDeviceClass.ENUM
_attr_options = [
"unknown",
*sleep_tracking_states,
]
_attr_translation_key = "sleep_as_android_status"

def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, name: str):
Expand Down Expand Up @@ -101,11 +105,11 @@ async def async_added_to_hass(self):
_LOGGER.debug("My device id is %s", device.id)
self._device_id = device.id

if (old_state := await self.async_get_last_state()) is not None:
self._attr_native_value = old_state.state
if (old_state := await self.async_get_last_sensor_data()) is not None:
self._attr_native_value = old_state.native_value
_LOGGER.debug(
f"async_added_to_hass: restored previous state for {self.name}: "
f"{self.state=}, {self._attr_native_value=}."
f"{self.state=}, {self.native_value=}."
)
else:
# No previous state. It is fine, but it would be nice to report
Expand Down Expand Up @@ -168,7 +172,7 @@ def unique_id(self) -> str:
@property
def available(self) -> bool:
"""Is sensor available or not."""
return self.state != STATE_UNKNOWN
return self.native_value != STATE_UNKNOWN

@property
def device_id(self) -> str:
Expand Down

0 comments on commit 4558ba8

Please sign in to comment.