Skip to content

Sensor

Several sensor types are created at startup and dynamically as new data appears.

Diagnostic sensors (static)

Entity Source key Notes
Uptime state.uptime Seconds since last boot
Firmware Version state.version String
Hostname config.hostname Device hostname

These use EntityCategory.DIAGNOSTIC.

DS1820 temperature sensors (dynamic)

One sensor per ROM address found in GET /state → sensors. New probes appearing after startup are added dynamically without a reload.

  • Name comes from sensors#sensor#{rom}#name
  • Unit: sensors#sensor#{rom}#unit (0 = raw, 1 = °C)
  • Decimal places: sensors#sensor#{rom}#unitcomma

Weather simulation sensors (dynamic)

One sensor per channel that has a weather program assigned. Reports the current clouds_state or similar weather simulation parameter.

Reference

sensor

Classes

SunRiserUptimeSensor

Bases: CoordinatorEntity[SunRiserCoordinator], SensorEntity

Device uptime in seconds.

Source code in custom_components/sunriser/sensor.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class SunRiserUptimeSensor(CoordinatorEntity[SunRiserCoordinator], SensorEntity):
    """Device uptime in seconds."""

    _attr_has_entity_name = True
    _attr_translation_key = "uptime"
    _attr_state_class = SensorStateClass.TOTAL_INCREASING
    _attr_native_unit_of_measurement = "s"
    _attr_entity_category = EntityCategory.DIAGNOSTIC
    # Changes every poll — creates many state changes; disabled by default.
    _attr_entity_registry_enabled_default = False

    def __init__(self, coordinator: SunRiserCoordinator) -> None:
        super().__init__(coordinator)
        self._attr_unique_id = f"{coordinator._entry_id}_uptime"
        self._attr_device_info = coordinator.device_info

    @property
    def native_value(self) -> int | None:
        if self.coordinator.data is None:
            return None
        return self.coordinator.data.get("uptime")

SunRiserFirmwareSensor

Bases: CoordinatorEntity[SunRiserCoordinator], SensorEntity

Firmware version reported by the device.

Source code in custom_components/sunriser/sensor.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class SunRiserFirmwareSensor(CoordinatorEntity[SunRiserCoordinator], SensorEntity):
    """Firmware version reported by the device."""

    _attr_has_entity_name = True
    _attr_translation_key = "firmware_version"
    _attr_entity_category = EntityCategory.DIAGNOSTIC

    def __init__(self, coordinator: SunRiserCoordinator) -> None:
        super().__init__(coordinator)
        self._attr_unique_id = f"{coordinator._entry_id}_firmware"
        self._attr_device_info = coordinator.device_info

    @property
    def native_value(self) -> str | None:
        return self.coordinator.config.get("save_version") or None

SunRiserHostnameSensor

Bases: CoordinatorEntity[SunRiserCoordinator], SensorEntity

Hostname configured on the device.

Source code in custom_components/sunriser/sensor.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class SunRiserHostnameSensor(CoordinatorEntity[SunRiserCoordinator], SensorEntity):
    """Hostname configured on the device."""

    _attr_has_entity_name = True
    _attr_translation_key = "hostname"
    _attr_entity_category = EntityCategory.DIAGNOSTIC

    def __init__(self, coordinator: SunRiserCoordinator) -> None:
        super().__init__(coordinator)
        self._attr_unique_id = f"{coordinator._entry_id}_hostname"
        self._attr_device_info = coordinator.device_info

    @property
    def native_value(self) -> str | None:
        return self.coordinator.config.get("hostname") or None

SunRiserTemperatureSensor

Bases: CoordinatorEntity[SunRiserCoordinator], SensorEntity

DS1820 temperature sensor reported in /state.

Source code in custom_components/sunriser/sensor.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class SunRiserTemperatureSensor(CoordinatorEntity[SunRiserCoordinator], SensorEntity):
    """DS1820 temperature sensor reported in /state."""

    _attr_has_entity_name = True
    _attr_device_class = SensorDeviceClass.TEMPERATURE
    _attr_state_class = SensorStateClass.MEASUREMENT
    _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS

    def __init__(
        self,
        coordinator: SunRiserCoordinator,
        entry: ConfigEntry,
        rom: str,
    ) -> None:
        super().__init__(coordinator)
        self._rom = rom
        self._attr_unique_id = f"{entry.entry_id}_sensor_{rom}"
        self._attr_name = coordinator.sensor_name(rom)
        self._attr_device_info = coordinator.device_info

    @property
    def native_value(self) -> float | None:
        return self.coordinator.sensor_value(self._rom)

    @property
    def native_unit_of_measurement(self) -> str:
        # Sensors configured as raw (unit=0) have no meaningful HA unit.
        if self.coordinator.sensor_unit(self._rom) == _UNIT_CELSIUS:
            return UnitOfTemperature.CELSIUS
        return "raw"

