<commit_before>"""
homeassistant.components.sensor.time_date
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date and Time service.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.time_date/
"""
import logging

import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity

_LOGGER = logging.getLogger(__name__)
OPTION_TYPES = {
    'time': 'Time',
    'date': 'Date',
    'date_time': 'Date & Time',
    'time_date': 'Time & Date',
    'beat': 'Time (beat)',
    'time_utc': 'Time (UTC)',
}


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['display_options']:
        if variable not in OPTION_TYPES:
            _LOGGER.error('Option type: "%s" does not exist', variable)
        else:
            dev.append(TimeDateSensor(variable))

    add_devices(dev)


# pylint: disable=too-few-public-methods
class TimeDateSensor(Entity):
    """ Implements a Time and Date sensor. """

    def __init__(self, option_type):
        self._name = OPTION_TYPES[option_type]
        self.type = option_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.utcnow()
        time = dt_util.datetime_to_time_str(dt_util.as_local(time_date))
        time_utc = dt_util.datetime_to_time_str(time_date)
        date = dt_util.datetime_to_date_str(dt_util.as_local(time_date))

        # Calculate the beat (Swatch Internet Time) time without date.
        hours, minutes, seconds = time_date.strftime('%H:%M:%S').split(':')
        beat = ((int(seconds) + (int(minutes) * 60) + ((int(hours) + 1) *
                                                       3600)) / 86.4)

        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
        elif self.type == 'time_utc':
            self._state = time_utc
        elif self.type == 'beat':
            self._state = '{0:.2f}'.format(beat)
<commit_msg>provide default icon<commit_after>"""
homeassistant.components.sensor.time_date
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date and Time service.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.time_date/
"""
import logging

import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity

_LOGGER = logging.getLogger(__name__)
OPTION_TYPES = {
    'time': 'Time',
    'date': 'Date',
    'date_time': 'Date & Time',
    'time_date': 'Time & Date',
    'beat': 'Time (beat)',
    'time_utc': 'Time (UTC)',
}


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['display_options']:
        if variable not in OPTION_TYPES:
            _LOGGER.error('Option type: "%s" does not exist', variable)
        else:
            dev.append(TimeDateSensor(variable))

    add_devices(dev)


# pylint: disable=too-few-public-methods
class TimeDateSensor(Entity):
    """ Implements a Time and Date sensor. """

    def __init__(self, option_type):
        self._name = OPTION_TYPES[option_type]
        self.type = option_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

    @property
    def icon(self):
        if "date" in self.type and "time" in self.type:
            return "mdi:calendar-clock"
        elif "date" in self.type:
            return "mdi:calendar"
        else:
            return "mdi:clock"

    def update(self):
        """ Gets the latest data and updates the states. """

        time_date = dt_util.utcnow()
        time = dt_util.datetime_to_time_str(dt_util.as_local(time_date))
        time_utc = dt_util.datetime_to_time_str(time_date)
        date = dt_util.datetime_to_date_str(dt_util.as_local(time_date))

        # Calculate the beat (Swatch Internet Time) time without date.
        hours, minutes, seconds = time_date.strftime('%H:%M:%S').split(':')
        beat = ((int(seconds) + (int(minutes) * 60) + ((int(hours) + 1) *
                                                       3600)) / 86.4)

        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
        elif self.type == 'time_utc':
            self._state = time_utc
        elif self.type == 'beat':
            self._state = '{0:.2f}'.format(beat)

{'fraction_non_alphanumeric': 0.13564162056041407, 'fraction_numerical': 0.004283419596644655, 'mean_word_length': 2.412911084043849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140380', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1672, 'n_words': 436}