def resolve_state(self): """Return the current state of the sensor as a human readable string.""" # pylint: disable=invalid-name,too-many-return-statements if self.state is None: return None elif self.value_type == 'percent' and \ isinstance(self.state, DPTArray) and \ len(self.state.value) == 1: # TODO: Instanciate DPTScaling object with DPTArray class return "{0}".format(DPTScaling().from_knx(self.state.value)) elif self.value_type == 'humidity': return DPTHumidity().from_knx(self.state.value) elif self.value_type == 'temperature': return DPTTemperature().from_knx(self.state.value) elif self.value_type == 'illuminance': return DPTLux().from_knx(self.state.value) elif self.value_type == 'speed_ms': return DPTWsp().from_knx(self.state.value) elif self.value_type == 'current': return DPTUElCurrentmA().from_knx(self.state.value) elif isinstance(self.state, DPTArray): return ','.join('0x%02x' % i for i in self.state.value) elif isinstance(self.state, DPTBinary): return "{0:b}".format(self.state.value) raise TypeError()
def resolve_state(self): if self.state is None: return None elif self.value_type == 'percent' and \ isinstance(self.state, DPTArray) and \ len(self.state.value) == 1: # TODO: Instanciate DPTScaling object with DPTArray class return "{0}".format(DPTScaling().from_knx(self.state.value)) elif self.value_type == 'binary': return self.binary_state() elif isinstance(self.state, DPTArray): return ','.join('0x%02x' % i for i in self.state.value) elif isinstance(self.state, DPTBinary): return "{0:b}".format(self.state.value) raise TypeError()
def test_value_99_pct(self): """Test parsing and streaming of DPTScaling 99%.""" self.assertEqual(DPTScaling().to_knx(99), (0xFC, )) self.assertEqual(DPTScaling().from_knx((0xFC, )), 99)
def test_value_75(self): self.assertEqual(DPTScaling().to_knx(75), (0xC0, )) self.assertEqual(DPTScaling().from_knx((0xC0, )), 75)
def test_value_30_pct(self): """Test parsing and streaming of DPTScaling 30%.""" self.assertEqual(DPTScaling().to_knx(30), (0x4C, )) self.assertEqual(DPTScaling().from_knx((0x4C, )), 30)
def test_min(self): """Test parsing and streaming of DPTScaling DPTScaling.value_min.""" self.assertEqual(DPTScaling().to_knx(DPTScaling.value_min), (0x00,)) self.assertEqual(DPTScaling().from_knx((0x00,)), DPTScaling.value_min)
def test_value_zero(self): self.assertEqual(DPTScaling().to_knx(0), (0x00, )) self.assertEqual(DPTScaling().from_knx((0x00, )), 0)
def test_value_75(self): """Test parsing and streaming of DPTScaling 75%.""" self.assertEqual(DPTScaling().to_knx(75), (0xC0,)) self.assertEqual(DPTScaling().from_knx((0xC0,)), 75)
def test_value_zero(self): """Test parsing and streaming of DPTScaling 0%.""" self.assertEqual(DPTScaling().to_knx(0), (0x00,)) self.assertEqual(DPTScaling().from_knx((0x00,)), 0)
def test_from_knx_wrong_parameter(self): with self.assertRaises(ConversionError): DPTScaling().from_knx((0xF8, 0x01, 0x23))
def test_from_knx_wrong_parameter2(self): with self.assertRaises(ConversionError): DPTScaling().from_knx(("0x23"))
def test_to_knx_max_exceeded(self): with self.assertRaises(ConversionError): DPTScaling().to_knx(DPTScaling.value_max + 1)
def test_to_knx_wrong_parameter(self): with self.assertRaises(ConversionError): DPTScaling().to_knx("fnord")
def test_min(self): self.assertEqual(DPTScaling().to_knx(DPTScaling.value_min), (0x00, )) self.assertEqual(DPTScaling().from_knx((0x00, )), DPTScaling.value_min)
def test_max(self): self.assertEqual(DPTScaling().to_knx(DPTScaling.value_max), (0xFF, )) self.assertEqual(DPTScaling().from_knx((0xFF, )), DPTScaling.value_max)
def test_value_100(self): self.assertEqual(DPTScaling().to_knx(100), (0xFF, )) self.assertEqual(DPTScaling().from_knx((0xFF, )), 100)
def test_from_knx_wrong_value(self): """Test parsing of DPTScaling with value which exceeds limits.""" with self.assertRaises(ConversionError): DPTScaling().from_knx((0x256, ))
def test_value_float(self): self.assertEqual(DPTScaling().to_knx(50.00), (0x80, ))
def test_value_50(self): """Test parsing and streaming of DPTScaling 50%.""" self.assertEqual(DPTScaling().to_knx(50), (0x80,)) self.assertEqual(DPTScaling().from_knx((0x80,)), 50)
def test_value_50(self): self.assertEqual(DPTScaling().to_knx(50), (0x80, )) self.assertEqual(DPTScaling().from_knx((0x80, )), 50)
def test_value_25(self): """Test parsing and streaming of DPTScaling 25%.""" self.assertEqual(DPTScaling().to_knx(25), (0x40,)) self.assertEqual(DPTScaling().from_knx((0x40,)), 25)
def test_to_knx_max_exceeded(self): """Test parsing of DPTScaling with wrong value (overflow).""" with self.assertRaises(ConversionError): DPTScaling().to_knx(DPTScaling.value_max + 1)
def test_value_100(self): """Test parsing and streaming of DPTScaling 100%.""" self.assertEqual(DPTScaling().to_knx(100), (0xFF,)) self.assertEqual(DPTScaling().from_knx((0xFF,)), 100)
def test_from_knx_wrong_parameter(self): """Test parsing of DPTScaling with wrong value (3 byte array).""" with self.assertRaises(ConversionError): DPTScaling().from_knx((0xF8, 0x01, 0x23))
def test_value_float(self): """Test parsing of DPTScaling with float value.""" self.assertEqual(DPTScaling().to_knx(50.00), (0x80,))
def test_value_25(self): self.assertEqual(DPTScaling().to_knx(25), (0x40, )) self.assertEqual(DPTScaling().from_knx((0x40, )), 25)
def test_to_knx_wrong_parameter(self): """Test parsing of DPTScaling with wrong value (string).""" with self.assertRaises(ConversionError): DPTScaling().to_knx("fnord")
def to_knx(self, value): """Convert value to payload.""" if not self.invert: value = 100 - value return DPTArray(DPTScaling.to_knx(value))
def test_from_knx_wrong_parameter2(self): """Test parsing of DPTScaling with wrong value (array containing string).""" with self.assertRaises(ConversionError): DPTScaling().from_knx(("0x23"))
def from_knx(self, payload): """Convert current payload to value.""" value = DPTScaling.from_knx(payload.value) if not self.invert: value = 100 - value return value