I’m a programmer by trade, but my knowledge of Python is limited. So it’s entirely possible that I made a very simple error.
I am repeating the same code in my HASS configuration over and over. So I thought I should make a component to simplify things.
I need to be able to supply a List of entity ids that represent motion sensors. I need to be able to pass in an “off delay”.
I’d like the component to automatically create a binary_sensor to indicate when it believes a particular room is occupied (based on the motion sensor and the off delay) as well as an input_select offering “occupied, unoccupied, and auto” which will override the logic in the component.
Though it’s no where near complete, I loaded this in custom_componets to see if I was even on the right track. And added some config to load the platform. When doing so, my HASS logs get this far… and then just stop, though HASS is still running:
Jan 31 08:16:46 raspberrypi hass[28146]: INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: service=publish, domain=mqtt>
Jan 31 08:16:46 raspberrypi hass[28146]: INFO:homeassistant.core:Bus:Handling <Event component_loaded[L]: component=mqtt>
Jan 31 08:16:46 raspberrypi hass[28146]: Config directory: /home/hass/.homeassistant
Here is the code from my component, installed at custom_components/sensor/occupation_sensor.py:
from homeassistant.helpers.entity import Entity
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components import InputSelect
from homeassistant.const import CONF_NAME
def setup_platform(hass, config, add_devices, discovery_info=None):
"""setup platform"""
# config items
# motion sensors array (maybe empty)
# motion sensor delay
# create items
# input_select (on, off, auto (if motion sensors))
name = config.get(CONF_NAME)
add_devices([OccupationSensor(
hass,
name + 'sensor',
name + 'select',
config.get('motion_sensors'),
config.get('delay'),
), OccupationSelect(
name + 'select',
name + 'select',
'auto',
['auto','on','off'],
'',
)])
class OccupationSensor(BinarySensorDevice):
def __init__(self, hass, name, select, motion_sensors, delay):
"""Initialize the MQTT binary sensor."""
self._hass = hass
self._name = name
self._state = False
self._sensor_class = 'occupancy'
class OccupationSelect(InputSelect):