core/homeassistant/components/sensor/wink.py

87 lines
2.5 KiB
Python
Raw Normal View History

2015-08-06 16:53:44 +00:00
"""
Support for Wink sensors.
2015-10-20 19:29:19 +00:00
2015-11-09 12:12:18 +00:00
For more details about this platform, please refer to the documentation at
at https://home-assistant.io/components/sensor.wink/
2015-08-06 16:53:44 +00:00
"""
import logging
from homeassistant.const import (CONF_ACCESS_TOKEN, STATE_CLOSED,
2016-06-29 21:16:53 +00:00
STATE_OPEN, TEMP_CELSIUS)
from homeassistant.helpers.entity import Entity
2016-06-29 21:16:53 +00:00
from homeassistant.components.wink import WinkDevice
from homeassistant.loader import get_component
2016-07-20 14:39:45 +00:00
REQUIREMENTS = ['python-wink==0.7.11', 'pubnub==3.8.2']
SENSOR_TYPES = ['temperature', 'humidity']
2015-12-17 03:31:34 +00:00
2015-03-01 09:35:58 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 15:46:34 +00:00
"""Setup the Wink platform."""
import pywink
2015-03-01 09:35:58 +00:00
if discovery_info is None:
token = config.get(CONF_ACCESS_TOKEN)
2015-03-01 09:35:58 +00:00
if token is None:
logging.getLogger(__name__).error(
2015-10-21 08:47:12 +00:00
"Missing wink access_token. "
"Get one at https://winkbearertoken.appspot.com/")
2015-03-01 09:35:58 +00:00
return
2015-03-01 09:35:58 +00:00
pywink.set_bearer_token(token)
for sensor in pywink.get_sensors():
if sensor.capability() in SENSOR_TYPES:
add_devices([WinkSensorDevice(sensor)])
add_devices(WinkEggMinder(eggtray) for eggtray in pywink.get_eggtrays())
2016-06-29 21:16:53 +00:00
class WinkSensorDevice(WinkDevice, Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a Wink sensor."""
def __init__(self, wink):
2016-06-29 21:16:53 +00:00
"""Initialize the Wink device."""
super().__init__(wink)
wink = get_component('wink')
self.capability = self.wink.capability()
if self.wink.UNIT == "°":
2016-04-20 03:30:44 +00:00
self._unit_of_measurement = TEMP_CELSIUS
else:
self._unit_of_measurement = self.wink.UNIT
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state."""
if self.capability == "humidity":
2016-06-29 21:16:53 +00:00
return round(self.wink.humidity_percentage())
elif self.capability == "temperature":
2016-06-29 21:16:53 +00:00
return round(self.wink.temperature_float(), 1)
else:
return STATE_OPEN if self.is_open else STATE_CLOSED
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def is_open(self):
2016-03-08 15:46:34 +00:00
"""Return true if door is open."""
return self.wink.state()
2016-06-29 21:16:53 +00:00
class WinkEggMinder(WinkDevice, Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a Wink Egg Minder."""
def __init__(self, wink):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2016-06-29 21:16:53 +00:00
WinkDevice.__init__(self, wink)
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state."""
return self.wink.state()