hass-core/homeassistant/components/sensor/bloomsky.py

106 lines
3.6 KiB
Python
Raw Normal View History

2016-02-04 20:01:45 +00:00
"""
homeassistant.components.sensor.bloomsky
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-02-06 08:23:30 +01:00
Support the sensor of a BloomSky weather station.
2016-02-04 20:01:45 +00:00
For more details about this component, please refer to the documentation at
2016-02-06 08:23:30 +01:00
https://home-assistant.io/components/sensor.bloomsky/
2016-02-04 20:01:45 +00:00
"""
import logging
2016-02-18 13:10:25 -08:00
from homeassistant.loader import get_component
2016-02-04 20:01:45 +00:00
from homeassistant.helpers.entity import Entity
DEPENDENCIES = ["bloomsky"]
2016-02-06 08:23:30 +01:00
# These are the available sensors
2016-02-04 20:01:45 +00:00
SENSOR_TYPES = ["Temperature",
"Humidity",
"Rain",
"Pressure",
"Luminance",
"Night",
"UVIndex"]
2016-02-06 08:23:30 +01:00
# Sensor units - these do not currently align with the API documentation
2016-02-04 20:01:45 +00:00
SENSOR_UNITS = {"Temperature": "°F",
"Humidity": "%",
"Pressure": "inHg",
"Luminance": "cd/m²"}
2016-02-06 08:23:30 +01:00
# Which sensors to format numerically
2016-02-04 20:01:45 +00:00
FORMAT_NUMBERS = ["Temperature", "Pressure"]
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-02-06 08:23:30 +01:00
""" Set up the available BloomSky weather sensors. """
2016-02-04 20:01:45 +00:00
logger = logging.getLogger(__name__)
2016-02-18 13:10:25 -08:00
bloomsky = get_component('bloomsky')
2016-02-04 20:01:45 +00:00
for device_key in bloomsky.BLOOMSKY.devices:
device = bloomsky.BLOOMSKY.devices[device_key]
for variable in config["monitored_conditions"]:
if variable in SENSOR_TYPES:
add_devices([BloomSkySensor(bloomsky.BLOOMSKY,
device,
variable)])
else:
logger.error("Cannot find definition for device: %s", variable)
class BloomSkySensor(Entity):
2016-02-06 08:23:30 +01:00
""" Represents a single sensor in a BloomSky device. """
2016-02-04 20:01:45 +00:00
def __init__(self, bs, device, sensor_name):
self._bloomsky = bs
self._device_id = device["DeviceID"]
self._client_name = device["DeviceName"]
self._sensor_name = sensor_name
self._state = self.process_state(device)
self._sensor_update = ""
@property
def name(self):
2016-02-06 08:23:30 +01:00
""" The name of the BloomSky device and this sensor. """
2016-02-04 20:01:45 +00:00
return "{} {}".format(self._client_name, self._sensor_name)
@property
def state(self):
2016-02-06 08:23:30 +01:00
""" The current state (i.e. value) of this sensor. """
2016-02-04 20:01:45 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-02-06 08:23:30 +01:00
""" This sensor's units. """
2016-02-04 20:01:45 +00:00
return SENSOR_UNITS.get(self._sensor_name, None)
def update(self):
2016-02-06 08:23:30 +01:00
""" Request an update from the BloomSky API. """
2016-02-04 20:01:45 +00:00
self._bloomsky.refresh_devices()
# TS is a Unix epoch timestamp for the last time the BloomSky servers
# heard from this device. If that value hasn't changed, the value has
# not been updated.
last_ts = self._bloomsky.devices[self._device_id]["Data"]["TS"]
if last_ts != self._sensor_update:
self.process_state(self._bloomsky.devices[self._device_id])
self._sensor_update = last_ts
def process_state(self, device):
2016-02-06 08:23:30 +01:00
""" Handle the response from the BloomSky API for this sensor. """
2016-02-04 20:01:45 +00:00
data = device["Data"][self._sensor_name]
if self._sensor_name == "Rain":
if data:
self._state = "Raining"
else:
self._state = "Not raining"
elif self._sensor_name == "Night":
if data:
self._state = "Nighttime"
else:
self._state = "Daytime"
elif self._sensor_name in FORMAT_NUMBERS:
self._state = "{0:.2f}".format(data)
else:
self._state = data