Skip to content

Commit f302ac4

Browse files
authored
Add invert_day_night attribute to Weather device (#1801)
* Add `invert_day_night` attribute to Weather device * Update weather.py * Update weather_test.py * Update weather.md
1 parent 46e9cbe commit f302ac4

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ nav_order: 2
1515
### Devices
1616

1717
- ExposeSensor: `cooldown` is extended to wait for connection if not established.
18+
- Weather: Add `invert_day_night` option to invert day/night value.
1819

1920
### Internals
2021

docs/weather.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ The weather device is basically a set of sensors that you can obtain from your w
3939

4040
- **xknx** is the XKNX object.
4141
- **name** is the name of the object.
42-
- **group_address_temperature** KNX address of current outside temperature.
42+
- **group_address_temperature** KNX address of current outside temperature. **DPT 9.001**
4343
- **group_address_brightness_south** KNX address for the brightness to south **DPT 9.004**.
4444
- **group_address_brightness_west** KNX address for the brightness to west **DPT 9.004**.
4545
- **group_address_brightness_east** KNX address for the brightness to east **DPT 9.004**.
4646
- **group_address_wind_speed** KNX address for current wind speed. **DPT 9.005**
4747
- **group_address_wind_bearing** KNX address for current wind bearing. **DPT 5.003**
48-
- **group_address_rain_alarm** KNX address for reading if rain alarm is on/off.
49-
- **group_address_wind_alarm** KNX address for reading if wind alarm is on/off.
50-
- **group_address_frost_alarm** KNX address for reading if frost alarm is on/off.
51-
- **group_address_day_night** KNX address for reading a day/night object.
48+
- **group_address_rain_alarm** KNX address for reading if rain alarm is on/off. **DPT 1**
49+
- **group_address_wind_alarm** KNX address for reading if wind alarm is on/off. **DPT 1**
50+
- **group_address_frost_alarm** KNX address for reading if frost alarm is on/off. **DPT 1**
51+
- **group_address_day_night** KNX address for reading day/night state. By default the value True represents day and False represents night. **DPT 1**
5252
- **group_address_air_pressure** KNX address reading current air pressure. **DPT 9.006 or 14.058**
5353
- **group_address_humidity** KNX address for reading current humidity. **DPT 9.007**
5454
- **sync_state** Periodically sync the state.
5555
- **device_updated_cb** Callback for each update.
56+
- **invert_day_night** Invert the day/night state.
5657

5758
```python
5859

test/devices_tests/weather_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,15 @@ async def test_cloudy_winter(self) -> None:
320320
== WeatherCondition.CLOUDY
321321
)
322322

323-
async def test_day_night(self) -> None:
323+
@pytest.mark.parametrize("invert_day_night", [False, True])
324+
async def test_day_night(self, invert_day_night: bool) -> None:
324325
"""Test day night mapping."""
325326
xknx = XKNX()
326327
weather: Weather = Weather(
327-
name="weather", xknx=xknx, group_address_day_night="1/3/20"
328+
name="weather",
329+
xknx=xknx,
330+
group_address_day_night="1/3/20",
331+
invert_day_night=invert_day_night,
328332
)
329333

330334
weather.process(
@@ -333,8 +337,12 @@ async def test_day_night(self) -> None:
333337
payload=GroupValueWrite(value=DPTBinary(0)),
334338
)
335339
)
336-
337-
assert weather.ha_current_state() == WeatherCondition.CLEAR_NIGHT
340+
if invert_day_night:
341+
assert weather.ha_current_state() == WeatherCondition.EXCEPTIONAL
342+
assert weather.day_night is True
343+
else:
344+
assert weather.ha_current_state() == WeatherCondition.CLEAR_NIGHT
345+
assert weather.day_night is False
338346

339347
def test_weather_default(self) -> None:
340348
"""Test default state mapping."""

xknx/devices/weather.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(
103103
group_address_humidity: GroupAddressesType = None,
104104
sync_state: bool | int | float | str = True,
105105
device_updated_cb: DeviceCallbackType[Weather] | None = None,
106+
invert_day_night: bool = False,
106107
) -> None:
107108
"""Initialize Weather class."""
108109
super().__init__(xknx, name, device_updated_cb)
@@ -211,6 +212,7 @@ def __init__(
211212
device_name=self.name,
212213
feature_name="Day/Night",
213214
after_update_cb=self.after_update,
215+
invert=invert_day_night,
214216
)
215217

216218
self._air_pressure = RemoteValueByLength(
@@ -314,7 +316,7 @@ def frost_alarm(self) -> bool | None:
314316

315317
@property
316318
def day_night(self) -> bool | None:
317-
"""Return day or night."""
319+
"""Return day or night. `True` for day and `False` for night."""
318320
return self._day_night.value
319321

320322
@property

0 commit comments

Comments
 (0)