Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sensor): select sensor state in automations from list #54

Merged
merged 5 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
]
IATkachenko marked this conversation as resolved.
Show resolved Hide resolved
_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
IATkachenko marked this conversation as resolved.
Show resolved Hide resolved
_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