Return on_since only when its available and the device is on (#48)

* moves on_since property to smartdevice class, as it is not plug only
* returns None if the value is not available (some bulbs), or if the device is off
This commit is contained in:
Teemu R
2020-04-24 16:47:57 +02:00
committed by GitHub
parent fd560442a2
commit 0c71957aa8
5 changed files with 54 additions and 26 deletions

View File

@@ -15,7 +15,7 @@ import functools
import inspect
import logging
from dataclasses import dataclass
from datetime import datetime
from datetime import datetime, timedelta
from enum import Enum
from typing import Any, Dict, List, Optional
@@ -554,6 +554,23 @@ class SmartDevice:
"""
raise NotImplementedError("Device subclass needs to implement this.")
@property # type: ignore
@requires_update
def on_since(self) -> Optional[datetime]:
"""Return pretty-printed on-time, if available.
Returns None if the device is turned off or does not report it.
"""
if "on_time" not in self.sys_info:
return None
if self.is_off:
return None
on_time = self.sys_info["on_time"]
return datetime.now() - timedelta(seconds=on_time)
@property # type: ignore
@requires_update
def state_information(self) -> Dict[str, Any]: