Hi,
I’m working on new platform for Crestron. I need to set the initial state of a binary_sensor.
I can poll the state or I can send an update request crestron.update_request()
to the controller and process the callbacks, but I’m not really sure where is the right place to do it.
This is the code for the component.
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up binary sensor(s) for Crestron platform."""
if config == {}:
return
async_add_entities_config(hass, config, async_add_entities)
@callback
def async_add_entities_config(hass, config, async_add_entities):
"""Set up binary sensor for Crestron platform configured within platform."""
name = config[CONF_NAME]
join_status = config[CONF_DIGITAL_JOIN_IN_STATUS]
crestron = hass.data[CRESTRON_CONTROLLER]
entity = CrestronBinarySensor(name= name, join_status= join_status, controller= crestron)
async_add_entities([entity])
class CrestronBinarySensor(BinarySensorDevice):
"""Representation of a Crestron binary sensor."""
def __init__(self, name, join_status, controller):
"""Initialize of Crestron binary sensor."""
self._name = name
self._join_status = join_status
self._controller = controller
# poll the current state
self._state = controller.get("d", join_status)
@callback
def async_register_callbacks(self):
"""Register callbacks to update hass after device was changed."""
def after_update_callback(sigtype, join, state):
"""Call after device was updated."""
self._state = state
self.async_write_ha_state()
# async_schedule will go trough async_update
# self.async_schedule_update_ha_state(True)
self._controller.subscribe("d", self._join_status, after_update_callback)
async def async_added_to_hass(self):
"""Store register state change callback."""
self.async_register_callbacks()
In the init.py
def setup(hass, base_config):
"""Set up the Creston component."""
hass.data[CRESTRON_CONTROLLER] = None
config = base_config.get(DOMAIN)
crestron = CIPSocketClient(
config[CONF_HOST], config[CONF_IPID], config[CONF_PORT]
)
hass.data[CRESTRON_CONTROLLER] = crestron
crestron.start()
time.sleep(1.5)
# TODO verify connection
#
_LOGGER.info("Connected to CIP client at %s IPID %s", config[CONF_HOST], config[CONF_IPID])
return True