diff --git a/homeassistant/components/binary_sensor/bloomsky.py b/homeassistant/components/binary_sensor/bloomsky.py new file mode 100644 index 00000000000..3f18af03013 --- /dev/null +++ b/homeassistant/components/binary_sensor/bloomsky.py @@ -0,0 +1,69 @@ +""" +Support the binary sensors of a BloomSky weather station. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.bloomsky/ +""" +import logging + +from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.loader import get_component + +DEPENDENCIES = ["bloomsky"] + +# These are the available sensors mapped to binary_sensor class +SENSOR_TYPES = { + "Rain": "moisture", + "Night": None, +} + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the available BloomSky weather binary sensors.""" + logger = logging.getLogger(__name__) + bloomsky = get_component('bloomsky') + sensors = config.get('monitored_conditions', SENSOR_TYPES) + + for device in bloomsky.BLOOMSKY.devices.values(): + for variable in sensors: + if variable in SENSOR_TYPES: + add_devices([BloomSkySensor(bloomsky.BLOOMSKY, + device, + variable)]) + else: + logger.error("Cannot find definition for device: %s", variable) + + +class BloomSkySensor(BinarySensorDevice): + """ Represents a single binary sensor in a BloomSky device. """ + + def __init__(self, bs, device, sensor_name): + """Initialize a bloomsky binary sensor.""" + self._bloomsky = bs + self._device_id = device["DeviceID"] + self._sensor_name = sensor_name + self._name = "{} {}".format(device["DeviceName"], sensor_name) + self._unique_id = "bloomsky_binary_sensor {}".format(self._name) + self.update() + + @property + def name(self): + """The name of the BloomSky device and this sensor.""" + return self._name + + @property + def unique_id(self): + """Unique ID for this sensor.""" + return self._unique_id + + @property + def is_on(self): + """If binary sensor is on.""" + return self._state + + def update(self): + """Request an update from the BloomSky API.""" + self._bloomsky.refresh_devices() + + self._state = \ + self._bloomsky.devices[self._device_id]["Data"][self._sensor_name] diff --git a/homeassistant/components/sensor/bloomsky.py b/homeassistant/components/sensor/bloomsky.py index 902b42d6e3f..89aeeb9bb3f 100644 --- a/homeassistant/components/sensor/bloomsky.py +++ b/homeassistant/components/sensor/bloomsky.py @@ -1,6 +1,4 @@ """ -homeassistant.components.sensor.bloomsky -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support the sensor of a BloomSky weather station. For more details about this component, please refer to the documentation at @@ -8,6 +6,7 @@ https://home-assistant.io/components/sensor.bloomsky/ """ import logging +from homeassistant.const import TEMP_FAHRENHEIT from homeassistant.helpers.entity import Entity from homeassistant.loader import get_component @@ -16,14 +15,12 @@ DEPENDENCIES = ["bloomsky"] # These are the available sensors SENSOR_TYPES = ["Temperature", "Humidity", - "Rain", "Pressure", "Luminance", - "Night", "UVIndex"] # Sensor units - these do not currently align with the API documentation -SENSOR_UNITS = {"Temperature": "°F", +SENSOR_UNITS = {"Temperature": TEMP_FAHRENHEIT, "Humidity": "%", "Pressure": "inHg", "Luminance": "cd/m²"} @@ -34,14 +31,13 @@ FORMAT_NUMBERS = ["Temperature", "Pressure"] # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): - """ Set up the available BloomSky weather sensors. """ - + """Set up the available BloomSky weather sensors.""" logger = logging.getLogger(__name__) bloomsky = get_component('bloomsky') + sensors = config.get('monitored_conditions', SENSOR_TYPES) - for device_key in bloomsky.BLOOMSKY.devices: - device = bloomsky.BLOOMSKY.devices[device_key] - for variable in config["monitored_conditions"]: + for device in bloomsky.BLOOMSKY.devices.values(): + for variable in sensors: if variable in SENSOR_TYPES: add_devices([BloomSkySensor(bloomsky.BLOOMSKY, device, @@ -51,56 +47,45 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class BloomSkySensor(Entity): - """ Represents a single sensor in a BloomSky device. """ + """Represents a single sensor in a BloomSky device.""" def __init__(self, bs, device, sensor_name): + """Initialize a bloomsky sensor.""" 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 = "" + self._name = "{} {}".format(device["DeviceName"], sensor_name) + self._unique_id = "bloomsky_sensor {}".format(self._name) + self.update() @property def name(self): - """ The name of the BloomSky device and this sensor. """ - return "{} {}".format(self._client_name, self._sensor_name) + """The name of the BloomSky device and this sensor.""" + return self._name + + @property + def unique_id(self): + """Unique ID for this sensor.""" + return self._unique_id @property def state(self): - """ The current state (i.e. value) of this sensor. """ + """The current state (i.e. value) of this sensor.""" return self._state @property def unit_of_measurement(self): - """ This sensor's units. """ + """Return the sensor units.""" return SENSOR_UNITS.get(self._sensor_name, None) def update(self): - """ Request an update from the BloomSky API. """ + """Request an update from the BloomSky API.""" 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): - """ Handle the response from the BloomSky API for this sensor. """ - 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) + state = \ + self._bloomsky.devices[self._device_id]["Data"][self._sensor_name] + + if self._sensor_name in FORMAT_NUMBERS: + self._state = "{0:.2f}".format(state) else: - self._state = data + self._state = state