def __init__(self, interval=0, temp_in_f=False, mac_addresses=None, sensor_missing_value=9999): """ ``` interval How long in seconds to sleep between returning records. temp_in_f Return temperature in Farenheit mac_addresses If specified, should be a list of sensor MAC addresses. The reader will return the values read from those addresses in the order specified. If one or more of the specified sensors is not found, the value provided as 'sensor_missing_value' (default 9999) will be used. sensor_missing_value The value to be returned for a sensor that is specified in mac_addresses but can not be found. ``` """ self.interval = interval # seconds between records self.temp_in_f = temp_in_f self.mac_addresses = mac_addresses self.sensor_missing_value = sensor_missing_value self.last_record = 0 # timestamp at our last record self.sensors = Pi1Wire().find_all_sensors() # if mac_addresses is specified, map from mac_address to self.sensor[i] if mac_addresses: self.mac_map = { s.mac_address: self.sensors[self.mac_addresses.index(s.mac_address)] for s in self.sensors }
from pi1wire import Pi1Wire for s in Pi1Wire().find_all_sensors(): print('%s = %.2f' % (s.mac_address, s.get_temperature()))
async def check_mount_dir(self, mount_dir: str) -> None: """Test that the mount_dir is a valid path.""" if not await self.hass.async_add_executor_job(os.path.isdir, mount_dir): raise InvalidPath self.pi1proxy = Pi1Wire(mount_dir)
def get_entities(config): """Get a list of entities.""" base_dir = config[CONF_MOUNT_DIR] owhost = config.get(CONF_HOST) owport = config[CONF_PORT] # Ensure type is configured if owhost: conf_type = CONF_TYPE_OWSERVER elif base_dir == DEFAULT_SYSBUS_MOUNT_DIR: conf_type = CONF_TYPE_SYSBUS else: # pragma: no cover # This part of the implementation does not conform to policy regarding 3rd-party libraries, and will not longer be updated. # https://developers.home-assistant.io/docs/creating_platform_code_review/#5-communication-with-devicesservices conf_type = CONF_TYPE_OWFS entities = [] device_names = {} if CONF_NAMES in config: if isinstance(config[CONF_NAMES], dict): device_names = config[CONF_NAMES] # We have an owserver on a remote(or local) host/port if conf_type == CONF_TYPE_OWSERVER: _LOGGER.debug("Initializing using %s:%s", owhost, owport) try: owproxy = protocol.proxy(host=owhost, port=owport) devices = owproxy.dir() except protocol.Error as exc: _LOGGER.error("Cannot connect to owserver on %s:%d, got: %s", owhost, owport, exc) devices = [] for device in devices: _LOGGER.debug("Found device: %s", device) family = owproxy.read(f"{device}family").decode() dev_type = "std" if "EF" in family: dev_type = "HobbyBoard" family = owproxy.read(f"{device}type").decode() if family not in hb_info_from_type(dev_type): _LOGGER.warning( "Ignoring unknown family (%s) of sensor found for device: %s", family, device, ) continue for sensor_key, sensor_value in hb_info_from_type( dev_type)[family].items(): if "moisture" in sensor_key: s_id = sensor_key.split("_")[1] is_leaf = int( owproxy.read( f"{device}moisture/is_leaf.{s_id}").decode()) if is_leaf: sensor_key = f"wetness_{s_id}" sensor_id = os.path.split(os.path.split(device)[0])[1] device_file = os.path.join( os.path.split(device)[0], sensor_value) entities.append( OneWireProxy( device_names.get(sensor_id, sensor_id), device_file, sensor_key, owproxy, )) # We have a raw GPIO ow sensor on a Pi elif conf_type == CONF_TYPE_SYSBUS: _LOGGER.debug("Initializing using SysBus") for p1sensor in Pi1Wire().find_all_sensors(): family = p1sensor.mac_address[:2] sensor_id = f"{family}-{p1sensor.mac_address[2:]}" if family not in DEVICE_SUPPORT_SYSBUS: _LOGGER.warning( "Ignoring unknown family (%s) of sensor found for device: %s", family, sensor_id, ) continue device_file = f"/sys/bus/w1/devices/{sensor_id}/w1_slave" entities.append( OneWireDirect( device_names.get(sensor_id, sensor_id), device_file, "temperature", p1sensor, )) if not entities: _LOGGER.error( "No onewire sensor found. Check if dtoverlay=w1-gpio " "is in your /boot/config.txt. " "Check the mount_dir parameter if it's defined") # We have an owfs mounted else: # pragma: no cover # This part of the implementation does not conform to policy regarding 3rd-party libraries, and will not longer be updated. # https://developers.home-assistant.io/docs/creating_platform_code_review/#5-communication-with-devicesservices _LOGGER.debug("Initializing using OWFS %s", base_dir) _LOGGER.warning( "The OWFS implementation of 1-Wire sensors is deprecated, " "and should be migrated to OWServer (on localhost:4304). " "If migration to OWServer is not feasible on your installation, " "please raise an issue at https://github.com/home-assistant/core/issues/new" "?title=Unable%20to%20migrate%20onewire%20from%20OWFS%20to%20OWServer", ) for family_file_path in glob(os.path.join(base_dir, "*", "family")): with open(family_file_path) as family_file: family = family_file.read() if "EF" in family: continue if family in DEVICE_SENSORS: for sensor_key, sensor_value in DEVICE_SENSORS[family].items(): sensor_id = os.path.split( os.path.split(family_file_path)[0])[1] device_file = os.path.join( os.path.split(family_file_path)[0], sensor_value) entities.append( OneWireOWFS( device_names.get(sensor_id, sensor_id), device_file, sensor_key, )) return entities
def get_entities(config): """Get a list of entities.""" base_dir = config[CONF_MOUNT_DIR] owhost = config.get(CONF_HOST) owport = config[CONF_PORT] # Ensure type is configured if owhost: conf_type = CONF_TYPE_OWSERVER elif base_dir == DEFAULT_SYSBUS_MOUNT_DIR: conf_type = CONF_TYPE_SYSBUS else: conf_type = CONF_TYPE_OWFS entities = [] device_names = {} if CONF_NAMES in config: if isinstance(config[CONF_NAMES], dict): device_names = config[CONF_NAMES] # We have an owserver on a remote(or local) host/port if conf_type == CONF_TYPE_OWSERVER: _LOGGER.debug("Initializing using %s:%s", owhost, owport) try: owproxy = protocol.proxy(host=owhost, port=owport) devices = owproxy.dir() except protocol.Error as exc: _LOGGER.error("Cannot connect to owserver on %s:%d, got: %s", owhost, owport, exc) devices = [] for device in devices: _LOGGER.debug("Found device: %s", device) family = owproxy.read(f"{device}family").decode() dev_type = "std" if "EF" in family: dev_type = "HobbyBoard" family = owproxy.read(f"{device}type").decode() if family not in hb_info_from_type(dev_type): _LOGGER.warning( "Ignoring unknown family (%s) of sensor found for device: %s", family, device, ) continue for sensor_key, sensor_value in hb_info_from_type( dev_type)[family].items(): if "moisture" in sensor_key: s_id = sensor_key.split("_")[1] is_leaf = int( owproxy.read( f"{device}moisture/is_leaf.{s_id}").decode()) if is_leaf: sensor_key = f"wetness_{s_id}" sensor_id = os.path.split(os.path.split(device)[0])[1] device_file = os.path.join( os.path.split(device)[0], sensor_value) entities.append( OneWireProxy( device_names.get(sensor_id, sensor_id), device_file, sensor_key, owproxy, )) # We have a raw GPIO ow sensor on a Pi elif conf_type == CONF_TYPE_SYSBUS: _LOGGER.debug("Initializing using SysBus") for p1sensor in Pi1Wire().find_all_sensors(): family = p1sensor.mac_address[:2] sensor_id = f"{family}-{p1sensor.mac_address[2:]}" if family not in DEVICE_SUPPORT_SYSBUS: _LOGGER.warning( "Ignoring unknown family (%s) of sensor found for device: %s", family, sensor_id, ) continue device_file = f"/sys/bus/w1/devices/{sensor_id}/w1_slave" entities.append( OneWireDirect( device_names.get(sensor_id, sensor_id), device_file, "temperature", p1sensor, )) if not entities: _LOGGER.error( "No onewire sensor found. Check if dtoverlay=w1-gpio " "is in your /boot/config.txt. " "Check the mount_dir parameter if it's defined") # We have an owfs mounted else: _LOGGER.debug("Initializing using OWFS %s", base_dir) for family_file_path in glob(os.path.join(base_dir, "*", "family")): with open(family_file_path) as family_file: family = family_file.read() if "EF" in family: continue if family in DEVICE_SENSORS: for sensor_key, sensor_value in DEVICE_SENSORS[family].items(): sensor_id = os.path.split( os.path.split(family_file_path)[0])[1] device_file = os.path.join( os.path.split(family_file_path)[0], sensor_value) entities.append( OneWireOWFS( device_names.get(sensor_id, sensor_id), device_file, sensor_key, )) return entities