def __init__( self, xknx: "XKNX", name: str, group_address: Optional["GroupAddressableType"] = None, group_address_state: Optional["GroupAddressableType"] = None, invert: bool = False, reset_after: Optional[float] = None, device_updated_cb: Optional[DeviceCallbackType] = None, ): """Initialize Switch class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) self.reset_after = reset_after self._reset_task: Optional[asyncio.Task[None]] = None self.switch = RemoteValueSwitch( xknx, group_address, group_address_state, invert=invert, device_name=self.name, after_update_cb=self.after_update, )
def __init__( self, xknx: XKNX, name: str, group_address: GroupAddressesType | None = None, value_type: int | str | None = None, device_updated_cb: DeviceCallbackType | None = None, ): """Initialize Sensor class.""" super().__init__(xknx, name, device_updated_cb) self.sensor_value: RemoteValueSensor | RemoteValueSwitch if value_type == "binary": self.sensor_value = RemoteValueSwitch( xknx, group_address=group_address, sync_state=False, device_name=self.name, after_update_cb=self.after_update, ) else: self.sensor_value = RemoteValueSensor( xknx, group_address=group_address, sync_state=False, device_name=self.name, after_update_cb=self.after_update, value_type=value_type, )
def __init__(self, xknx, name, group_address=None, value_type=None, device_updated_cb=None): """Initialize Sensor class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) self.sensor_value = None if value_type == "binary": self.sensor_value = RemoteValueSwitch( xknx, group_address=group_address, sync_state=False, device_name=self.name, after_update_cb=self.after_update, ) else: self.sensor_value = RemoteValueSensor( xknx, group_address=group_address, sync_state=False, device_name=self.name, after_update_cb=self.after_update, value_type=value_type, )
def test_set(self): """Test setting value.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) self.loop.run_until_complete(remote_value.on()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1)), ), ) self.loop.run_until_complete(remote_value.off()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(0)), ), )
def __init__( self, xknx: "XKNX", name: str, group_address: Optional["GroupAddressableType"] = None, value_type: Optional[str] = None, device_updated_cb: Optional[DeviceCallbackType] = None, ): """Initialize Sensor class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) self.sensor_value: "RemoteValue" if value_type == "binary": self.sensor_value = RemoteValueSwitch( xknx, group_address=group_address, sync_state=False, device_name=self.name, after_update_cb=self.after_update, ) else: self.sensor_value = RemoteValueSensor( xknx, group_address=group_address, sync_state=False, device_name=self.name, after_update_cb=self.after_update, value_type=value_type, )
def __init__( self, xknx, name, feature_name: str, group_address_switch=None, group_address_switch_state=None, group_address_brightness=None, group_address_brightness_state=None, after_update_cb=None, ): self.switch = RemoteValueSwitch( xknx, group_address_switch, group_address_switch_state, device_name=name, feature_name=feature_name + "_state", after_update_cb=after_update_cb, ) self.brightness = RemoteValueScaling( xknx, group_address_brightness, group_address_brightness_state, device_name=name, feature_name=feature_name + "_brightness", after_update_cb=after_update_cb, range_from=0, range_to=255, )
def __init__( self, xknx, name: str, group_address=None, group_address_state=None, invert: Optional[bool] = False, reset_after: Optional[float] = None, device_updated_cb=None, ): """Initialize Switch class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) self.reset_after = reset_after self._reset_task = None self.switch = RemoteValueSwitch( xknx, group_address, group_address_state, invert=invert, device_name=self.name, after_update_cb=self.after_update, )
def __init__( self, xknx: XKNX, name: str, feature_name: str, group_address_switch: GroupAddressesType | None = None, group_address_switch_state: GroupAddressesType | None = None, group_address_brightness: GroupAddressesType | None = None, group_address_brightness_state: GroupAddressesType | None = None, after_update_cb: AsyncCallback | None = None, ): self.switch = RemoteValueSwitch( xknx, group_address_switch, group_address_switch_state, device_name=name, feature_name=feature_name + "_state", after_update_cb=after_update_cb, ) self.brightness = RemoteValueScaling( xknx, group_address_brightness, group_address_brightness_state, device_name=name, feature_name=feature_name + "_brightness", after_update_cb=after_update_cb, range_from=0, range_to=255, )
def __init__( self, xknx: XKNX, name: str, group_address: GroupAddressesType | None = None, group_address_state: GroupAddressesType | None = None, respond_to_read: bool = False, sync_state: bool | int | float | str = True, invert: bool = False, reset_after: float | None = None, device_updated_cb: DeviceCallbackType | None = None, ): """Initialize Switch class.""" super().__init__(xknx, name, device_updated_cb) self.reset_after = reset_after self._reset_task_name = f"switch.reset_{id(self)}" self._reset_task: Task | None = None self.respond_to_read = respond_to_read self.switch = RemoteValueSwitch( xknx, group_address, group_address_state, sync_state=sync_state, invert=invert, device_name=self.name, after_update_cb=self.after_update, )
def __init__(self, xknx, name, group_address_state=None, sync_state=True, ignore_internal_state=False, device_class=None, reset_after=None, actions=None, device_updated_cb=None): """Initialize BinarySensor class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) if actions is None: actions = [] self.actions = actions self.device_class = device_class self.ignore_internal_state = ignore_internal_state self.reset_after = reset_after self.state = False self._count_set_on = 0 self._count_set_off = 0 self._last_set = None self._reset_task = None # TODO: log a warning if reset_after and sync_state are true ? This could cause actions to self-fire. self.remote_value = RemoteValueSwitch( xknx, group_address_state=group_address_state, sync_state=sync_state, device_name=self.name, # after_update called internally after_update_cb=self._state_from_remote_value)
def __init__( self, xknx: "XKNX", name: str, feature_name: str, group_address_switch: Optional["GroupAddressableType"] = None, group_address_switch_state: Optional["GroupAddressableType"] = None, group_address_brightness: Optional["GroupAddressableType"] = None, group_address_brightness_state: Optional["GroupAddressableType"] = None, after_update_cb: Optional[AsyncCallback] = None, ): self.switch = RemoteValueSwitch( xknx, group_address_switch, group_address_switch_state, device_name=name, feature_name=feature_name + "_state", after_update_cb=after_update_cb, ) self.brightness = RemoteValueScaling( xknx, group_address_brightness, group_address_brightness_state, device_name=name, feature_name=feature_name + "_brightness", after_update_cb=after_update_cb, range_from=0, range_to=255, )
def test_process_off(self): """Test process OFF telegram.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) telegram = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTBinary(0)) self.assertEqual(remote_value.value, None) self.loop.run_until_complete(remote_value.process(telegram)) self.assertIsNotNone(remote_value.payload) self.assertEqual(remote_value.value, False)
def test_process(self): """Test process telegram.""" xknx = XKNX(loop=self.loop) remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) telegram = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTBinary(1)) self.assertEqual(remote_value.value, None) self.loop.run_until_complete( asyncio.Task(remote_value.process(telegram))) self.assertIsNotNone(remote_value.payload) self.assertEqual(remote_value.value, True)
async def test_process_off(self): """Test process OFF telegram.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(0)), ) assert remote_value.value is None await remote_value.process(telegram) assert remote_value.telegram is not None assert remote_value.value is False
def test_process(self): """Test process telegram.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1)), ) self.assertEqual(remote_value.value, None) self.loop.run_until_complete(remote_value.process(telegram)) self.assertIsNotNone(remote_value.payload) self.assertEqual(remote_value.value, True)
def __init__( self, xknx: XKNX, name: str, group_address_speed: GroupAddressesType | None = None, group_address_speed_state: GroupAddressesType | None = None, group_address_oscillation: GroupAddressesType | None = None, group_address_oscillation_state: GroupAddressesType | None = None, sync_state: bool | int | float | str = True, device_updated_cb: DeviceCallbackType | None = None, max_step: int | None = None, ): """Initialize fan class.""" super().__init__(xknx, name, device_updated_cb) self.speed: RemoteValueDptValue1Ucount | RemoteValueScaling self.mode = FanSpeedMode.STEP if max_step else FanSpeedMode.PERCENT self.max_step = max_step if self.mode == FanSpeedMode.STEP: self.speed = RemoteValueDptValue1Ucount( xknx, group_address_speed, group_address_speed_state, sync_state=sync_state, device_name=self.name, feature_name="Speed", after_update_cb=self.after_update, ) else: self.speed = RemoteValueScaling( xknx, group_address_speed, group_address_speed_state, sync_state=sync_state, device_name=self.name, feature_name="Speed", after_update_cb=self.after_update, range_from=0, range_to=100, ) self.oscillation = RemoteValueSwitch( xknx, group_address_oscillation, group_address_oscillation_state, sync_state=sync_state, device_name=self.name, feature_name="Oscillation", after_update_cb=self.after_update, )
def __init__( self, xknx: "XKNX", name: str, group_address_speed: Optional["GroupAddressableType"] = None, group_address_speed_state: Optional["GroupAddressableType"] = None, group_address_oscillation: Optional["GroupAddressableType"] = None, group_address_oscillation_state: Optional[ "GroupAddressableType"] = None, device_updated_cb: Optional[DeviceCallbackType] = None, max_step: Optional[int] = None, ): """Initialize fan class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) self.speed: Union[RemoteValueDptValue1Ucount, RemoteValueScaling] self.mode = FanSpeedMode.Step if max_step is not None else FanSpeedMode.Percent self.max_step = max_step if self.mode == FanSpeedMode.Step: self.speed = RemoteValueDptValue1Ucount( xknx, group_address_speed, group_address_speed_state, device_name=self.name, feature_name="Speed", after_update_cb=self.after_update, ) else: self.speed = RemoteValueScaling( xknx, group_address_speed, group_address_speed_state, device_name=self.name, feature_name="Speed", after_update_cb=self.after_update, range_from=0, range_to=100, ) self.oscillation = RemoteValueSwitch( xknx, group_address_oscillation, group_address_oscillation_state, device_name=self.name, feature_name="Oscillation", after_update_cb=self.after_update, )
def test_to_process_error(self): """Test process errornous telegram.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) with self.assertRaises(CouldNotParseTelegram): telegram = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTArray(0x01)) self.loop.run_until_complete(remote_value.process(telegram)) with self.assertRaises(CouldNotParseTelegram): telegram = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTBinary(3)) self.loop.run_until_complete(remote_value.process(telegram)) # pylint: disable=pointless-statement remote_value.value
def __init__(self, xknx, name, group_address=None, group_address_state=None, device_updated_cb=None): """Initialize Switch class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) self.switch = RemoteValueSwitch(xknx, group_address, group_address_state, device_name=self.name, after_update_cb=self.after_update)
def test_set(self): """Test setting value.""" xknx = XKNX(loop=self.loop) remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) self.loop.run_until_complete(asyncio.Task(remote_value.on())) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/3'), payload=DPTBinary(1))) self.loop.run_until_complete(asyncio.Task(remote_value.off())) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/3'), payload=DPTBinary(0)))
def test_read_state(self): """Test read state while waiting for the result.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address_state="1/2/3") with patch("xknx.remote_value.RemoteValue.payload_valid") as patch_valid, patch( "xknx.core.ValueReader.read", new_callable=MagicMock ) as patch_read: patch_valid.return_value = True fut = asyncio.Future() telegram = Telegram(GroupAddress("1/2/3"), payload=DPTBinary(1)) fut.set_result(telegram) patch_read.return_value = fut self.loop.run_until_complete(remote_value.read_state(wait_for_result=True)) self.assertTrue(remote_value.value)
async def test_set(self): """Test setting value.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) await remote_value.on() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1)), ) await remote_value.off() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(0)), )
async def test_to_process_error(self): """Test process errornous telegram.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) with pytest.raises(CouldNotParseTelegram): telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray(0x01)), ) await remote_value.process(telegram) with pytest.raises(CouldNotParseTelegram): telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(3)), ) await remote_value.process(telegram) # pylint: disable=pointless-statement remote_value.value
def __init__( self, xknx, name: str, group_address_state=None, invert: Optional[bool] = False, sync_state: bool = True, ignore_internal_state: bool = False, device_class: str = None, reset_after: Optional[float] = None, actions: List[Action] = None, context_timeout: Optional[float] = None, device_updated_cb=None, ): """Initialize BinarySensor class.""" # pylint: disable=too-many-arguments super().__init__(xknx, name, device_updated_cb) if actions is None: actions = [] self.actions = actions self.device_class = device_class self.ignore_internal_state = ignore_internal_state or bool( context_timeout) self.reset_after = reset_after self.state = None self._context_timeout = context_timeout self._count_set_on = 0 self._count_set_off = 0 self._last_set = None self._reset_task = None self._context_task = None # TODO: log a warning if reset_after and sync_state are true ? This could cause actions to self-fire. self.remote_value = RemoteValueSwitch( xknx, group_address_state=group_address_state, invert=invert, sync_state=sync_state, device_name=self.name, # after_update called internally after_update_cb=self._state_from_remote_value, )
async def test_to_process_error(self): """Test process errornous telegram.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address=GroupAddress("1/2/3")) telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray(0x01)), ) assert await remote_value.process(telegram) is False telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(3)), ) assert await remote_value.process(telegram) is False assert remote_value.value is None
async def test_read_state(self): """Test read state while waiting for the result.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address_state="1/2/3") with patch("xknx.remote_value.RemoteValue.payload_valid" ) as patch_valid, patch( "xknx.core.ValueReader.read", new_callable=AsyncMock) as patch_read: patch_valid.return_value = True telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1)), ) patch_read.return_value = telegram await remote_value.read_state(wait_for_result=True) assert remote_value.telegram == telegram assert remote_value.value
async def test_read_state_none(self): """Test read state while waiting for the result but got None.""" xknx = XKNX() remote_value = RemoteValueSwitch(xknx, group_address_state="1/2/3") with patch("xknx.remote_value.RemoteValue.payload_valid" ) as patch_valid, patch( "xknx.core.ValueReader.read", new_callable=AsyncMock) as patch_read, patch( "logging.Logger.warning") as mock_info: patch_valid.return_value = False patch_read.return_value = None await remote_value.read_state(wait_for_result=True) mock_info.assert_called_with( "Could not sync group address '%s' (%s - %s)", GroupAddress("1/2/3"), "Unknown", "State", )
def __init__( self, xknx: XKNX, name: str, group_address_state: GroupAddressesType = None, invert: bool = False, sync_state: bool = True, ignore_internal_state: bool = False, device_class: str | None = None, reset_after: float | None = None, context_timeout: float | None = None, ha_value_template: Any = None, device_updated_cb: DeviceCallbackType | None = None, ): """Initialize BinarySensor class.""" super().__init__(xknx, name, device_updated_cb) self.device_class = device_class self.ha_value_template = ha_value_template self.ignore_internal_state = ignore_internal_state or bool(context_timeout) self.reset_after = reset_after self.state: bool | None = None self._context_timeout = context_timeout self._count_set_on = 0 self._count_set_off = 0 self._last_set: float | None = None self._reset_task: asyncio.Task[None] | None = None self._context_task: asyncio.Task[None] | None = None # TODO: log a warning if reset_after and sync_state are true ? This could cause actions to self-fire. self.remote_value = RemoteValueSwitch( xknx, group_address_state=group_address_state, invert=invert, sync_state=sync_state, device_name=self.name, # after_update called internally after_update_cb=self._state_from_remote_value, )
def __init__( self, xknx: XKNX, name: str, group_address_state: GroupAddressesType = None, invert: bool = False, sync_state: bool | int | float | str = True, ignore_internal_state: bool = False, reset_after: float | None = None, context_timeout: float | None = None, device_updated_cb: DeviceCallbackType | None = None, ): """Initialize BinarySensor class.""" super().__init__(xknx, name, device_updated_cb) self.ignore_internal_state = ignore_internal_state or bool( context_timeout) self.reset_after = reset_after self.state: bool | None = None self._context_timeout = context_timeout self._count_set_on = 0 self._count_set_off = 0 self._last_set: float | None = None self._reset_task_name = f"binary_sensor.reset_{id(self)}" self._context_task_name = f"binary_sensor.context_{id(self)}" self._reset_task: Task | None = None self._context_task: Task | None = None self.remote_value = RemoteValueSwitch( xknx, group_address_state=group_address_state, invert=invert, sync_state=sync_state, device_name=self.name, # after_update called internally after_update_cb=self._state_from_remote_value, )
def test_read_state_none(self): """Test read state while waiting for the result but got None.""" xknx = XKNX(loop=self.loop) remote_value = RemoteValueSwitch(xknx, group_address_state="1/2/3") with patch("xknx.remote_value.RemoteValue.payload_valid" ) as patch_valid, patch( "xknx.core.ValueReader.read", new_callable=MagicMock) as patch_read, patch( "logging.Logger.warning") as mock_info: fut = asyncio.Future() fut.set_result(None) patch_valid.return_value = False patch_read.return_value = fut self.loop.run_until_complete( remote_value.read_state(wait_for_result=True)) mock_info.assert_called_with( "Could not sync group address '%s' (%s - %s)", GroupAddress("1/2/3"), "Unknown", "State", )