1 """Support for Sensor.Community sensors."""
3 from __future__
import annotations
5 from typing
import cast
10 SensorEntityDescription,
17 CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
28 DataUpdateCoordinator,
31 from .const
import ATTR_SENSOR_ID, CONF_SENSOR_ID, DOMAIN
33 SENSORS: tuple[SensorEntityDescription, ...] = (
36 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
37 device_class=SensorDeviceClass.TEMPERATURE,
38 state_class=SensorStateClass.MEASUREMENT,
42 native_unit_of_measurement=PERCENTAGE,
43 device_class=SensorDeviceClass.HUMIDITY,
44 state_class=SensorStateClass.MEASUREMENT,
48 native_unit_of_measurement=UnitOfPressure.PA,
49 device_class=SensorDeviceClass.PRESSURE,
50 state_class=SensorStateClass.MEASUREMENT,
53 key=
"pressure_at_sealevel",
54 translation_key=
"pressure_at_sealevel",
55 native_unit_of_measurement=UnitOfPressure.PA,
56 device_class=SensorDeviceClass.PRESSURE,
57 state_class=SensorStateClass.MEASUREMENT,
61 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
62 device_class=SensorDeviceClass.PM10,
63 state_class=SensorStateClass.MEASUREMENT,
67 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
68 device_class=SensorDeviceClass.PM25,
69 state_class=SensorStateClass.MEASUREMENT,
75 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
77 """Set up a Sensor.Community sensor based on a config entry."""
78 coordinator = hass.data[DOMAIN][entry.entry_id]
82 coordinator=coordinator,
83 description=description,
84 sensor_id=entry.data[CONF_SENSOR_ID],
85 show_on_map=entry.data[CONF_SHOW_ON_MAP],
87 for description
in SENSORS
88 if description.key
in coordinator.data
93 """Implementation of a Sensor.Community sensor."""
95 _attr_attribution =
"Data provided by Sensor.Community"
96 _attr_has_entity_name =
True
97 _attr_should_poll =
False
102 coordinator: DataUpdateCoordinator,
103 description: SensorEntityDescription,
107 """Initialize the Sensor.Community sensor."""
108 super().
__init__(coordinator=coordinator)
112 ATTR_SENSOR_ID: sensor_id,
116 f
"https://devices.sensor.community/sensors/{sensor_id}/settings"
118 identifiers={(DOMAIN,
str(sensor_id))},
119 name=f
"Sensor {sensor_id}",
120 manufacturer=
"Sensor.Community",
133 """Return the state of the device."""
135 not self.coordinator.data
136 or (value := self.coordinator.data.get(self.
entity_descriptionentity_description.key))
is None
139 return cast(float, value)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)