Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -4,8 +4,7 @@ import datetime
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_DATE, ATTR_TIME, CONF_ICON, CONF_NAME)
|
||||
from homeassistant.const import ATTR_DATE, ATTR_TIME, CONF_ICON, CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.config_validation import ENTITY_SERVICE_SCHEMA
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
@@ -15,24 +14,26 @@ from homeassistant.util import dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = 'input_datetime'
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
DOMAIN = "input_datetime"
|
||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||
|
||||
CONF_HAS_DATE = 'has_date'
|
||||
CONF_HAS_TIME = 'has_time'
|
||||
CONF_INITIAL = 'initial'
|
||||
CONF_HAS_DATE = "has_date"
|
||||
CONF_HAS_TIME = "has_time"
|
||||
CONF_INITIAL = "initial"
|
||||
|
||||
DEFAULT_VALUE = '1970-01-01 00:00:00'
|
||||
DEFAULT_VALUE = "1970-01-01 00:00:00"
|
||||
|
||||
ATTR_DATETIME = 'datetime'
|
||||
ATTR_DATETIME = "datetime"
|
||||
|
||||
SERVICE_SET_DATETIME = 'set_datetime'
|
||||
SERVICE_SET_DATETIME = "set_datetime"
|
||||
|
||||
SERVICE_SET_DATETIME_SCHEMA = ENTITY_SERVICE_SCHEMA.extend({
|
||||
vol.Optional(ATTR_DATE): cv.date,
|
||||
vol.Optional(ATTR_TIME): cv.time,
|
||||
vol.Optional(ATTR_DATETIME): cv.datetime,
|
||||
})
|
||||
SERVICE_SET_DATETIME_SCHEMA = ENTITY_SERVICE_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(ATTR_DATE): cv.date,
|
||||
vol.Optional(ATTR_TIME): cv.time,
|
||||
vol.Optional(ATTR_DATETIME): cv.datetime,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def has_date_or_time(conf):
|
||||
@@ -40,20 +41,26 @@ def has_date_or_time(conf):
|
||||
if conf[CONF_HAS_DATE] or conf[CONF_HAS_TIME]:
|
||||
return conf
|
||||
|
||||
raise vol.Invalid('Entity needs at least a date or a time')
|
||||
raise vol.Invalid("Entity needs at least a date or a time")
|
||||
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: cv.schema_with_slug_keys(
|
||||
vol.All({
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_HAS_DATE, default=False): cv.boolean,
|
||||
vol.Optional(CONF_HAS_TIME, default=False): cv.boolean,
|
||||
vol.Optional(CONF_ICON): cv.icon,
|
||||
vol.Optional(CONF_INITIAL): cv.string,
|
||||
}, has_date_or_time)
|
||||
)
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: cv.schema_with_slug_keys(
|
||||
vol.All(
|
||||
{
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_HAS_DATE, default=False): cv.boolean,
|
||||
vol.Optional(CONF_HAS_TIME, default=False): cv.boolean,
|
||||
vol.Optional(CONF_ICON): cv.icon,
|
||||
vol.Optional(CONF_INITIAL): cv.string,
|
||||
},
|
||||
has_date_or_time,
|
||||
)
|
||||
)
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
@@ -68,8 +75,9 @@ async def async_setup(hass, config):
|
||||
has_date = cfg.get(CONF_HAS_DATE)
|
||||
icon = cfg.get(CONF_ICON)
|
||||
initial = cfg.get(CONF_INITIAL)
|
||||
entities.append(InputDatetime(object_id, name,
|
||||
has_date, has_time, icon, initial))
|
||||
entities.append(
|
||||
InputDatetime(object_id, name, has_date, has_time, icon, initial)
|
||||
)
|
||||
|
||||
if not entities:
|
||||
return False
|
||||
@@ -80,12 +88,19 @@ async def async_setup(hass, config):
|
||||
date = call.data.get(ATTR_DATE)
|
||||
dttm = call.data.get(ATTR_DATETIME)
|
||||
# pylint: disable=too-many-boolean-expressions
|
||||
if (dttm and (date or time)
|
||||
or entity.has_date and not (date or dttm)
|
||||
or entity.has_time and not (time or dttm)):
|
||||
_LOGGER.error("Invalid service data for %s "
|
||||
"input_datetime.set_datetime: %s",
|
||||
entity.entity_id, str(call.data))
|
||||
if (
|
||||
dttm
|
||||
and (date or time)
|
||||
or entity.has_date
|
||||
and not (date or dttm)
|
||||
or entity.has_time
|
||||
and not (time or dttm)
|
||||
):
|
||||
_LOGGER.error(
|
||||
"Invalid service data for %s " "input_datetime.set_datetime: %s",
|
||||
entity.entity_id,
|
||||
str(call.data),
|
||||
)
|
||||
return
|
||||
|
||||
if dttm:
|
||||
@@ -94,8 +109,7 @@ async def async_setup(hass, config):
|
||||
entity.async_set_datetime(date, time)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_SET_DATETIME, SERVICE_SET_DATETIME_SCHEMA,
|
||||
async_set_datetime_service
|
||||
SERVICE_SET_DATETIME, SERVICE_SET_DATETIME_SCHEMA, async_set_datetime_service
|
||||
)
|
||||
|
||||
await component.async_add_entities(entities)
|
||||
@@ -166,42 +180,41 @@ class InputDatetime(RestoreEntity):
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
attrs = {
|
||||
'has_date': self.has_date,
|
||||
'has_time': self.has_time,
|
||||
}
|
||||
attrs = {"has_date": self.has_date, "has_time": self.has_time}
|
||||
|
||||
if self._current_datetime is None:
|
||||
return attrs
|
||||
|
||||
if self.has_date and self._current_datetime is not None:
|
||||
attrs['year'] = self._current_datetime.year
|
||||
attrs['month'] = self._current_datetime.month
|
||||
attrs['day'] = self._current_datetime.day
|
||||
attrs["year"] = self._current_datetime.year
|
||||
attrs["month"] = self._current_datetime.month
|
||||
attrs["day"] = self._current_datetime.day
|
||||
|
||||
if self.has_time and self._current_datetime is not None:
|
||||
attrs['hour'] = self._current_datetime.hour
|
||||
attrs['minute'] = self._current_datetime.minute
|
||||
attrs['second'] = self._current_datetime.second
|
||||
attrs["hour"] = self._current_datetime.hour
|
||||
attrs["minute"] = self._current_datetime.minute
|
||||
attrs["second"] = self._current_datetime.second
|
||||
|
||||
if not self.has_date:
|
||||
attrs['timestamp'] = self._current_datetime.hour * 3600 + \
|
||||
self._current_datetime.minute * 60 + \
|
||||
self._current_datetime.second
|
||||
attrs["timestamp"] = (
|
||||
self._current_datetime.hour * 3600
|
||||
+ self._current_datetime.minute * 60
|
||||
+ self._current_datetime.second
|
||||
)
|
||||
elif not self.has_time:
|
||||
extended = datetime.datetime.combine(self._current_datetime,
|
||||
datetime.time(0, 0))
|
||||
attrs['timestamp'] = extended.timestamp()
|
||||
extended = datetime.datetime.combine(
|
||||
self._current_datetime, datetime.time(0, 0)
|
||||
)
|
||||
attrs["timestamp"] = extended.timestamp()
|
||||
else:
|
||||
attrs['timestamp'] = self._current_datetime.timestamp()
|
||||
attrs["timestamp"] = self._current_datetime.timestamp()
|
||||
|
||||
return attrs
|
||||
|
||||
def async_set_datetime(self, date_val, time_val):
|
||||
"""Set a new date / time."""
|
||||
if self.has_date and self.has_time and date_val and time_val:
|
||||
self._current_datetime = datetime.datetime.combine(date_val,
|
||||
time_val)
|
||||
self._current_datetime = datetime.datetime.combine(date_val, time_val)
|
||||
elif self.has_date and not self.has_time and date_val:
|
||||
self._current_datetime = date_val
|
||||
if self.has_time and not self.has_date and time_val:
|
||||
|
||||
Reference in new issue
Block a user