async def get_raw_sensor_strings(self) -> List[str]: # type: ignore """Reads the raw strings from the kernel module sysfs interface :returns: raw strings containing all bytes from the sensor memory :rtype: str :raises NoSensorFoundError: if the sensor could not be found :raises SensorNotReadyError: if the sensor is not ready yet """ try: import aiofiles async with aiofiles.open(str(self.sensorpath), mode="r") as f: data = await f.readlines() except IOError: # pragma: no cover raise NoSensorFoundError( "Could not find sensor of type {} with id {}".format(self.name, self.id) ) if ( data[0].strip()[-3:] != "YES" or "00 00 00 00 00 00 00 00 00" in data[0] ): # pragma: no cover raise SensorNotReadyError(self) return data
def _init_with_first_sensor_by_type(self, sensor_type: Sensor) -> None: s = self.get_available_sensors([sensor_type]) if not s: raise NoSensorFoundError( "Could not find any sensor of type {}".format( sensor_type.name)) self._init_with_type_and_id(sensor_type, s[0].id)
def _init_with_first_sensor_by_id(self, sensor_id: str) -> None: sensor = next( # pragma: no cover (s for s in self.get_available_sensors() if s.id == sensor_id), None) if not sensor: raise NoSensorFoundError( "Could not find sensor with id {}".format(sensor_id)) self._init_with_type_and_id(sensor.type, sensor.id)
def _init_with_first_sensor(self): for _ in range(self.RETRY_ATTEMPTS): s = self.get_available_sensors() if s: self._init_with_type_and_id(s[0].type, s[0].id) break time.sleep(self.RETRY_DELAY_SECONDS) else: raise NoSensorFoundError("Could not find any sensor")
def __init__( self, sensor_type: Optional[Sensor] = None, sensor_id: Optional[str] = None, offset: float = 0.0, offset_unit: Unit = Unit.DEGREES_C, ) -> None: """Initializes a W1ThermSensor. If the W1ThermSensor base directory is not found it will automatically load the needed kernel modules to make this directory available. If the expected directory will not be created after some time an exception is raised. If no type and no id are given the first found sensor will be taken for this instance. :param int sensor_type: the type of the sensor. :param string id: the id of the sensor. :param float offset: a calibration offset for the temperature sensor readings in the unit of ``offset_unit``. :param offset_unit: the unit in which the offset is provided. :raises KernelModuleLoadError: if the w1 therm kernel modules could not be loaded correctly :raises NoSensorFoundError: if the sensor with the given type and/or id does not exist or is not connected """ if not sensor_type and not sensor_id: self._init_with_first_sensor() elif not sensor_id and sensor_type: self._init_with_first_sensor_by_type(sensor_type) elif not sensor_type and sensor_id: self._init_with_first_sensor_by_id(sensor_id) else: self._init_with_type_and_id(sensor_type, sensor_id) # type: ignore # store path to sensor self.sensorpath = (self.BASE_DIRECTORY / (self.slave_prefix + self.id) / self.SLAVE_FILE) if not self.exists(): raise NoSensorFoundError( "Could not find sensor of type {} with id {}".format( self.name, self.id)) self.set_offset(offset, offset_unit)
def get_raw_sensor_strings(self) -> List[str]: """Reads the raw strings from the kernel module sysfs interface :returns: raw strings containing all bytes from the sensor memory :rtype: str :raises NoSensorFoundError: if the sensor could not be found :raises SensorNotReadyError: if the sensor is not ready yet """ try: with self.sensorpath.open("r") as f: data = f.readlines() except IOError: raise NoSensorFoundError( "Could not find sensor of type {} with id {}".format( self.name, self.id)) if (len(data) < 1 or data[0].strip()[-3:] != "YES" or "00 00 00 00 00 00 00 00 00" in data[0]): raise SensorNotReadyError(self) return data