Code optimization for get_device_state function