<commit_before>"""Support for Wunderground weather service."""
from datetime import timedelta
import logging
import requests

import voluptuous as vol

from homeassistant.helpers.entity import Entity
from homeassistant.helpers.config_validation import ensure_list
from homeassistant.util import Throttle
from homeassistant.const import (CONF_PLATFORM, CONF_MONITORED_CONDITIONS,
                                 CONF_API_KEY, TEMP_FAHRENHEIT, TEMP_CELSIUS,
                                 STATE_UNKNOWN)

CONF_PWS_ID = 'pws_id'

_URL_QUERY = '/conditions/q/'
_RESOURCE = 'http://api.wunderground.com/api/'
_LOGGER = logging.getLogger(__name__)

# Return cached results if last scan was less then this time ago.
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)

# Sensor types are defined like: Name, units
SENSOR_TYPES = {
    'weather': ['Weather Summary', None],
    'station_id': ['Station ID', None],
    'feelslike_c': ['Feels Like (°C)', TEMP_CELSIUS],
    'feelslike_f': ['Feels Like (°F)', TEMP_FAHRENHEIT],
    'feelslike_string': ['Feels Like', None],
    'heat_index_c': ['Dewpoint (°C)', TEMP_CELSIUS],
    'heat_index_f': ['Dewpoint (°F)', TEMP_FAHRENHEIT],
    'heat_index_string': ['Heat Index Summary', None],
    'dewpoint_c': ['Dewpoint (°C)', TEMP_CELSIUS],
    'dewpoint_f': ['Dewpoint (°F)', TEMP_FAHRENHEIT],
    'dewpoint_string': ['Dewpoint Summary', None],
    'wind_kph': ['Wind Speed', 'kpH'],
    'wind_mph': ['Wind Speed', 'mpH'],
    'UV': ['UV', None],
    'pressure_in': ['Pressure', 'in'],
    'pressure_mb': ['Pressure', 'mbar'],
    'wind_dir': ['Wind Direction', None],
    'wind_string': ['Wind Summary', None],
    'temp_c': ['Temperature (°C)', TEMP_CELSIUS],
    'temp_f': ['Temperature (°F)', TEMP_FAHRENHEIT],
    'relative_humidity': ['Relative Humidity', '%'],
    'visibility_mi': ['Visibility (miles)', 'mi'],
    'visibility_km': ['Visibility (km)', 'km'],
    'precip_today_in': ['Precipation Today', 'in'],
    'precip_today_metric': ['Precipitation Today', 'mm'],
    'precip_today_string': ['Precipitation today', None],
    'solarradiation': ['Solar Radiation', None]
}