SunRiserWeatherChannelSensor

Bases: CoordinatorEntity[SunRiserCoordinator], SensorEntity

Weather simulation state for a single PWM channel.

State = weather_program_id (which program is running on this channel). All other fields (clouds_state, rain ticks, moon state, etc.) are exposed as extra state attributes.

Source code in custom_components/sunriser/sensor.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
class SunRiserWeatherChannelSensor(
    CoordinatorEntity[SunRiserCoordinator], SensorEntity
):
    """Weather simulation state for a single PWM channel.

    State = weather_program_id (which program is running on this channel).
    All other fields (clouds_state, rain ticks, moon state, etc.) are
    exposed as extra state attributes.
    """

    _attr_has_entity_name = True
    _attr_translation_key = "weather_channel"

    def __init__(self, coordinator: SunRiserCoordinator, channel: int) -> None:
        super().__init__(coordinator)
        self._channel = channel
        self._attr_unique_id = f"{coordinator._entry_id}_weather_{channel}"
        self._attr_translation_placeholders = {"channel": coordinator.pwm_name(channel)}
        self._attr_device_info = coordinator.device_info

    def _channel_data(self) -> dict[str, Any] | None:
        if self.coordinator.data is None:
            return None
        weather = self.coordinator.data.get("weather") or []
        idx = self._channel - 1
        if idx >= len(weather):
            return None
        return cast(dict[str, Any] | None, weather[idx])

    @property
    def native_value(self) -> str | None:
        ch = self._channel_data()
        if ch is None:
            return None
        if bool(ch.get("thunder_state")):
            return "thunder"
        if (ch.get("rainmins") or 0) > 0:
            return "rain"
        if bool(ch.get("clouds_state")):
            return "cloudy"
        if bool(ch.get("moon_state")):
            return "moon"
        return "clear"

    # Maps raw tick field → (output attribute name, zero-value label)
    _TICK_FIELDS: dict[str, tuple[str, str]] = {
        "clouds_next_state_tick": ("clouds_next_change_at", "no clouds today"),
        "rain_next_tick": ("rain_next_at", "no rain today"),
        "thunder_next_state_tick": ("thunder_next_change_at", "no thunder today"),
        "moon_next_state_tick": ("moon_next_change_at", "no moon tonight"),
    }
    _RENAME_FIELDS: dict[str, str] = {
        "cloudticks": "cloud_ticks",
        "rainmins": "rain_duration_mins",
        "rainfront_start": "rainfront_start_tick",
        "rainfront_length": "rainfront_length_ticks",
        "stormfront_start": "stormfront_start_tick",
        "stormfront_length": "stormfront_length_ticks",
        "daycount": "day_count",
    }
    _EXCLUDE_FIELDS: frozenset[str] = frozenset(
        {"weather_program_id", "clouds_state", "thunder_state", "moon_state"}
    )
    # State fields whose presence indicates the subsystem is configured.
    _ACTIVE_STATE_FIELDS: dict[str, str] = {
        "clouds_state": "clouds_active",
        "thunder_state": "thunder_active",
        "moon_state": "moon_active",
    }

    def _tick_to_attr(self, tick_value: Any, uptime_ms: int, zero_label: str) -> str:
        if tick_value:
            seconds = round((tick_value - uptime_ms) / 1000)
            return (dt_util.utcnow() + timedelta(seconds=seconds)).isoformat()
        return zero_label

    @property
    def extra_state_attributes(self) -> dict[str, Any]:
        ch = self._channel_data()
        if not ch:
            return {}

        uptime_ms = ((self.coordinator.data or {}).get("uptime") or 0) * 1000
        result: dict[str, Any] = {}

        for k, v in ch.items():
            if k in self._EXCLUDE_FIELDS:
                continue
            if k in self._TICK_FIELDS:
                attr_name, zero_label = self._TICK_FIELDS[k]
                result[attr_name] = self._tick_to_attr(v, uptime_ms, zero_label)
            else:
                result[self._RENAME_FIELDS.get(k, k)] = v

        program_id = ch.get("weather_program_id")
        result["weather_program_id"] = program_id
        result["weather_program_name"] = self.coordinator.weather_program_name(
            program_id
        )

        # Convenience booleans: only included when the firmware reports that
        # subsystem as configured (absent fields → not in this program).
        for state_key, attr_name in self._ACTIVE_STATE_FIELDS.items():
            if state_key in ch:
                result[attr_name] = bool(ch[state_key])
        # rain_active: rain is running when rainmins > 0 (device counts down
        # the remaining minutes of the current rain event).
        if "rainmins" in ch:
            result["rain_active"] = (ch.get("rainmins") or 0) > 0

        return result