core/homeassistant/components/sensor/bloomsky.py

94 lines
2.9 KiB
Python
Raw Normal View History

2016-02-04 20:01:45 +00:00
"""
2016-02-06 07:23:30 +00: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 07:23:30 +00:00
https://home-assistant.io/components/sensor.bloomsky/
2016-02-04 20:01:45 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
2016-02-20 07:21:56 +00:00
from homeassistant.const import TEMP_FAHRENHEIT
2016-02-04 20:01:45 +00:00
from homeassistant.helpers.entity import Entity
2016-02-19 05:27:50 +00:00
from homeassistant.loader import get_component
2016-02-04 20:01:45 +00:00
DEPENDENCIES = ["bloomsky"]
2016-02-06 07:23:30 +00:00
# These are the available sensors
2016-02-04 20:01:45 +00:00
SENSOR_TYPES = ["Temperature",
"Humidity",
"Pressure",
"Luminance",
"UVIndex",
"Voltage"]
2016-02-04 20:01:45 +00:00
2016-02-06 07:23:30 +00:00
# Sensor units - these do not currently align with the API documentation
2016-02-20 07:21:56 +00:00
SENSOR_UNITS = {"Temperature": TEMP_FAHRENHEIT,
2016-02-04 20:01:45 +00:00
"Humidity": "%",
"Pressure": "inHg",
"Luminance": "cd/m²",
"Voltage": "mV"}
2016-02-04 20:01:45 +00:00
2016-02-06 07:23:30 +00:00
# Which sensors to format numerically
FORMAT_NUMBERS = ["Temperature", "Pressure", "Voltage"]
2016-02-04 20:01:45 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 15:46:34 +00:00
"""Setup the available BloomSky weather sensors."""
2016-02-04 20:01:45 +00:00
logger = logging.getLogger(__name__)
2016-02-18 21:10:25 +00:00
bloomsky = get_component('bloomsky')
2016-02-20 07:21:56 +00:00
sensors = config.get('monitored_conditions', SENSOR_TYPES)
2016-02-04 20:01:45 +00:00
2016-02-20 07:21:56 +00:00
for device in bloomsky.BLOOMSKY.devices.values():
for variable in sensors:
2016-02-04 20:01:45 +00:00
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-03-08 15:46:34 +00:00
"""Representation of a single sensor in a BloomSky device."""
2016-02-04 20:01:45 +00:00
def __init__(self, bs, device, sensor_name):
2016-02-20 07:21:56 +00:00
"""Initialize a bloomsky sensor."""
2016-02-04 20:01:45 +00:00
self._bloomsky = bs
self._device_id = device["DeviceID"]
self._sensor_name = sensor_name
2016-02-20 07:21:56 +00:00
self._name = "{} {}".format(device["DeviceName"], sensor_name)
self._unique_id = "bloomsky_sensor {}".format(self._name)
self.update()
2016-02-04 20:01:45 +00:00
@property
def name(self):
2016-02-20 07:21:56 +00:00
"""The name of the BloomSky device and this sensor."""
return self._name
@property
def unique_id(self):
2016-03-08 15:46:34 +00:00
"""Return the unique ID for this sensor."""
2016-02-20 07:21:56 +00:00
return self._unique_id
2016-02-04 20:01:45 +00:00
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""The current state, eg. value, of this sensor."""
2016-02-04 20:01:45 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-02-20 07:21:56 +00:00
"""Return the sensor units."""
2016-02-04 20:01:45 +00:00
return SENSOR_UNITS.get(self._sensor_name, None)
def update(self):
2016-02-20 07:21:56 +00:00
"""Request an update from the BloomSky API."""
2016-02-04 20:01:45 +00:00
self._bloomsky.refresh_devices()
2016-02-20 07:21:56 +00:00
state = \
self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]
if self._sensor_name in FORMAT_NUMBERS:
self._state = "{0:.2f}".format(state)
2016-02-04 20:01:45 +00:00
else:
2016-02-20 07:21:56 +00:00
self._state = state