def build_wrapper( cloud_client, device_type, # type: str device_uuid, # type: str device_specs # type: dict ): if device_type.startswith('msl') or device_type.startswith( 'mss560m') or device_type.startswith('mss570m'): return GenericBulb(cloud_client, device_uuid=device_uuid, **device_specs) elif device_type.startswith('mss'): return GenericPlug(cloud_client, device_uuid=device_uuid, **device_specs) elif device_type.startswith('msg'): return GenericGarageDoorOpener(cloud_client, device_uuid=device_uuid, **device_specs) elif device_type.startswith('msh'): return GenericHub(cloud_client, device_uuid=device_uuid, **device_specs) elif device_type.startswith('msxh'): return GenericHumidifier(cloud_client, device_uuid=device_uuid, **device_specs) else: return GenericPlug(cloud_client, device_uuid=device_uuid, **device_specs)
def __init__(self, device: GenericPlug, channel: int): # Device State self._is_on = None self._is_online = None # Reference to the device object self._device = device # Devide properties self._channel_id = channel self._device_id = device.uuid self._device_name = self._device.name # If the current device has more than 1 channel, we need to setup the device name and id accordingly if len(device.get_channels()) > 1: self._id = calculate_switch_id(self._device.uuid, channel) channelData = self._device.get_channels()[channel] self._entity_name = "{} - {}".format( self._device.name, channelData.get('devName', 'Main Switch')) else: self._id = self._device.uuid self._entity_name = self._device.name # Load the current device status self._is_online = self._device.online device.register_event_callback(self.handler)
def __init__(self, device: GenericPlug, channel: int): self._device = device self._channel_id = channel self._id = calculate_switch_id(self._device.uuid, channel) if len(self._device.get_channels())>1: self._device_name = "%s (channel: %d)" % (self._device.name, channel) else: self._device_name = self._device.name device.register_event_callback(self.handler)
def __init__(self, device: GenericPlug, channel: int): self._device = device self._channel_id = channel self._id = calculate_switch_id(self._device.uuid, channel) if channel > 0: # This is a sub-channel within the multi-way adapter channelData = self._device.get_channels()[channel] self._device_name = channelData['devName'] else: # This is the root device self._device_name = self._device.name device.register_event_callback(self.handler)
def __init__(self, device: GenericPlug, channel: int): self._device = device self._channel_id = channel self._device_id = device.uuid self._device_name = self._device.name # If the current device has more than 1 channel, we need to setup the device name and id accordingly if len(device.get_channels())>1: self._id = calculate_switch_id(self._device.uuid, channel) channelData = self._device.get_channels()[channel] self._entity_name = "{} - {}".format(self._device.name, channelData.get('devName', 'Main Switch')) else: self._id = self._device.uuid self._entity_name = self._device.name device.register_event_callback(self.handler)
def __init__(self, device: GenericPlug, channel: int): self._device = device # If the current device has more than 1 channel, we need to setup the device name and id accordingly if len(device.get_channels()) > 1: self._id = calculate_switch_id(device.uuid, channel) channel_data = device.get_channels()[channel] self._entity_name = "{} - {}".format( device.name, channel_data.get('devName', 'Main Switch')) else: self._id = device.uuid self._entity_name = device.name # Device properties self._channel_id = channel self._available = True # Assume the mqtt client is connected self._first_update_done = False
async def log_device_usage(plug_device: GenericPlug) -> None: device_uuid = plug_device.uuid # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here. iothub_conn_str = os.getenv("IOTHUB_DEVICE_CONNSTR_" + device_uuid) if not iothub_conn_str: logging.warn("The plug '%s' (%s) is not configured." % (device_uuid, plug_device.name)) return # The client object is used to interact with your Azure IoT hub. device_client = IoTHubDeviceClient.create_from_connection_string( iothub_conn_str) # Connect the IOT hub client. await device_client.connect() logging.info("Smart plug %s" % plug_device.name) logging.info(" uuid : %s" % plug_device.uuid) electricity = plug_device.get_electricity() converted = {} converted["voltage"] = electricity["voltage"] * 0.1 converted["current"] = electricity["current"] * 0.001 converted["power"] = electricity["power"] * 0.001 logging.debug("sending message") msg = Message(str(converted)) msg.message_id = uuid.uuid4() await device_client.send_message(msg) logging.debug("done sending message") # Disconnect IOT hub await device_client.disconnect()