async def main(): """Connect to KNX/IP device and create a weather device and read its sensors.""" xknx = XKNX() await xknx.start(connection_config=ConnectionConfig( connection_type=ConnectionType.TUNNELING, local_ip="192.168.0.50", gateway_ip="192.168.0.100", )) weather = Weather( xknx, "Home", group_address_temperature="7/0/1", group_address_brightness_south="7/0/5", group_address_brightness_east="7/0/4", group_address_brightness_west="7/0/3", group_address_wind_speed="7/0/2", group_address_day_night="7/0/7", group_address_rain_alarm="7/0/0", ) await weather.sync(wait_for_result=True) print(weather.max_brightness) print(weather.ha_current_state()) print(weather) await xknx.stop()
def test_temperature(self): """Test resolve state with temperature.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_temperature="1/3/4") weather._temperature.payload = DPTArray((0x19, 0xA)) self.assertTrue(weather.has_group_address(GroupAddress("1/3/4"))) self.assertEqual(weather.temperature, 21.28) self.assertEqual(weather._temperature.unit_of_measurement, "°C") self.assertEqual(weather._temperature.ha_device_class, "temperature")
def test_weather(self): """Test string representation of switch object.""" xknx = XKNX() weather = Weather( xknx, "Home", group_address_temperature="7/0/1", group_address_brightness_south="7/0/5", group_address_brightness_east="7/0/4", group_address_brightness_west="7/0/3", group_address_wind_speed="7/0/2", group_address_wind_bearing="7/0/6", group_address_day_night="7/0/7", group_address_rain_alarm="7/0/0", group_address_frost_alarm="7/0/8", create_sensors=True, group_address_air_pressure="7/0/9", group_address_humidity="7/0/9", group_address_wind_alarm="7/0/10", ) self.assertEqual( str(weather), '<Weather name="Home" temperature="None/GroupAddress("7/0/1")/None/None" ' 'brightness_south="None/GroupAddress("7/0/5")/None/None" brightness_north="None/None/None/None" ' 'brightness_west="None/GroupAddress("7/0/3")/None/None" ' 'brightness_east="None/GroupAddress("7/0/4")/None/None" wind_speed="None/GroupAddress("7/0/2")/None/None" ' 'wind_bearing="None/GroupAddress("7/0/6")/None/None" rain_alarm="None/GroupAddress("7/0/0")/None/None" ' 'wind_alarm="None/GroupAddress("7/0/10")/None/None" frost_alarm="None/GroupAddress("7/0/8")/None/None" ' 'day_night="None/GroupAddress("7/0/7")/None/None" air_pressure="None/GroupAddress("7/0/9")/None/None" ' 'humidity="None/GroupAddress("7/0/9")/None/None" />', ) telegram = Telegram( destination_address=GroupAddress("7/0/10"), direction=TelegramDirection.INCOMING, payload=GroupValueWrite(DPTBinary(1)), ) self.loop.run_until_complete(weather.process_group_write(telegram)) self.assertEqual( str(weather), '<Weather name="Home" temperature="None/GroupAddress("7/0/1")/None/None" ' 'brightness_south="None/GroupAddress("7/0/5")/None/None" brightness_north="None/None/None/None" ' 'brightness_west="None/GroupAddress("7/0/3")/None/None" ' 'brightness_east="None/GroupAddress("7/0/4")/None/None" wind_speed="None/GroupAddress("7/0/2")/None/None" ' 'wind_bearing="None/GroupAddress("7/0/6")/None/None" rain_alarm="None/GroupAddress("7/0/0")/None/None" ' 'wind_alarm="None/GroupAddress("7/0/10")/<DPTBinary value="1" />/True" ' 'frost_alarm="None/GroupAddress("7/0/8")/None/None" day_night="None/GroupAddress("7/0/7")/None/None" ' 'air_pressure="None/GroupAddress("7/0/9")/None/None" humidity="None/GroupAddress("7/0/9")/None/None" />', )
def test_iter_remote_values(self): """Test sensor has group address.""" xknx = XKNX(loop=self.loop) weather = Weather( name="weather", xknx=xknx, group_address_temperature="1/3/4", group_address_rain_alarm="1/4/5", group_address_brightness_south="7/7/0", ) self.assertTrue(weather.has_group_address(GroupAddress("1/3/4"))) self.assertTrue(weather.has_group_address(GroupAddress("7/7/0"))) self.assertTrue(weather.has_group_address(GroupAddress("1/4/5"))) self.assertFalse(weather.has_group_address(GroupAddress("1/2/4")))
def test_unique_id(self): """Test unique id functionality.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_temperature="1/3/4") assert weather.unique_id == "1/3/4"
def test_weather_default(self): """Test default state mapping.""" xknx = XKNX(loop=self.loop) weather: Weather = Weather(name="weather", xknx=xknx) self.assertEqual(weather.ha_current_state(), WeatherCondition.exceptional)
async def test_temperature(self): """Test resolve state with temperature.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_temperature="1/3/4") await weather.process( Telegram( destination_address=GroupAddress("1/3/4"), payload=GroupValueWrite(value=DPTArray((0x19, 0xA))), ) ) assert weather.has_group_address(GroupAddress("1/3/4")) assert weather.temperature == 21.28 assert weather._temperature.unit_of_measurement == "°C" assert weather._temperature.ha_device_class == "temperature"
def test_pressure(self): """Test resolve state with pressure.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_air_pressure="1/3/4") weather._air_pressure.payload = DPTArray((0x6C, 0xAD)) self.assertEqual(weather.air_pressure, 98058.24) self.assertEqual(weather._air_pressure.unit_of_measurement, "Pa") self.assertEqual(weather._air_pressure.ha_device_class, "pressure")
def test_has_group_address(self): """Test sensor has group address.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_temperature="1/3/4") assert weather._temperature.has_group_address(GroupAddress("1/3/4")) assert not weather._temperature.has_group_address( GroupAddress("1/2/4"))
async def test_brightness(self): """Test resolve state for brightness east, west and south.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_brightness_east="1/3/5", group_address_brightness_south="1/3/6", group_address_brightness_west="1/3/7", group_address_brightness_north="1/3/8", group_address_temperature="1/4/4", ) await weather.process( Telegram( destination_address=GroupAddress("1/3/5"), payload=GroupValueWrite(value=DPTArray((0x7C, 0x5E))), ) ) await weather.process( Telegram( destination_address=GroupAddress("1/3/7"), payload=GroupValueWrite(value=DPTArray((0x7C, 0x5C))), ) ) await weather.process( Telegram( destination_address=GroupAddress("1/3/6"), payload=GroupValueWrite(value=DPTArray((0x7C, 0x5A))), ) ) await weather.process( Telegram( destination_address=GroupAddress("1/3/8"), payload=GroupValueWrite(value=DPTArray((0x7C, 0x5A))), ) ) assert weather.brightness_east == 366346.24 assert weather._brightness_east.unit_of_measurement == "lx" assert weather._brightness_east.ha_device_class == "illuminance" assert weather.brightness_west == 365690.88 assert weather._brightness_west.unit_of_measurement == "lx" assert weather._brightness_west.ha_device_class == "illuminance" assert weather.brightness_south == 365035.52 assert weather._brightness_south.unit_of_measurement == "lx" assert weather._brightness_south.ha_device_class == "illuminance" assert weather.brightness_north == 365035.52 assert weather._brightness_north.unit_of_measurement == "lx" assert weather._brightness_north.ha_device_class == "illuminance"
def test_day_night(self): """Test day night mapping.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_day_night="1/3/20" ) weather._day_night.payload = DPTBinary(0) self.assertEqual(weather.ha_current_state(), WeatherCondition.clear_night)
def test_has_group_address(self): """Test sensor has group address.""" xknx = XKNX(loop=self.loop) weather = Weather(name="weather", xknx=xknx, group_address_temperature="1/3/4") self.assertTrue( weather._temperature.has_group_address(GroupAddress("1/3/4"))) self.assertFalse( weather._temperature.has_group_address(GroupAddress("1/2/4")))
def test_wind_bearing(self): """Test wind bearing received.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_brightness_east="1/3/8" ) weather._wind_bearing.payload = DPTArray((0xBF,)) self.assertEqual(weather.wind_bearing, 270) self.assertEqual(weather._wind_bearing.unit_of_measurement, "°") self.assertEqual(weather._wind_bearing.ha_device_class, None)
def test_state_lightning(self): """Test current_state returns lightning if wind alarm and rain alarm are true.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_rain_alarm="1/3/8", group_address_wind_alarm="1/3/9", ) weather._rain_alarm.value = True weather._wind_alarm.value = True assert weather.ha_current_state() == WeatherCondition.LIGHTNING_RAINY
def test_rain_alarm(self): """Test basic state mapping.""" xknx = XKNX(loop=self.loop) weather: Weather = Weather( name="weather", xknx=xknx, group_address_rain_alarm="1/3/8", group_address_wind_alarm="1/3/9", group_address_frost_alarm="1/3/10", ) weather._rain_alarm.payload = DPTBinary(1) self.assertEqual(weather.ha_current_state(), WeatherCondition.rainy)
def test_state_snowy_rainy(self): """Test snow rain if frost alarm and rain alarm are true.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_rain_alarm="1/3/8", group_address_frost_alarm="1/3/10", ) weather._rain_alarm.value = True weather._frost_alarm.value = True assert weather.ha_current_state() == WeatherCondition.SNOWY_RAINY
async def main(): """Connect to KNX/IP device and create a weather device and read its sensors.""" xknx = XKNX() await xknx.start() weather = Weather( xknx, "Home", group_address_temperature="7/0/1", group_address_brightness_south="7/0/5", group_address_brightness_east="7/0/4", group_address_brightness_west="7/0/3", group_address_wind_speed="7/0/2", group_address_day_night="7/0/7", group_address_rain_alarm="7/0/0", ) await weather.sync(wait_for_result=True) print(weather.max_brightness) print(weather.ha_current_state()) print(weather) await xknx.stop()
async def test_day_night(self): """Test day night mapping.""" xknx = XKNX() weather: Weather = Weather(name="weather", xknx=xknx, group_address_day_night="1/3/20") await weather.process( Telegram( destination_address=GroupAddress("1/3/20"), payload=GroupValueWrite(value=DPTBinary(0)), )) assert weather.ha_current_state() == WeatherCondition.CLEAR_NIGHT
def test_rain_alarm(self): """Test basic state mapping.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_rain_alarm="1/3/8", group_address_wind_alarm="1/3/9", group_address_frost_alarm="1/3/10", ) weather._rain_alarm.value = True assert weather.ha_current_state() == WeatherCondition.RAINY
def test_state_lightning(self): """Test current_state returns lightning if wind alarm and rain alarm are true.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_rain_alarm="1/3/8", group_address_wind_alarm="1/3/9", ) weather._rain_alarm.payload = DPTBinary(1) weather._wind_alarm.payload = DPTBinary(1) self.assertEqual(weather.ha_current_state(), WeatherCondition.lightning_rainy)
def test_state_snowy_rainy(self): """Test snow rain if frost alarm and rain alarm are true.""" xknx = XKNX() weather: Weather = Weather( name="weather", xknx=xknx, group_address_rain_alarm="1/3/8", group_address_frost_alarm="1/3/10", ) weather._rain_alarm.payload = DPTBinary(1) weather._frost_alarm.payload = DPTBinary(1) self.assertEqual(weather.ha_current_state(), WeatherCondition.snowy_rainy)
def test_humidity(self): """Test humidity.""" xknx = XKNX(loop=self.loop) weather = Weather(name="weather", xknx=xknx, group_address_humidity="1/2/4") weather._humidity.payload = DPTArray(( 0x7E, 0xE1, )) self.assertEqual(weather.humidity, 577044.48) self.assertEqual(weather._humidity.unit_of_measurement, "%") self.assertEqual(weather._humidity.ha_device_class, "humidity")
async def test_weather(self): """Test string representation of switch object.""" xknx = XKNX() weather = Weather( xknx, "Home", group_address_temperature="7/0/1", group_address_brightness_south="7/0/5", group_address_brightness_east="7/0/4", group_address_brightness_west="7/0/3", group_address_wind_speed="7/0/2", group_address_wind_bearing="7/0/6", group_address_day_night="7/0/7", group_address_rain_alarm="7/0/0", group_address_frost_alarm="7/0/8", group_address_air_pressure="7/0/9", group_address_humidity="7/0/9", group_address_wind_alarm="7/0/10", ) assert ( str(weather) == '<Weather name="Home" temperature=<None, 7/0/1, [], None /> ' "brightness_south=<None, 7/0/5, [], None /> brightness_north=<None, None, [], None /> " "brightness_west=<None, 7/0/3, [], None /> " "brightness_east=<None, 7/0/4, [], None /> wind_speed=<None, 7/0/2, [], None /> " "wind_bearing=<None, 7/0/6, [], None /> rain_alarm=<None, 7/0/0, [], None /> " "wind_alarm=<None, 7/0/10, [], None /> frost_alarm=<None, 7/0/8, [], None /> " "day_night=<None, 7/0/7, [], None /> air_pressure=<None, 7/0/9, [], None /> " "humidity=<None, 7/0/9, [], None /> />" ) telegram = Telegram( destination_address=GroupAddress("7/0/10"), direction=TelegramDirection.INCOMING, payload=GroupValueWrite(DPTBinary(1)), ) await weather.process_group_write(telegram) assert ( str(weather) == '<Weather name="Home" temperature=<None, 7/0/1, [], None /> ' "brightness_south=<None, 7/0/5, [], None /> brightness_north=<None, None, [], None /> " "brightness_west=<None, 7/0/3, [], None /> " "brightness_east=<None, 7/0/4, [], None /> wind_speed=<None, 7/0/2, [], None /> " "wind_bearing=<None, 7/0/6, [], None /> rain_alarm=<None, 7/0/0, [], None /> " "wind_alarm=<None, 7/0/10, [], True /> " "frost_alarm=<None, 7/0/8, [], None /> day_night=<None, 7/0/7, [], None /> " "air_pressure=<None, 7/0/9, [], None /> humidity=<None, 7/0/9, [], None /> />" )
async def test_humidity(self): """Test humidity.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_humidity="1/2/4") await weather.process( Telegram( destination_address=GroupAddress("1/2/4"), payload=GroupValueWrite(value=DPTArray((0x7E, 0xE1))), ) ) assert weather.humidity == 577044.48 assert weather._humidity.unit_of_measurement == "%" assert weather._humidity.ha_device_class == "humidity"
async def test_pressure(self): """Test resolve state with pressure.""" xknx = XKNX() weather = Weather(name="weather", xknx=xknx, group_address_air_pressure="1/3/4") await weather.process( Telegram( destination_address=GroupAddress("1/3/4"), payload=GroupValueWrite(value=DPTArray((0x6C, 0xAD))), ) ) assert weather.air_pressure == 98058.24 assert weather._air_pressure.unit_of_measurement == "Pa" assert weather._air_pressure.ha_device_class == "pressure"
def test_expose_sensor(self): """Test default state mapping.""" xknx = XKNX(loop=self.loop) Weather( name="weather", xknx=xknx, group_address_brightness_east="1/3/5", group_address_brightness_south="1/3/6", group_address_brightness_west="1/3/7", ) self.assertEqual(len(xknx.devices), 1) Weather( name="weather", xknx=xknx, group_address_brightness_east="1/3/5", group_address_brightness_south="1/3/6", group_address_brightness_west="1/3/7", group_address_wind_alarm="1/5/4", expose_sensors=True, ) self.assertEqual(len(xknx.devices), 6)
def test_create_sensor(self): """Test default state mapping.""" xknx = XKNX() Weather( name="weather", xknx=xknx, group_address_brightness_east="1/3/5", group_address_brightness_south="1/3/6", group_address_brightness_west="1/3/7", ) assert len(xknx.devices) == 1 Weather( name="weather", xknx=xknx, group_address_brightness_east="1/3/5", group_address_brightness_south="1/3/6", group_address_brightness_west="1/3/7", group_address_wind_alarm="1/5/4", create_sensors=True, ) assert len(xknx.devices) == 6
def test_wind_speed(self): """Test wind speed received.""" xknx = XKNX(loop=self.loop) weather: Weather = Weather(name="weather", xknx=xknx, group_address_brightness_east="1/3/8") weather._wind_speed.payload = DPTArray(( 0x7D, 0x98, )) self.assertEqual(weather.wind_speed, 469237.76) self.assertEqual(weather._wind_speed.unit_of_measurement, "m/s") self.assertEqual(weather._wind_speed.ha_device_class, None)
async def test_wind_speed(self): """Test wind speed received.""" xknx = XKNX() weather: Weather = Weather(name="weather", xknx=xknx, group_address_wind_speed="1/3/8") await weather.process( Telegram( destination_address=GroupAddress("1/3/8"), payload=GroupValueWrite(value=DPTArray((0x7D, 0x98))), )) assert weather.wind_speed == 469237.76 assert weather._wind_speed.unit_of_measurement == "m/s" assert weather._wind_speed.ha_device_class is None
async def test_wind_bearing(self): """Test wind bearing received.""" xknx = XKNX() weather: Weather = Weather(name="weather", xknx=xknx, group_address_wind_bearing="1/3/8") await weather.process( Telegram( destination_address=GroupAddress("1/3/8"), payload=GroupValueWrite(value=DPTArray((0xBF, ))), )) assert weather.wind_bearing == 270 assert weather._wind_bearing.unit_of_measurement == "°" assert weather._wind_bearing.ha_device_class is None