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

Change Sleep Data Last Collected to Timestamp #59

Merged
Merged
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
20 changes: 8 additions & 12 deletions custom_components/resmed_myair/sensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from collections.abc import Mapping
from datetime import datetime, timedelta
import logging

from aiohttp import DummyCookieJar
from dateutil import parser
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
Expand All @@ -18,6 +16,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.redact import async_redact_data
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util

from .client.myair_client import MyAirConfig
from .client.rest_client import RESTClient
Expand Down Expand Up @@ -86,9 +85,8 @@ def native_value(self):
# The API always returns the previous month of data, so the client stores this
# We assume this is ordered temporally and grab the last one: the latest one
value = self.coordinator.sleep_records[-1].get(self.sensor_key, 0)
if self.sensor_key.endswith("Date"):
# A bit of a hack to interpret date's as datetimes.
value = datetime.strptime(value, "%Y-%m-%d").date()
if self.entity_description.device_class == SensorDeviceClass.DATE:
value = dt_util.parse_date(value)
return value


Expand All @@ -105,9 +103,8 @@ def __init__(
def native_value(self):

value = self.coordinator.device[self.sensor_key]
if self.sensor_key.endswith("Time"):
# A bit of a hack to interpret this time as a time
value = parser.parse(value)
if self.entity_description.device_class == SensorDeviceClass.TIMESTAMP:
value = dt_util.parse_datetime(value)
return value


Expand All @@ -124,8 +121,7 @@ def __init__(
def native_value(self):

usage_minutes = self.coordinator.sleep_records[-1]["totalUsage"]
# The split stuff is to cut off the seconds, so "07:30:00" becomes "07:30"
return str(timedelta(minutes=float(usage_minutes)))[::-1].split(":", 1)[1][::-1]
return f"{usage_minutes // 60}:{(usage_minutes % 60):02}"


class MyAirMostRecentSleepDate(MyAirBaseSensor):
Expand All @@ -149,7 +145,7 @@ def native_value(self):
)
)
date_string = sleep_days_with_data[-1]["startDate"]
return datetime.strptime(date_string, "%Y-%m-%d").date()
return dt_util.parse_date(date_string)


# Our sensor class will prepend the serial number to the key
Expand Down Expand Up @@ -184,7 +180,7 @@ def native_value(self):

DEVICE_SENSOR_DESCRIPTIONS: Mapping[str, SensorEntityDescription] = {
"CPAP Sleep Data Last Collected": SensorEntityDescription(
key="lastSleepDataReportTime", device_class=SensorDeviceClass.DATE
key="lastSleepDataReportTime", device_class=SensorDeviceClass.TIMESTAMP
)
}

Expand Down
Loading