from homeassistant.const import TEMP_CELSIUS, PRESSURE_HPA, VOLUME_MILLILITERS, ILLUMINANCE
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import device_registry as dr
import psycopg2
def setup_platform(hass, config, add_entities, discovery_info=None):
add_entities([TempSensor(), HumiditySensor(), PressureSensor(), RainSensor(), LightSensor()])
class TempSensor(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
@property
def device_info(self):
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
("sensor", self.unique_id)
},
"name": self.name,
"manufacturer": "",
"model": "",
"sw_version": ""
}
@property
def unique_id(self):
"""Return the entity id of the sensor."""
return 'sensor.outside_temperature'
@property
def name(self):
"""Return the name of the sensor."""
return 'Temperature'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()
# Current Conditions data
cur.execute(
"select temperature_external as DS18b20 from weather.temperature WHERE read_time > CURRENT_DATE order by read_time DESC")
row = cur.fetchone()
tempFloat = row[0]
self._state = "{0:.2f}".format(tempFloat)
class HumiditySensor(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return 'Humidity'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return '%'
@property
def icon(self):
return 'mdi:water-percent'
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()
# Current Conditions data
cur.execute(
"select humidity from weather.humidity WHERE read_time > CURRENT_DATE order by read_time DESC")
row = cur.fetchone()
self._state = "{0:.1f}".format(row[0])
# self._state = 0
class PressureSensor(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return 'Air Pressure'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return PRESSURE_HPA
@property
def icon(self):
return 'mdi:gauge'
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()
# Current Conditions data
cur.execute(
"select pressure from weather.pressure WHERE read_time > CURRENT_DATE order by read_time DESC")
row = cur.fetchone()
self._state = "{0:.1f}".format(row[0])
# self._state = 0
class RainSensor(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return 'Rainfall Today'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return VOLUME_MILLILITERS
@property
def icon(self):
return 'mdi:water'
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()
# Current Conditions data
cur.execute(
"select rain as rainfall from weather.rain WHERE read_time > CURRENT_DATE")
rainVolume = 0
for row in cur.fetchall():
rainVolume = rainVolume + row[0]
rainf = float(int(rainVolume) * 0.2794)
self._state = "%.2f" % round(rainf,2)
# self._state = 0
class LightSensor(Entity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return 'Light Level'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ''
@property
def icon(self):
return 'mdi:lightbulb'
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
conn = psycopg2.connect("****Connection Settings*****")
cur = conn.cursor()
# Current Conditions data
cur.execute(
"select light from weather.light order by read_time DESC")
row = cur.fetchone()
self._state = row[0]