core/homeassistant/components/darksky/sensor.py

833 lines
22 KiB
Python
Raw Normal View History

"""Support for Dark Sky weather service."""
2018-10-25 07:33:23 +00:00
import logging
from datetime import timedelta
2016-09-03 07:35:33 +00:00
import forecastio
import voluptuous as vol
2019-07-31 19:25:30 +00:00
from requests.exceptions import ConnectionError as ConnectError, HTTPError, Timeout
2015-06-17 19:59:38 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
2016-09-03 07:35:33 +00:00
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_ATTRIBUTION,
CONF_API_KEY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
UNIT_UV_INDEX,
CONF_SCAN_INTERVAL,
)
2015-06-17 19:59:38 +00:00
from homeassistant.helpers.entity import Entity
2016-02-19 05:27:50 +00:00
from homeassistant.util import Throttle
2015-06-17 19:59:38 +00:00
_LOGGER = logging.getLogger(__name__)
ATTRIBUTION = "Powered by Dark Sky"
2019-07-31 19:25:30 +00:00
CONF_FORECAST = "forecast"
CONF_HOURLY_FORECAST = "hourly_forecast"
CONF_LANGUAGE = "language"
CONF_UNITS = "units"
2019-07-31 19:25:30 +00:00
DEFAULT_LANGUAGE = "en"
DEFAULT_NAME = "Dark Sky"
SCAN_INTERVAL = timedelta(seconds=300)
2016-09-03 07:35:33 +00:00
DEPRECATED_SENSOR_TYPES = {
2019-07-31 19:25:30 +00:00
"apparent_temperature_max",
"apparent_temperature_min",
"temperature_max",
"temperature_min",
}
# Sensor types are defined like so:
# Name, si unit, us unit, ca unit, uk unit, uk2 unit
2015-06-17 19:59:38 +00:00
SENSOR_TYPES = {
2019-07-31 19:25:30 +00:00
"summary": [
"Summary",
None,
None,
None,
None,
None,
None,
["currently", "hourly", "daily"],
],
"minutely_summary": ["Minutely Summary", None, None, None, None, None, None, []],
"hourly_summary": ["Hourly Summary", None, None, None, None, None, None, []],
"daily_summary": ["Daily Summary", None, None, None, None, None, None, []],
"icon": [
"Icon",
None,
None,
None,
None,
None,
None,
["currently", "hourly", "daily"],
],
"nearest_storm_distance": [
"Nearest Storm Distance",
"km",
"mi",
"km",
"km",
"mi",
"mdi:weather-lightning",
["currently"],
],
"nearest_storm_bearing": [
"Nearest Storm Bearing",
"°",
"°",
"°",
"°",
"°",
"mdi:weather-lightning",
["currently"],
],
"precip_type": [
"Precip",
None,
None,
None,
None,
None,
"mdi:weather-pouring",
["currently", "minutely", "hourly", "daily"],
],
"precip_intensity": [
"Precip Intensity",
"mm/h",
"in",
"mm/h",
"mm/h",
"mm/h",
"mdi:weather-rainy",
["currently", "minutely", "hourly", "daily"],
],
"precip_probability": [
"Precip Probability",
"%",
"%",
"%",
"%",
"%",
"mdi:water-percent",
["currently", "minutely", "hourly", "daily"],
],
"precip_accumulation": [
"Precip Accumulation",
"cm",
"in",
"cm",
"cm",
"cm",
"mdi:weather-snowy",
["hourly", "daily"],
],
"temperature": [
"Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["currently", "hourly"],
],
"apparent_temperature": [
"Apparent Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["currently", "hourly"],
],
"dew_point": [
"Dew Point",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["currently", "hourly", "daily"],
],
"wind_speed": [
"Wind Speed",
"m/s",
"mph",
"km/h",
"mph",
"mph",
"mdi:weather-windy",
["currently", "hourly", "daily"],
],
"wind_bearing": [
"Wind Bearing",
"°",
"°",
"°",
"°",
"°",
"mdi:compass",
["currently", "hourly", "daily"],
],
"wind_gust": [
"Wind Gust",
"m/s",
"mph",
"km/h",
"mph",
"mph",
"mdi:weather-windy-variant",
["currently", "hourly", "daily"],
],
"cloud_cover": [
"Cloud Coverage",
"%",
"%",
"%",
"%",
"%",
"mdi:weather-partly-cloudy",
2019-07-31 19:25:30 +00:00
["currently", "hourly", "daily"],
],
"humidity": [
"Humidity",
"%",
"%",
"%",
"%",
"%",
"mdi:water-percent",
["currently", "hourly", "daily"],
],
"pressure": [
"Pressure",
"mbar",
"mbar",
"mbar",
"mbar",
"mbar",
"mdi:gauge",
["currently", "hourly", "daily"],
],
"visibility": [
"Visibility",
"km",
"mi",
"km",
"km",
"mi",
"mdi:eye",
["currently", "hourly", "daily"],
],
"ozone": [
"Ozone",
"DU",
"DU",
"DU",
"DU",
"DU",
"mdi:eye",
["currently", "hourly", "daily"],
],
"apparent_temperature_max": [
"Daily High Apparent Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"apparent_temperature_high": [
"Daytime High Apparent Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"apparent_temperature_min": [
"Daily Low Apparent Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"apparent_temperature_low": [
"Overnight Low Apparent Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"temperature_max": [
"Daily High Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"temperature_high": [
"Daytime High Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"temperature_min": [
"Daily Low Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"temperature_low": [
"Overnight Low Temperature",
"°C",
"°F",
"°C",
"°C",
"°C",
"mdi:thermometer",
["daily"],
],
"precip_intensity_max": [
"Daily Max Precip Intensity",
"mm/h",
"in",
"mm/h",
"mm/h",
"mm/h",
"mdi:thermometer",
["daily"],
],
"uv_index": [
"UV Index",
UNIT_UV_INDEX,
UNIT_UV_INDEX,
UNIT_UV_INDEX,
UNIT_UV_INDEX,
UNIT_UV_INDEX,
"mdi:weather-sunny",
["currently", "hourly", "daily"],
],
"moon_phase": [
"Moon Phase",
None,
None,
None,
None,
None,
"mdi:weather-night",
["daily"],
],
"sunrise_time": [
"Sunrise",
None,
None,
None,
None,
None,
"mdi:white-balance-sunny",
["daily"],
],
"sunset_time": [
"Sunset",
None,
None,
None,
None,
None,
"mdi:weather-night",
["daily"],
],
"alerts": ["Alerts", None, None, None, None, None, "mdi:alert-circle-outline", []],
2015-06-17 19:59:38 +00:00
}
CONDITION_PICTURES = {
2019-07-31 19:25:30 +00:00
"clear-day": ["/static/images/darksky/weather-sunny.svg", "mdi:weather-sunny"],
"clear-night": ["/static/images/darksky/weather-night.svg", "mdi:weather-night"],
2019-07-31 19:25:30 +00:00
"rain": ["/static/images/darksky/weather-pouring.svg", "mdi:weather-pouring"],
"snow": ["/static/images/darksky/weather-snowy.svg", "mdi:weather-snowy"],
"sleet": ["/static/images/darksky/weather-hail.svg", "mdi:weather-snowy-rainy"],
"wind": ["/static/images/darksky/weather-windy.svg", "mdi:weather-windy"],
"fog": ["/static/images/darksky/weather-fog.svg", "mdi:weather-fog"],
"cloudy": ["/static/images/darksky/weather-cloudy.svg", "mdi:weather-cloudy"],
"partly-cloudy-day": [
"/static/images/darksky/weather-partlycloudy.svg",
"mdi:weather-partly-cloudy",
2019-07-31 19:25:30 +00:00
],
"partly-cloudy-night": [
"/static/images/darksky/weather-cloudy.svg",
"mdi:weather-partly-cloudy",
2019-07-31 19:25:30 +00:00
],
}
# Language Supported Codes
LANGUAGE_CODES = [
2019-07-31 19:25:30 +00:00
"ar",
"az",
"be",
"bg",
"bn",
"bs",
"ca",
"cs",
"da",
"de",
"el",
"en",
"ja",
"ka",
"kn",
"ko",
"eo",
"es",
"et",
"fi",
"fr",
"he",
"hi",
"hr",
"hu",
"id",
"is",
"it",
"kw",
"lv",
"ml",
"mr",
"nb",
"nl",
"pa",
"pl",
"pt",
"ro",
"ru",
"sk",
"sl",
"sr",
"sv",
"ta",
"te",
"tet",
"tr",
"uk",
"ur",
"x-pig-latin",
"zh",
"zh-tw",
]
2019-07-31 19:25:30 +00:00
ALLOWED_UNITS = ["auto", "si", "us", "ca", "uk", "uk2"]
ALERTS_ATTRS = ["time", "description", "expires", "severity", "uri", "regions", "title"]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MONITORED_CONDITIONS): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNITS): vol.In(ALLOWED_UNITS),
vol.Optional(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): vol.In(LANGUAGE_CODES),
vol.Inclusive(
CONF_LATITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.latitude,
vol.Inclusive(
CONF_LONGITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.longitude,
vol.Optional(CONF_FORECAST): vol.All(cv.ensure_list, [vol.Range(min=0, max=7)]),
vol.Optional(CONF_HOURLY_FORECAST): vol.All(
cv.ensure_list, [vol.Range(min=0, max=48)]
),
}
)
2015-06-17 19:59:38 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Dark Sky sensor."""
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
2017-03-19 18:00:13 +00:00
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
language = config.get(CONF_LANGUAGE)
interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL)
2015-06-17 19:59:38 +00:00
if CONF_UNITS in config:
units = config[CONF_UNITS]
Add unit system support Add unit symbol constants Initial unit system object Import more constants Pydoc for unit system file Import constants for configuration validation Unit system validation method Typing for constants Inches are valid lengths too Typings Change base class to dict - needed for remote api call serialization Validation Use dictionary keys Defined unit systems Update location util to use metric instead of us fahrenheit Update constant imports Import defined unit systems Update configuration to use unit system Update schema to use unit system Update constants Add imports to core for unit system and distance Type for config Default unit system Convert distance from HASS instance Update temperature conversion to use unit system Update temperature conversion Set unit system based on configuration Set info unit system Return unit system dictionary with config dictionary Auto discover unit system Update location test for use metric Update forecast unit system Update mold indicator unit system Update thermostat unit system Update thermostat demo test Unit tests around unit system Update test common hass configuration Update configuration unit tests There should always be a unit system! Update core unit tests Constants typing Linting issues Remove unused import Update fitbit sensor to use application unit system Update google travel time to use application unit system Update configuration example Update dht sensor Update DHT temperature conversion to use the utility function Update swagger config Update my sensors metric flag Update hvac component temperature conversion HVAC conversion for temperature Pull unit from sensor type map Pull unit from sensor type map Update the temper sensor unit Update yWeather sensor unit Update hvac demo unit test Set unit test config unit system to metric Use hass unit system length for default in proximity Use the name of the system instead of temperature Use constants from const Unused import Forecasted temperature Fix calculation in case furthest distance is greater than 1000000 units Remove unneeded constants Set default length to km or miles Use constants Linting doesn't like importing just for typing Fix reference Test is expecting meters - set config to meters Use constant Use constant PyDoc for unit test Should be not in Rename to units Change unit system to be an object - not a dictionary Return tuple in conversion Move convert to temperature util Temperature conversion is now in unit system Update imports Rename to units Units is now an object Use temperature util conversion Unit system is now an object Validate and convert unit system config Return the scalar value in template distance Test is expecting meters Update unit tests around unit system Distance util returns tuple Fix location info test Set units Update unit tests Convert distance DOH Pull out the scalar from the vector Linting I really hate python linting Linting again BLARG Unit test documentation Unit test around is metric flag Break ternary statement into if/else blocks Don't use dictionary - use members is metric flag Rename constants Use is metric flag Move constants to CONST file Move to const file Raise error if unit is not expected Typing No need to return unit since only performing conversion if it can work Use constants Line wrapping Raise error if invalid value Remove subscripts from conversion as they are no longer returned as tuples No longer tuples No longer tuples Check for numeric type Fix string format to use correct variable Typing Assert errors raised Remove subscript Only convert temperature if we know the unit If no unit of measurement set - default to HASS config Convert only if we know the unit Remove subscription Fix not in clause Linting fixes Wants a boolean Clearer if-block Check if the key is in the config first Missed a couple expecting tuples Backwards compatibility No like-y ternary! Error handling around state setting Pretty unit system configuration validation More tuple crap Use is metric flag Error handling around min/max temp Explode if no unit Pull unit from config Celsius has a decimal Unused import Check if it's a temperature before we try to convert it to a temperature Linting says too many statements - combine lat/long in a fairly reasonable manner Backwards compatibility unit test Better doc
2016-07-31 20:24:49 +00:00
elif hass.config.units.is_metric:
2019-07-31 19:25:30 +00:00
units = "si"
else:
2019-07-31 19:25:30 +00:00
units = "us"
forecast_data = DarkSkyData(
2019-07-31 19:25:30 +00:00
api_key=config.get(CONF_API_KEY, None),
latitude=latitude,
longitude=longitude,
units=units,
language=language,
interval=interval,
)
forecast_data.update()
forecast_data.update_currently()
# If connection failed don't setup platform.
if forecast_data.data is None:
2018-10-25 07:33:23 +00:00
return
2015-06-17 19:59:38 +00:00
name = config.get(CONF_NAME)
Added forecast support to DarkSky (#5264) * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Fix async_volume_up / async_volume_down (#5249) async_volume_up / async_volume_down should be async versions of volume_up / volume_down, not a async version of the default variants of volume_up / volume_down. The previous code always called into the mediaplayers set_volume_level, and never into volume_up / volume_down. Signed-off-by: Anton Lundin <glance@acc.umu.se> * adding a default icon "blind" to a PowerView blinds scene. (#5210) * adding a default icon "blind" to a PowerView blinds scene. * Adding icon property to define blind icon. Removed it from the state attributes dict. * fixing lint error * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Use SHA hash to make token harder to guess (#5258) * Use SHA hash to make token harder to guess Use hashlib SHA256 to encode object id instead of using it directly. * Cache access token Instead of generating a token on the fly cache it in the constructor. * Fix lint * Bugfix async device_tracker see callback (#5259) * Add support for NAD receivers (#5191) * Add support for NAD receivers * remove self.update() in various methods * remove setting attributes in various methods * Change import to hass style * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Revert "Bugfix camera streams (#5306)" This reverts commit 4b43537801a5c088329f6b12c99c95fdb2eb0e9c. Revert "Version bump for kodi dependency (#5307)" This reverts commit 6abad6b76e610b1bfb13f3f9342a2a0a53971fcf. Revert "Add HMWIOSwitch to sensor, binary (#5304)" This reverts commit 2c3f55acc4cc8890e54bf6a94f5a960eee28c486. Revert "Remove GTFS default name & string change" This reverts commit 6000c59bb559b8e37553b3f0def79c2bd84f2af2. Revert "Update pyhomematic 1.19 & small cleanups (#5299)" This reverts commit a30711f1a0e2d4a286799d714fe59ff147883fab. Revert "[sensor] Add Dublin bus RTPI sensor (#5257)" This reverts commit 1219ca3c3bc083c8f919c4db7eb3670686e52861. Revert "Bugfix group reload (#5292)" This reverts commit baa8e53e66167a1fb0f9d090f28325454ad3d4ef. Revert "Support for TrackR device trackers (#5010)" This reverts commit f7a1d63d52dc7687a07cd2c52ef4e8e6894e45d9. Revert "Bump pywemo version." This reverts commit dc937cc8cffbb9ec2b4342d801f8d7332a8dd9cf. Revert "Upgrade to voluptuous to 0.9.3 (#5288)" This reverts commit d12decc4714cb61af58ab08581712b8be5367960. Revert "Upgrade distro to 1.0.2 (#5291)" This reverts commit 64800fd48c02520b1f44be960dc8c539f82d1692. Revert "Don't build Adafruit_BBIO - doesn't work on all platforms. (#5281)" This reverts commit 9a3c0c8cd3a06d118cfcf58d1078912e41f12f31. Revert "Convert flic to synchronous platform. (#5276)" This reverts commit eb9b95c2922181b097258856af9bd2bc4d7a814e. Revert "Upgrade to aiohttp 1.2 (#4964)" This reverts commit e68e29e03ebd43175761d1ae2b4e598d382d2cf4. Revert "Fix TCP sensor to correctly use value_template (#5211)" This reverts commit 1cf9ae5a01d663bb9e3d3e38741b2ae818b36f93. Revert "Cleanup language support on TTS (#5255)" This reverts commit 3f3a3bcc8ac7eec2e5e9eba9981c74db3842f22d. Revert "Add last triggered to script (#5261)" This reverts commit 467cb18625da9323f743ed62a342e446a79fb05b. Revert "Bump flux_led version and make use of PyPi package (#5267)" This reverts commit 34a9fb01ac1fb9568f18677be5faf3d23ab7dc2a. Revert "Add support for NAD receivers (#5191)" This reverts commit 3b59e169f1bc11b3887bc98b2f8425f6c70a0df2. Revert "Bugfix async device_tracker see callback (#5259)" This reverts commit 71fddd26eb9c9ffe6cbd809298f07e17aad152a4. Revert "Use SHA hash to make token harder to guess (#5258)" This reverts commit 922308bc1f7a2a0a769a8c29d663c90a97a0583b. * Revert "Revert "Bugfix camera streams (#5306)"" This reverts commit 2ee8c44021cf9c3a91d20f9ee26752aa8369d2e6. * Update darksky.py
2017-01-27 06:32:45 +00:00
forecast = config.get(CONF_FORECAST)
forecast_hour = config.get(CONF_HOURLY_FORECAST)
sensors = []
for variable in config[CONF_MONITORED_CONDITIONS]:
if variable in DEPRECATED_SENSOR_TYPES:
2018-10-25 07:33:23 +00:00
_LOGGER.warning("Monitored condition %s is deprecated", variable)
2019-07-31 19:25:30 +00:00
if not SENSOR_TYPES[variable][7] or "currently" in SENSOR_TYPES[variable][7]:
if variable == "alerts":
sensors.append(DarkSkyAlertSensor(forecast_data, variable, name))
else:
sensors.append(DarkSkySensor(forecast_data, variable, name))
2019-07-31 19:25:30 +00:00
if forecast is not None and "daily" in SENSOR_TYPES[variable][7]:
Added forecast support to DarkSky (#5264) * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Fix async_volume_up / async_volume_down (#5249) async_volume_up / async_volume_down should be async versions of volume_up / volume_down, not a async version of the default variants of volume_up / volume_down. The previous code always called into the mediaplayers set_volume_level, and never into volume_up / volume_down. Signed-off-by: Anton Lundin <glance@acc.umu.se> * adding a default icon "blind" to a PowerView blinds scene. (#5210) * adding a default icon "blind" to a PowerView blinds scene. * Adding icon property to define blind icon. Removed it from the state attributes dict. * fixing lint error * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Use SHA hash to make token harder to guess (#5258) * Use SHA hash to make token harder to guess Use hashlib SHA256 to encode object id instead of using it directly. * Cache access token Instead of generating a token on the fly cache it in the constructor. * Fix lint * Bugfix async device_tracker see callback (#5259) * Add support for NAD receivers (#5191) * Add support for NAD receivers * remove self.update() in various methods * remove setting attributes in various methods * Change import to hass style * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Revert "Bugfix camera streams (#5306)" This reverts commit 4b43537801a5c088329f6b12c99c95fdb2eb0e9c. Revert "Version bump for kodi dependency (#5307)" This reverts commit 6abad6b76e610b1bfb13f3f9342a2a0a53971fcf. Revert "Add HMWIOSwitch to sensor, binary (#5304)" This reverts commit 2c3f55acc4cc8890e54bf6a94f5a960eee28c486. Revert "Remove GTFS default name & string change" This reverts commit 6000c59bb559b8e37553b3f0def79c2bd84f2af2. Revert "Update pyhomematic 1.19 & small cleanups (#5299)" This reverts commit a30711f1a0e2d4a286799d714fe59ff147883fab. Revert "[sensor] Add Dublin bus RTPI sensor (#5257)" This reverts commit 1219ca3c3bc083c8f919c4db7eb3670686e52861. Revert "Bugfix group reload (#5292)" This reverts commit baa8e53e66167a1fb0f9d090f28325454ad3d4ef. Revert "Support for TrackR device trackers (#5010)" This reverts commit f7a1d63d52dc7687a07cd2c52ef4e8e6894e45d9. Revert "Bump pywemo version." This reverts commit dc937cc8cffbb9ec2b4342d801f8d7332a8dd9cf. Revert "Upgrade to voluptuous to 0.9.3 (#5288)" This reverts commit d12decc4714cb61af58ab08581712b8be5367960. Revert "Upgrade distro to 1.0.2 (#5291)" This reverts commit 64800fd48c02520b1f44be960dc8c539f82d1692. Revert "Don't build Adafruit_BBIO - doesn't work on all platforms. (#5281)" This reverts commit 9a3c0c8cd3a06d118cfcf58d1078912e41f12f31. Revert "Convert flic to synchronous platform. (#5276)" This reverts commit eb9b95c2922181b097258856af9bd2bc4d7a814e. Revert "Upgrade to aiohttp 1.2 (#4964)" This reverts commit e68e29e03ebd43175761d1ae2b4e598d382d2cf4. Revert "Fix TCP sensor to correctly use value_template (#5211)" This reverts commit 1cf9ae5a01d663bb9e3d3e38741b2ae818b36f93. Revert "Cleanup language support on TTS (#5255)" This reverts commit 3f3a3bcc8ac7eec2e5e9eba9981c74db3842f22d. Revert "Add last triggered to script (#5261)" This reverts commit 467cb18625da9323f743ed62a342e446a79fb05b. Revert "Bump flux_led version and make use of PyPi package (#5267)" This reverts commit 34a9fb01ac1fb9568f18677be5faf3d23ab7dc2a. Revert "Add support for NAD receivers (#5191)" This reverts commit 3b59e169f1bc11b3887bc98b2f8425f6c70a0df2. Revert "Bugfix async device_tracker see callback (#5259)" This reverts commit 71fddd26eb9c9ffe6cbd809298f07e17aad152a4. Revert "Use SHA hash to make token harder to guess (#5258)" This reverts commit 922308bc1f7a2a0a769a8c29d663c90a97a0583b. * Revert "Revert "Bugfix camera streams (#5306)"" This reverts commit 2ee8c44021cf9c3a91d20f9ee26752aa8369d2e6. * Update darksky.py
2017-01-27 06:32:45 +00:00
for forecast_day in forecast:
2019-07-31 19:25:30 +00:00
sensors.append(
DarkSkySensor(
forecast_data, variable, name, forecast_day=forecast_day
)
)
if forecast_hour is not None and "hourly" in SENSOR_TYPES[variable][7]:
for forecast_h in forecast_hour:
2019-07-31 19:25:30 +00:00
sensors.append(
DarkSkySensor(
forecast_data, variable, name, forecast_hour=forecast_h
)
)
2015-06-17 19:59:38 +00:00
add_entities(sensors, True)
2015-06-17 19:59:38 +00:00
class DarkSkySensor(Entity):
"""Implementation of a Dark Sky sensor."""
2015-06-17 19:59:38 +00:00
2019-07-31 19:25:30 +00:00
def __init__(
self, forecast_data, sensor_type, name, forecast_day=None, forecast_hour=None
):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
self.client_name = name
2015-06-17 19:59:38 +00:00
self._name = SENSOR_TYPES[sensor_type][0]
self.forecast_data = forecast_data
2015-06-17 19:59:38 +00:00
self.type = sensor_type
Added forecast support to DarkSky (#5264) * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Fix async_volume_up / async_volume_down (#5249) async_volume_up / async_volume_down should be async versions of volume_up / volume_down, not a async version of the default variants of volume_up / volume_down. The previous code always called into the mediaplayers set_volume_level, and never into volume_up / volume_down. Signed-off-by: Anton Lundin <glance@acc.umu.se> * adding a default icon "blind" to a PowerView blinds scene. (#5210) * adding a default icon "blind" to a PowerView blinds scene. * Adding icon property to define blind icon. Removed it from the state attributes dict. * fixing lint error * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Use SHA hash to make token harder to guess (#5258) * Use SHA hash to make token harder to guess Use hashlib SHA256 to encode object id instead of using it directly. * Cache access token Instead of generating a token on the fly cache it in the constructor. * Fix lint * Bugfix async device_tracker see callback (#5259) * Add support for NAD receivers (#5191) * Add support for NAD receivers * remove self.update() in various methods * remove setting attributes in various methods * Change import to hass style * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Revert "Bugfix camera streams (#5306)" This reverts commit 4b43537801a5c088329f6b12c99c95fdb2eb0e9c. Revert "Version bump for kodi dependency (#5307)" This reverts commit 6abad6b76e610b1bfb13f3f9342a2a0a53971fcf. Revert "Add HMWIOSwitch to sensor, binary (#5304)" This reverts commit 2c3f55acc4cc8890e54bf6a94f5a960eee28c486. Revert "Remove GTFS default name & string change" This reverts commit 6000c59bb559b8e37553b3f0def79c2bd84f2af2. Revert "Update pyhomematic 1.19 & small cleanups (#5299)" This reverts commit a30711f1a0e2d4a286799d714fe59ff147883fab. Revert "[sensor] Add Dublin bus RTPI sensor (#5257)" This reverts commit 1219ca3c3bc083c8f919c4db7eb3670686e52861. Revert "Bugfix group reload (#5292)" This reverts commit baa8e53e66167a1fb0f9d090f28325454ad3d4ef. Revert "Support for TrackR device trackers (#5010)" This reverts commit f7a1d63d52dc7687a07cd2c52ef4e8e6894e45d9. Revert "Bump pywemo version." This reverts commit dc937cc8cffbb9ec2b4342d801f8d7332a8dd9cf. Revert "Upgrade to voluptuous to 0.9.3 (#5288)" This reverts commit d12decc4714cb61af58ab08581712b8be5367960. Revert "Upgrade distro to 1.0.2 (#5291)" This reverts commit 64800fd48c02520b1f44be960dc8c539f82d1692. Revert "Don't build Adafruit_BBIO - doesn't work on all platforms. (#5281)" This reverts commit 9a3c0c8cd3a06d118cfcf58d1078912e41f12f31. Revert "Convert flic to synchronous platform. (#5276)" This reverts commit eb9b95c2922181b097258856af9bd2bc4d7a814e. Revert "Upgrade to aiohttp 1.2 (#4964)" This reverts commit e68e29e03ebd43175761d1ae2b4e598d382d2cf4. Revert "Fix TCP sensor to correctly use value_template (#5211)" This reverts commit 1cf9ae5a01d663bb9e3d3e38741b2ae818b36f93. Revert "Cleanup language support on TTS (#5255)" This reverts commit 3f3a3bcc8ac7eec2e5e9eba9981c74db3842f22d. Revert "Add last triggered to script (#5261)" This reverts commit 467cb18625da9323f743ed62a342e446a79fb05b. Revert "Bump flux_led version and make use of PyPi package (#5267)" This reverts commit 34a9fb01ac1fb9568f18677be5faf3d23ab7dc2a. Revert "Add support for NAD receivers (#5191)" This reverts commit 3b59e169f1bc11b3887bc98b2f8425f6c70a0df2. Revert "Bugfix async device_tracker see callback (#5259)" This reverts commit 71fddd26eb9c9ffe6cbd809298f07e17aad152a4. Revert "Use SHA hash to make token harder to guess (#5258)" This reverts commit 922308bc1f7a2a0a769a8c29d663c90a97a0583b. * Revert "Revert "Bugfix camera streams (#5306)"" This reverts commit 2ee8c44021cf9c3a91d20f9ee26752aa8369d2e6. * Update darksky.py
2017-01-27 06:32:45 +00:00
self.forecast_day = forecast_day
self.forecast_hour = forecast_hour
2015-06-17 19:59:38 +00:00
self._state = None
self._icon = None
self._unit_of_measurement = None
2015-06-17 19:59:38 +00:00
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
if self.forecast_day is not None:
return f"{self.client_name} {self._name} {self.forecast_day}d"
if self.forecast_hour is not None:
return f"{self.client_name} {self._name} {self.forecast_hour}h"
return f"{self.client_name} {self._name}"
2015-06-17 19:59:38 +00:00
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
2015-06-17 19:59:38 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
2015-06-17 19:59:38 +00:00
return self._unit_of_measurement
@property
def unit_system(self):
2016-03-08 15:46:34 +00:00
"""Return the unit system of this entity."""
return self.forecast_data.unit_system
@property
def entity_picture(self):
"""Return the entity picture to use in the frontend, if any."""
2019-07-31 19:25:30 +00:00
if self._icon is None or "summary" not in self.type:
return None
if self._icon in CONDITION_PICTURES:
return CONDITION_PICTURES[self._icon][0]
return None
def update_unit_of_measurement(self):
"""Update units based on unit system."""
2019-07-31 19:25:30 +00:00
unit_index = {"si": 1, "us": 2, "ca": 3, "uk": 4, "uk2": 5}.get(
self.unit_system, 1
)
self._unit_of_measurement = SENSOR_TYPES[self.type][unit_index]
@property
def icon(self):
"""Icon to use in the frontend, if any."""
2019-07-31 19:25:30 +00:00
if "summary" in self.type and self._icon in CONDITION_PICTURES:
return CONDITION_PICTURES[self._icon][1]
return SENSOR_TYPES[self.type][6]
@property
def device_state_attributes(self):
"""Return the state attributes."""
2019-07-31 19:25:30 +00:00
return {ATTR_ATTRIBUTION: ATTRIBUTION}
2015-06-17 19:59:38 +00:00
def update(self):
"""Get the latest data from Dark Sky and updates the states."""
# Call the API for new forecast data. Each sensor will re-trigger this
# same exact call, but that's fine. We cache results for a short period
# of time to prevent hitting API limits. Note that Dark Sky will
# charge users for too many calls in 1 day, so take care when updating.
self.forecast_data.update()
self.update_unit_of_measurement()
2019-07-31 19:25:30 +00:00
if self.type == "minutely_summary":
self.forecast_data.update_minutely()
minutely = self.forecast_data.data_minutely
2019-07-31 19:25:30 +00:00
self._state = getattr(minutely, "summary", "")
self._icon = getattr(minutely, "icon", "")
elif self.type == "hourly_summary":
self.forecast_data.update_hourly()
hourly = self.forecast_data.data_hourly
2019-07-31 19:25:30 +00:00
self._state = getattr(hourly, "summary", "")
self._icon = getattr(hourly, "icon", "")
elif self.forecast_hour is not None:
self.forecast_data.update_hourly()
hourly = self.forecast_data.data_hourly
2019-07-31 19:25:30 +00:00
if hasattr(hourly, "data"):
self._state = self.get_state(hourly.data[self.forecast_hour])
else:
self._state = 0
2019-07-31 19:25:30 +00:00
elif self.type == "daily_summary":
self.forecast_data.update_daily()
daily = self.forecast_data.data_daily
2019-07-31 19:25:30 +00:00
self._state = getattr(daily, "summary", "")
self._icon = getattr(daily, "icon", "")
elif self.forecast_day is not None:
self.forecast_data.update_daily()
daily = self.forecast_data.data_daily
2019-07-31 19:25:30 +00:00
if hasattr(daily, "data"):
self._state = self.get_state(daily.data[self.forecast_day])
else:
self._state = 0
else:
self.forecast_data.update_currently()
currently = self.forecast_data.data_currently
self._state = self.get_state(currently)
2015-06-17 19:59:38 +00:00
def get_state(self, data):
"""
Return a new state based on the type.
2015-06-17 19:59:38 +00:00
If the sensor type is unknown, the current state is returned.
"""
lookup_type = convert_to_camel(self.type)
state = getattr(data, lookup_type, None)
if state is None:
return state
2016-06-01 16:17:15 +00:00
2019-07-31 19:25:30 +00:00
if "summary" in self.type:
self._icon = getattr(data, "icon", "")
# Some state data needs to be rounded to whole values or converted to
# percentages
2019-07-31 19:25:30 +00:00
if self.type in ["precip_probability", "cloud_cover", "humidity"]:
return round(state * 100, 1)
2019-07-31 19:25:30 +00:00
if self.type in [
"dew_point",
"temperature",
"apparent_temperature",
"temperature_low",
"apparent_temperature_low",
"temperature_min",
"apparent_temperature_min",
"temperature_high",
"apparent_temperature_high",
"temperature_max",
2019-07-31 19:46:17 +00:00
"apparent_temperature_max",
"precip_accumulation",
2019-07-31 19:25:30 +00:00
"pressure",
"ozone",
"uvIndex",
]:
return round(state, 1)
return state
2016-06-01 16:17:15 +00:00
class DarkSkyAlertSensor(Entity):
"""Implementation of a Dark Sky sensor."""
def __init__(self, forecast_data, sensor_type, name):
"""Initialize the sensor."""
self.client_name = name
self._name = SENSOR_TYPES[sensor_type][0]
self.forecast_data = forecast_data
self.type = sensor_type
self._state = None
self._icon = None
self._alerts = None
@property
def name(self):
"""Return the name of the sensor."""
return f"{self.client_name} {self._name}"
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def icon(self):
"""Icon to use in the frontend, if any."""
if self._state is not None and self._state > 0:
return "mdi:alert-circle"
return "mdi:alert-circle-outline"
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._alerts
def update(self):
"""Get the latest data from Dark Sky and updates the states."""
# Call the API for new forecast data. Each sensor will re-trigger this
# same exact call, but that's fine. We cache results for a short period
# of time to prevent hitting API limits. Note that Dark Sky will
# charge users for too many calls in 1 day, so take care when updating.
self.forecast_data.update()
self.forecast_data.update_alerts()
alerts = self.forecast_data.data_alerts
self._state = self.get_state(alerts)
def get_state(self, data):
"""
Return a new state based on the type.
If the sensor type is unknown, the current state is returned.
"""
alerts = {}
if data is None:
self._alerts = alerts
return data
multiple_alerts = len(data) > 1
for i, alert in enumerate(data):
for attr in ALERTS_ATTRS:
if multiple_alerts:
2019-07-31 19:25:30 +00:00
dkey = attr + "_" + str(i)
else:
dkey = attr
alerts[dkey] = getattr(alert, attr)
self._alerts = alerts
return len(data)
def convert_to_camel(data):
"""
Convert snake case (foo_bar_bat) to camel case (fooBarBat).
2016-06-01 16:17:15 +00:00
2018-10-25 07:33:23 +00:00
This is not pythonic, but needed for certain situations.
"""
2019-07-31 19:25:30 +00:00
components = data.split("_")
return components[0] + "".join(x.title() for x in components[1:])
2015-06-17 19:59:38 +00:00
class DarkSkyData:
"""Get the latest data from Darksky."""
2015-06-17 19:59:38 +00:00
2019-07-31 19:25:30 +00:00
def __init__(self, api_key, latitude, longitude, units, language, interval):
2016-03-08 15:46:34 +00:00
"""Initialize the data object."""
2015-06-17 19:59:38 +00:00
self._api_key = api_key
self.latitude = latitude
self.longitude = longitude
2016-06-01 16:17:15 +00:00
self.units = units
self.language = language
2016-06-01 16:17:15 +00:00
2015-06-17 19:59:38 +00:00
self.data = None
self.unit_system = None
2016-06-01 16:17:15 +00:00
self.data_currently = None
self.data_minutely = None
self.data_hourly = None
self.data_daily = None
self.data_alerts = None
2016-06-01 16:17:15 +00:00
# Apply throttling to methods using configured interval
self.update = Throttle(interval)(self._update)
self.update_currently = Throttle(interval)(self._update_currently)
self.update_minutely = Throttle(interval)(self._update_minutely)
self.update_hourly = Throttle(interval)(self._update_hourly)
self.update_daily = Throttle(interval)(self._update_daily)
self.update_alerts = Throttle(interval)(self._update_alerts)
def _update(self):
"""Get the latest data from Dark Sky."""
try:
2016-09-03 07:35:33 +00:00
self.data = forecastio.load_forecast(
2019-07-31 19:25:30 +00:00
self._api_key,
self.latitude,
self.longitude,
units=self.units,
lang=self.language,
)
except (ConnectError, HTTPError, Timeout, ValueError) as error:
_LOGGER.error("Unable to connect to Dark Sky: %s", error)
self.data = None
2019-07-31 19:25:30 +00:00
self.unit_system = self.data and self.data.json["flags"]["units"]
2016-06-01 16:17:15 +00:00
def _update_currently(self):
2016-06-01 16:17:15 +00:00
"""Update currently data."""
self.data_currently = self.data and self.data.currently()
2016-06-01 16:17:15 +00:00
def _update_minutely(self):
2016-06-01 16:17:15 +00:00
"""Update minutely data."""
self.data_minutely = self.data and self.data.minutely()
2016-06-01 16:17:15 +00:00
def _update_hourly(self):
2016-06-01 16:17:15 +00:00
"""Update hourly data."""
self.data_hourly = self.data and self.data.hourly()
2016-06-01 16:17:15 +00:00
def _update_daily(self):
2016-06-01 16:17:15 +00:00
"""Update daily data."""
self.data_daily = self.data and self.data.daily()
def _update_alerts(self):
"""Update alerts data."""
self.data_alerts = self.data and self.data.alerts()