mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Add worldclock sensor
This commit is contained in:
parent
707ca4b752
commit
58ac4be24c
80
homeassistant/components/sensor/worldclock.py
Normal file
80
homeassistant/components/sensor/worldclock.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
"""
|
||||||
|
homeassistant.components.sensor.worldclock
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
The Worldclock sensor let you display the current time of a different time
|
||||||
|
zone.
|
||||||
|
|
||||||
|
Configuration:
|
||||||
|
|
||||||
|
To use the Worldclock sensor you will need to add something like the
|
||||||
|
following to your configuration.yaml file.
|
||||||
|
|
||||||
|
sensor:
|
||||||
|
platform: worldclock
|
||||||
|
time_zone: America/New_York
|
||||||
|
name: New York
|
||||||
|
|
||||||
|
Variables:
|
||||||
|
|
||||||
|
time_zone
|
||||||
|
*Required
|
||||||
|
Time zone you want to display.
|
||||||
|
|
||||||
|
name
|
||||||
|
*Optional
|
||||||
|
Name of the sensor to use in the frontend.
|
||||||
|
|
||||||
|
For more details about this platform, please refer to the documentation at
|
||||||
|
https://home-assistant.io/components/sensor.worldclock.html
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
DEFAULT_NAME = "Worldclock Sensor"
|
||||||
|
|
||||||
|
|
||||||
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
""" Get the Worldclock sensor. """
|
||||||
|
|
||||||
|
try:
|
||||||
|
time_zone = dt_util.get_time_zone(config.get('time_zone'))
|
||||||
|
except AttributeError:
|
||||||
|
_LOGGER.error("time_zone in platform configuration is missing.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if time_zone is None:
|
||||||
|
_LOGGER.error("Timezone '%s' is not valid.", config.get('time_zone'))
|
||||||
|
return False
|
||||||
|
|
||||||
|
add_devices([WorldClockSensor(
|
||||||
|
time_zone,
|
||||||
|
config.get('name', DEFAULT_NAME)
|
||||||
|
)])
|
||||||
|
|
||||||
|
|
||||||
|
class WorldClockSensor(Entity):
|
||||||
|
""" Implements a Worldclock sensor. """
|
||||||
|
|
||||||
|
def __init__(self, time_zone, name):
|
||||||
|
self._name = name
|
||||||
|
self._time_zone = time_zone
|
||||||
|
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 time and updates the states. """
|
||||||
|
self._state = dt_util.datetime_to_time_str(
|
||||||
|
dt_util.now(time_zone=self._time_zone))
|
Loading…
x
Reference in New Issue
Block a user