mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
commit
b6c88b1a6c
@ -39,6 +39,7 @@ omit =
|
|||||||
homeassistant/components/sensor/openweathermap.py
|
homeassistant/components/sensor/openweathermap.py
|
||||||
homeassistant/components/sensor/sabnzbd.py
|
homeassistant/components/sensor/sabnzbd.py
|
||||||
homeassistant/components/sensor/systemmonitor.py
|
homeassistant/components/sensor/systemmonitor.py
|
||||||
|
homeassistant/components/sensor/time_date.py
|
||||||
homeassistant/components/sensor/transmission.py
|
homeassistant/components/sensor/transmission.py
|
||||||
homeassistant/components/switch/wemo.py
|
homeassistant/components/switch/wemo.py
|
||||||
homeassistant/components/thermostat/nest.py
|
homeassistant/components/thermostat/nest.py
|
||||||
|
100
homeassistant/components/sensor/time_date.py
Normal file
100
homeassistant/components/sensor/time_date.py
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
"""
|
||||||
|
homeassistant.components.sensor.time_date
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Date and Time service.
|
||||||
|
|
||||||
|
Configuration:
|
||||||
|
|
||||||
|
To use the Date and Time sensor you will need to add something like the
|
||||||
|
following to your config/configuration.yaml
|
||||||
|
|
||||||
|
sensor:
|
||||||
|
platform: time_date
|
||||||
|
monitored_variables:
|
||||||
|
- type: 'time'
|
||||||
|
- type: 'date'
|
||||||
|
- type: 'date_time'
|
||||||
|
- type: 'time_date'
|
||||||
|
|
||||||
|
VARIABLES:
|
||||||
|
|
||||||
|
monitored_variables
|
||||||
|
*Required
|
||||||
|
An array specifying the variables to monitor.
|
||||||
|
|
||||||
|
These are the variables for the monitored_variables array:
|
||||||
|
|
||||||
|
type
|
||||||
|
*Required
|
||||||
|
The variable you wish to display, see the configuration example above for a
|
||||||
|
list of all available variables
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
SENSOR_TYPES = {
|
||||||
|
'time': 'Time',
|
||||||
|
'date': 'Date',
|
||||||
|
'date_time': 'Date & Time',
|
||||||
|
'time_date': 'Time & Date'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
""" Get the Time and Date sensor. """
|
||||||
|
|
||||||
|
if hass.config.time_zone is None:
|
||||||
|
_LOGGER.error("Timezone is not set in Home Assistant config")
|
||||||
|
return False
|
||||||
|
|
||||||
|
dev = []
|
||||||
|
for variable in config['monitored_variables']:
|
||||||
|
if variable['type'] not in SENSOR_TYPES:
|
||||||
|
_LOGGER.error('Sensor type: "%s" does not exist', variable['type'])
|
||||||
|
else:
|
||||||
|
dev.append(TimeDateSensor(variable['type']))
|
||||||
|
|
||||||
|
add_devices(dev)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
class TimeDateSensor(Entity):
|
||||||
|
""" Implements a Time and Date sensor. """
|
||||||
|
|
||||||
|
def __init__(self, sensor_type):
|
||||||
|
self._name = SENSOR_TYPES[sensor_type]
|
||||||
|
self.type = sensor_type
|
||||||
|
self._state = None
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
""" Returns the name of the device. """
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
""" Returns the state of the device. """
|
||||||
|
return self._state
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
""" Gets the latest data and updates the states. """
|
||||||
|
|
||||||
|
time_date = dt_util.now()
|
||||||
|
time = dt_util.datetime_to_short_time_str(time_date)
|
||||||
|
date = dt_util.datetime_to_short_date_str(time_date)
|
||||||
|
|
||||||
|
if self.type == 'time':
|
||||||
|
self._state = time
|
||||||
|
elif self.type == 'date':
|
||||||
|
self._state = date
|
||||||
|
elif self.type == 'date_time':
|
||||||
|
self._state = date + ', ' + time
|
||||||
|
elif self.type == 'time_date':
|
||||||
|
self._state = time + ', ' + date
|
@ -10,6 +10,8 @@ import datetime as dt
|
|||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
DATE_STR_FORMAT = "%H:%M:%S %d-%m-%Y"
|
DATE_STR_FORMAT = "%H:%M:%S %d-%m-%Y"
|
||||||
|
DATE_SHORT_STR_FORMAT = "%Y-%m-%d"
|
||||||
|
TIME_SHORT_STR_FORMAT = "%H:%M"
|
||||||
UTC = DEFAULT_TIME_ZONE = pytz.utc
|
UTC = DEFAULT_TIME_ZONE = pytz.utc
|
||||||
|
|
||||||
|
|
||||||
@ -79,6 +81,22 @@ def datetime_to_str(dattim):
|
|||||||
return dattim.strftime(DATE_STR_FORMAT)
|
return dattim.strftime(DATE_STR_FORMAT)
|
||||||
|
|
||||||
|
|
||||||
|
def datetime_to_short_time_str(dattim):
|
||||||
|
""" Converts datetime to a string format as short time.
|
||||||
|
|
||||||
|
@rtype : str
|
||||||
|
"""
|
||||||
|
return dattim.strftime(TIME_SHORT_STR_FORMAT)
|
||||||
|
|
||||||
|
|
||||||
|
def datetime_to_short_date_str(dattim):
|
||||||
|
""" Converts datetime to a string format as short date.
|
||||||
|
|
||||||
|
@rtype : str
|
||||||
|
"""
|
||||||
|
return dattim.strftime(DATE_SHORT_STR_FORMAT)
|
||||||
|
|
||||||
|
|
||||||
def str_to_datetime(dt_str):
|
def str_to_datetime(dt_str):
|
||||||
""" Converts a string to a UTC datetime object.
|
""" Converts a string to a UTC datetime object.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user