1 """Support for particulate matter sensors connected to a serial port."""
3 from __future__
import annotations
7 from pmsensor
import serial_pm
as pm
8 import voluptuous
as vol
11 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
20 _LOGGER = logging.getLogger(__name__)
23 CONF_SERIAL_DEVICE =
"serial_device"
25 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
27 vol.Required(CONF_BRAND): cv.string,
28 vol.Required(CONF_SERIAL_DEVICE): cv.string,
29 vol.Optional(CONF_NAME): cv.string,
37 add_entities: AddEntitiesCallback,
38 discovery_info: DiscoveryInfoType |
None =
None,
40 """Set up the available PM sensors."""
42 coll = pm.PMDataCollector(
43 config.get(CONF_SERIAL_DEVICE), pm.SUPPORTED_SENSORS[config.get(CONF_BRAND)]
47 "Brand %s not supported\n supported brands: %s",
48 config.get(CONF_BRAND),
49 pm.SUPPORTED_SENSORS.keys(),
52 except OSError
as err:
54 "Could not open serial connection to %s (%s)",
55 config.get(CONF_SERIAL_DEVICE),
62 for pmname
in coll.supported_values():
63 if config.get(CONF_NAME)
is not None:
64 name = f
"{config.get(CONF_NAME)} PM{pmname}"
73 """Representation of an Particulate matter sensor."""
75 def __init__(self, pm_data_collector, name, pmname):
76 """Initialize a new PM sensor."""
84 """Return the name of the sensor."""
85 return self.
_name_name
89 """Return the state of the sensor."""
94 """Return the unit of measurement of this entity, if any."""
95 return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
98 """Read from sensor and update the state."""
99 _LOGGER.debug(
"Reading data from PM sensor")
103 _LOGGER.error(
"Could not read PM%s value", self.
_pmname_pmname)
def __init__(self, pm_data_collector, name, pmname)
def native_unit_of_measurement(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)