def setup_platform(hass, config, add_devices, discovery_info=None): """ Set up the Thingy 52 temperature sensor""" global e_battery_handle _LOGGER.debug('Version %s', VERSION) _LOGGER.info('if you have ANY issues with this, please report them here:' ' https://github.com/wlgrd/thingy52_homeassistant') mac_address = config.get(CONF_MAC) conf_sensors = config.get(CONF_SENSORS) friendly_name = config.get(ATTR_FRIENDLY_NAME) refresh_interval = config.get(CONF_REFRESH_INT) gas_interval = config.get(CONF_GAS_INT) refresh_interval = refresh_interval.total_seconds() notification_interval = int(refresh_interval) * 1000 sensors = [] _LOGGER.debug("#[THINGYSENSOR]: Connecting to Thingy %s with address %s", friendly_name, mac_address[-6:]) try: thingy = thingy52.Thingy52(mac_address) except Exception as e: _LOGGER.error("#[THINGYSENSOR]: Unable to connect to Thingy (%s): %s", friendly_name, str(e)) _LOGGER.debug( "#[THINGYSENSOR]: Configuring and enabling environment notifications") thingy.environment.enable() # Enable notifications for enabled services # Update interval 1000ms = 1s if "temperature" in conf_sensors: thingy.environment.set_temperature_notification(True) thingy.environment.configure(temp_int=notification_interval) if "humidity" in conf_sensors: thingy.environment.set_humidity_notification(True) thingy.environment.configure(humid_int=notification_interval) if (("co2" in conf_sensors) or ("tvoc" in conf_sensors)): thingy.environment.set_gas_notification(True) thingy.environment.configure(gas_mode_int=gas_interval) if "pressure" in conf_sensors: thingy.environment.set_pressure_notification(True) thingy.environment.configure(press_int=notification_interval) if "battery" in conf_sensors: thingy.battery.enable() # Battery notification not included in bluepy.thingy52 e_battery_handle = thingy.battery.data.getHandle() # Is this needed? battery_ccd = thingy.battery.data.getDescriptors(forUUID=CCCD_UUID)[0] battery_ccd.write(b"\x01\x00", True) for sensorname in conf_sensors: _LOGGER.debug("Adding sensor: %s", sensorname) sensors.append( Thingy52Sensor(thingy, sensorname, SENSOR_TYPES[sensorname][0], friendly_name, SENSOR_TYPES[sensorname][1], SENSOR_TYPES[sensorname][2], mac_address)) add_devices(sensors) thingy.setDelegate(NotificationDelegate(sensors))
def main(protocol): try: print("\nPython %s\n" % sys.version) print("IoT Hub Client for Python") hub_manager = HubManager(protocol) print("Starting the IoT Hub Python sample using protocol %s..." % hub_manager.client_protocol) print( "The sample is now waiting for messages and will indefinitely. Press Ctrl-C to exit. " ) thingy = thingy52.Thingy52(MAC_ADDRESS) thingy.sound.enable() thingy.sound.configure( speaker_mode=0x03) # 0x03 means sample mode, ref FW doc thingy.sound.play_speaker_sample(1) thingy.disconnect() while True: time.sleep(1) except IoTHubError as iothub_error: print("Unexpected error %s from IoTHub" % iothub_error) return except KeyboardInterrupt: print("IoTHubModuleClient sample stopped")
def setup_platform(hass, config, add_devices, discovery_info=None): """ Set up the Thingy 52 temperature sensor""" global e_battery_handle friendly_name = "" mac_address = config.get(CONF_MAC) conf_sensors = config.get(CONF_SENSORS) friendly_name = config.get(ATTR_FRIENDLY_NAME) if(friendly_name is None): friendly_name = "Thingy:" scan_interval = config.get(CONF_SCAN_INTERVAL) gas_interval = config.get(CONF_GAS_INT) scan_interval = scan_interval.total_seconds() notification_interval = int(scan_interval) * 1000 sensors = [] print("#[THINGYSENSOR]: Connecting to Thingy {} with address {}...".format(friendly_name, mac_address)) thingy = thingy52.Thingy52(mac_address) print("#[THINGYSENSOR]: Configuring and enabling environment notifications...") thingy.environment.enable() # Enable notifications for enabled services # Update interval 1000ms = 1s if "temperature" in conf_sensors: thingy.environment.set_temperature_notification(True) thingy.environment.configure(temp_int=notification_interval) if "humidity" in conf_sensors: thingy.environment.set_humidity_notification(True) thingy.environment.configure(humid_int=notification_interval) if ( ("co2" in conf_sensors) or ("tvoc" in conf_sensors) ): thingy.environment.set_gas_notification(True) thingy.environment.configure(gas_mode_int=gas_interval) if "pressure" in conf_sensors: thingy.environment.set_pressure_notification(True) thingy.environment.configure(press_int=notification_interval) if "battery" in conf_sensors: thingy.battery.enable() # Battery notification not included in bluepy.thingy52 e_battery_handle = thingy.battery.data.getHandle() # Is this needed? battery_ccd = thingy.battery.data.getDescriptors(forUUID=CCCD_UUID)[0] battery_ccd.write(b"\x01\x00", True) for sensorname in conf_sensors: print("Adding sensor: {}".format(sensorname)) sensors.append(Thingy52Sensor(thingy, sensorname, friendly_name, SENSOR_UNITS[sensorname])) add_devices(sensors) thingy.setDelegate(NotificationDelegate(sensors))
def connect(self): self.connecting = True try: self.thingy = thingy52.Thingy52(self.mac) self.available = True _LOGGER.debug("#[THINGYSENSOR]: Connected") # if not self.environment_set: self.environmental() except Exception as e: _LOGGER.debug("#[THINGYSENSOR]: Unable to connect to Thingy: %s", str(e)) self.available = False self.connecting = False return self.thingy
def connect(notificationDelegate): global args global thingy connected = False while not connected: try: logging.info('Try to connect to ' + args.mac_address) thingy = thingy52.Thingy52(args.mac_address) connected = True logging.info('Connected...') thingy.setDelegate(notificationDelegate) mqttSend('connected', 1, '') except btle.BTLEException as ex: connected = False logging.debug('Could not connect, sleeping a while before retry') time.sleep(args.sleep) # FIXME: use different sleep value??
i -= 2**8 return i class NewDelegate(btle.DefaultDelegate): def handleNotification(self, hnd, data): if (hnd == thingy52.e_temperature_handle): teptep = binascii.b2a_hex(data) thingy52Data.temperature = str_to_int(teptep[:-2]) print('Notification: Temp received: {}.{} degCelcius'.format( thingy52Data.temperature, int(teptep[-2:], 16))) if (hnd == thingy52.ui_button_handle): print("# Notification: Thingy Button press received: {}".format(repr(data))) print("# Connecting to Thingy with address {}...".format(args.thingyMacAddress)) thingy = thingy52.Thingy52(args.thingyMacAddress) print("# Setting notification handler to new handler...") thingy.setDelegate(NewDelegate()) print("# Configuring and enabling temperature notification...") thingy.environment.enable() thingy.environment.configure(temp_int=5000) # 5000 means 5 seconds thingy.environment.set_temperature_notification(True) print("# Configuring and enabling button press notification...") thingy.ui.enable() thingy.ui.set_btn_notification(True) print("# Configure and enable the sound service...") thingy.sound.enable()