diff --git a/sensor.py b/sensor.py new file mode 100644 index 0000000..05ecf41 --- /dev/null +++ b/sensor.py @@ -0,0 +1,67 @@ +"""Platform for DockerPI sensor integration.""" +from homeassistant.const import TEMP_CELSIUS +from homeassistant.helpers.entity import Entity + + +import smbus + +DEVICE_BUS = 1 +DEVICE_ADDR = 0x17 + +TEMP_REG = 0x01 +LIGHT_REG_L = 0x02 +LIGHT_REG_H = 0x03 +STATUS_REG = 0x04 +ON_BOARD_TEMP_REG = 0x05 +ON_BOARD_HUMIDITY_REG = 0x06 +ON_BOARD_SENSOR_ERROR = 0x07 +BMP280_TEMP_REG = 0x08 +BMP280_PRESSURE_REG_L = 0x09 +BMP280_PRESSURE_REG_M = 0x0A +BMP280_PRESSURE_REG_H = 0x0B +BMP280_STATUS = 0x0C +HUMAN_DETECT = 0x0D + +bus = smbus.SMBus(DEVICE_BUS) + +aReceiveBuf = [] + +aReceiveBuf.append(0x00) + +for i in range(TEMP_REG,HUMAN_DETECT + 1): + aReceiveBuf.append(bus.read_byte_data(DEVICE_ADDR, i)) + + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the sensor platform.""" + add_entities([DockerPI_Sensor()]) + + +class DockerPI_Sensor(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 '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. + """ + self._state = aReceiveBuf[ON_BOARD_TEMP_REG])