Added a config flag[Boolean] to declare if SenseHAT is attached (#5883)

* Added a config flag[Boolean] to declare if SenseHAT is attached to RaspberryPi

* Hound found a line too long violation - fix
pull/6074/head
George.M 2017-02-15 10:11:55 +00:00 committed by Pascal Vizeli
parent cf99551110
commit b1fa178df4
1 changed files with 15 additions and 7 deletions

View File

@ -21,6 +21,7 @@ REQUIREMENTS = ['sense-hat==2.2.0']
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'sensehat'
CONF_IS_HAT_ATTACHED = 'is_hat_attached'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
@ -34,6 +35,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DISPLAY_OPTIONS, default=SENSOR_TYPES):
[vol.In(SENSOR_TYPES)],
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_IS_HAT_ATTACHED, default=True): cv.boolean
})
@ -51,15 +53,15 @@ def get_average(temp_base):
get_average.temp[2] = get_average.temp[1]
get_average.temp[1] = get_average.temp[0]
get_average.temp[0] = temp_base
temp_avg = (get_average.temp[0]+get_average.temp[1]+get_average.temp[2])/3
temp_avg = (get_average.temp[0] + get_average.temp[1]
+ get_average.temp[2]) / 3
return temp_avg
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Sense HAT sensor platform."""
data = SenseHatData()
data = SenseHatData(config.get(CONF_IS_HAT_ATTACHED))
dev = []
for variable in config[CONF_DISPLAY_OPTIONS]:
dev.append(SenseHatSensor(data, variable))
@ -111,11 +113,12 @@ class SenseHatSensor(Entity):
class SenseHatData(object):
"""Get the latest data and update."""
def __init__(self):
def __init__(self, is_hat_attached):
"""Initialize the data object."""
self.temperature = None
self.humidity = None
self.pressure = None
self.is_hat_attached = is_hat_attached
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
@ -124,10 +127,15 @@ class SenseHatData(object):
sense = SenseHat()
temp_from_h = sense.get_temperature_from_humidity()
temp_from_p = sense.get_temperature_from_pressure()
t_cpu = get_cpu_temp()
t_total = (temp_from_h + temp_from_p) / 2
t_correct = t_total - ((t_cpu - t_total) / 1.5)
t_correct = get_average(t_correct)
if self.is_hat_attached:
t_cpu = get_cpu_temp()
t_correct = t_total - ((t_cpu - t_total) / 1.5)
t_correct = get_average(t_correct)
else:
t_correct = get_average(t_total)
self.temperature = t_correct
self.humidity = sense.get_humidity()
self.pressure = sense.get_pressure()