PLATFORM_SCHEMA = vol.Schema({
    vol.Required(CONF_PLATFORM): "wunderground",
    vol.Required(CONF_API_KEY): vol.Coerce(str),
    CONF_PWS_ID: vol.Coerce(str),
    vol.Required(CONF_MONITORED_CONDITIONS, default=[]): ensure_list,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Wunderground sensor."""
    if not PLATFORM_SCHEMA(config):
        return False

    rest = WUndergroundData(hass,
                            config.get(CONF_API_KEY),
                            config.get(CONF_PWS_ID, None))
    sensors = []
    for variable in config['monitored_conditions']:
        if variable in SENSOR_TYPES:
            sensors.append(WUndergroundSensor(rest, variable))
        else:
            _LOGGER.error('Wunderground sensor: "%s" does not exist', variable)
    response = requests.get(_RESOURCE + config.get(CONF_API_KEY) +
                            _URLCONST + config.get(CONF_PWS_ID) +
                            '.json', timeout=10)
    if "error" in response.json()["response"]:
        _LOGGER.error("Check your Wunderground API")
        return False
    else:
        add_devices(sensors)
        rest.update()


class WUndergroundSensor(Entity):
    """Implementing the Wunderground sensor."""

    def __init__(self, rest, condition):
        """Initialize the sensor."""
        self.rest = rest
        self._condition = condition
        self._unit_of_measurement = None
        self.update()

    @property
    def name(self):
        """Return the name of the sensor."""
        return "PWS_" + str(self._condition)

    @property
    def state(self):
        """Return the state of the sensor."""
        value = self.rest.data
        return value[str(self._condition)]

    @property
    def entity_picture(self):
        """Return the entity picture."""
        value = self.rest.data
        if self._condition == 'weather':
            return value['icon_url']

    @property<commit_msg>Catch exception from update on initial platform setup<commit_after>"""Support for Wunderground weather service."""
from datetime import timedelta
import logging
import requests

import voluptuous as vol

from homeassistant.helpers.entity import Entity
from homeassistant.helpers.config_validation import ensure_list
from homeassistant.util import Throttle
from homeassistant.const import (CONF_PLATFORM, CONF_MONITORED_CONDITIONS,
                                 CONF_API_KEY, TEMP_FAHRENHEIT, TEMP_CELSIUS,
                                 STATE_UNKNOWN)

CONF_PWS_ID = 'pws_id'

_URL_QUERY = '/conditions/q/'
_RESOURCE = 'http://api.wunderground.com/api/'
_LOGGER = logging.getLogger(__name__)

# Return cached results if last scan was less then this time ago.
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)

# Sensor types are defined like: Name, units
SENSOR_TYPES = {
    'weather': ['Weather Summary', None],
    'station_id': ['Station ID', None],
    'feelslike_c': ['Feels Like (°C)', TEMP_CELSIUS],
    'feelslike_f': ['Feels Like (°F)', TEMP_FAHRENHEIT],
    'feelslike_string': ['Feels Like', None],
    'heat_index_c': ['Dewpoint (°C)', TEMP_CELSIUS],
    'heat_index_f': ['Dewpoint (°F)', TEMP_FAHRENHEIT],
    'heat_index_string': ['Heat Index Summary', None],
    'dewpoint_c': ['Dewpoint (°C)', TEMP_CELSIUS],
    'dewpoint_f': ['Dewpoint (°F)', TEMP_FAHRENHEIT],
    'dewpoint_string': ['Dewpoint Summary', None],
    'wind_kph': ['Wind Speed', 'kpH'],
    'wind_mph': ['Wind Speed', 'mpH'],
    'UV': ['UV', None],
    'pressure_in': ['Pressure', 'in'],
    'pressure_mb': ['Pressure', 'mbar'],
    'wind_dir': ['Wind Direction', None],
    'wind_string': ['Wind Summary', None],
    'temp_c': ['Temperature (°C)', TEMP_CELSIUS],
    'temp_f': ['Temperature (°F)', TEMP_FAHRENHEIT],
    'relative_humidity': ['Relative Humidity', '%'],
    'visibility_mi': ['Visibility (miles)', 'mi'],
    'visibility_km': ['Visibility (km)', 'km'],
    'precip_today_in': ['Precipation Today', 'in'],
    'precip_today_metric': ['Precipitation Today', 'mm'],
    'precip_today_string': ['Precipitation today', None],
    'solarradiation': ['Solar Radiation', None]
}

PLATFORM_SCHEMA = vol.Schema({
    vol.Required(CONF_PLATFORM): "wunderground",
    vol.Required(CONF_API_KEY): vol.Coerce(str),
    CONF_PWS_ID: vol.Coerce(str),
    vol.Required(CONF_MONITORED_CONDITIONS, default=[]): ensure_list,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Wunderground sensor."""
    if not PLATFORM_SCHEMA(config):
        return False

    rest = WUndergroundData(hass,
                            config.get(CONF_API_KEY),
                            config.get(CONF_PWS_ID, None))
    sensors = []
    for variable in config['monitored_conditions']:
        if variable in SENSOR_TYPES:
            sensors.append(WUndergroundSensor(rest, variable))
        else:
            _LOGGER.error('Wunderground sensor: "%s" does not exist', variable)
    response = requests.get(_RESOURCE + config.get(CONF_API_KEY) +
                            _URLCONST + config.get(CONF_PWS_ID) +
                            '.json', timeout=10)
    if "error" in response.json()["response"]:
        _LOGGER.error("Check your Wunderground API")
        return False
    else:
        add_devices(sensors)

    try:
        rest.update()
    except ValueError as err:
        _LOGGER.error("Received error from WUnderground: %s", err)
        return False

    add_devices(sensors)

    return True


class WUndergroundSensor(Entity):
    """Implementing the Wunderground sensor."""

    def __init__(self, rest, condition):
        """Initialize the sensor."""
        self.rest = rest
        self._condition = condition
        self._unit_of_measurement = None
        self.update()

    @property
    def name(self):
        """Return the name of the sensor."""
        return "PWS_" + str(self._condition)

    @property
    def state(self):
        """Return the state of the sensor."""
        value = self.rest.data
        return value[str(self._condition)]

    @property
    def entity_picture(self):
        """Return the entity picture."""
        value = self.rest.data
        if self._condition == 'weather':
            return value['icon_url']

    @property
{'fraction_non_alphanumeric': 0.1369536739311659, 'fraction_numerical': 0.0011909015124449208, 'mean_word_length': 3.215863453815261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7550318', 'n_tokens_mistral': 2756, 'n_tokens_neox': 2681, 'n_words': 630}